1/*
2 * MPEG-4 encoder/decoder internal header.
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#ifndef AVCODEC_MPEG4VIDEO_H
24#define AVCODEC_MPEG4VIDEO_H
25
26#include <stdint.h>
27
28#include "mpegvideo.h"
29
30// shapes
31#define RECT_SHAPE       0
32#define BIN_SHAPE        1
33#define BIN_ONLY_SHAPE   2
34#define GRAY_SHAPE       3
35
36#define SIMPLE_VO_TYPE           1
37#define CORE_VO_TYPE             3
38#define MAIN_VO_TYPE             4
39#define NBIT_VO_TYPE             5
40#define ARTS_VO_TYPE            10
41#define ACE_VO_TYPE             12
42#define SIMPLE_STUDIO_VO_TYPE   14
43#define CORE_STUDIO_VO_TYPE     15
44#define ADV_SIMPLE_VO_TYPE      17
45
46#define VOT_VIDEO_ID 1
47#define VOT_STILL_TEXTURE_ID 2
48
49// aspect_ratio_info
50#define EXTENDED_PAR 15
51
52//vol_sprite_usage / sprite_enable
53#define STATIC_SPRITE 1
54#define GMC_SPRITE 2
55
56#define MOTION_MARKER 0x1F001
57#define DC_MARKER     0x6B001
58
59#define VOS_STARTCODE        0x1B0
60#define USER_DATA_STARTCODE  0x1B2
61#define GOP_STARTCODE        0x1B3
62#define VISUAL_OBJ_STARTCODE 0x1B5
63#define VOP_STARTCODE        0x1B6
64#define SLICE_STARTCODE      0x1B7
65#define EXT_STARTCODE        0x1B8
66
67#define QUANT_MATRIX_EXT_ID  0x3
68
69/* smaller packets likely don't contain a real frame */
70#define MAX_NVOP_SIZE 19
71
72void ff_mpeg4_clean_buffers(MpegEncContext *s);
73int ff_mpeg4_get_video_packet_prefix_length(MpegEncContext *s);
74void ff_mpeg4_init_direct_mv(MpegEncContext *s);
75
76/**
77 * @return the mb_type
78 */
79int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my);
80
81#if 0 //3IV1 is quite rare and it slows things down a tiny bit
82#define IS_3IV1 s->codec_tag == AV_RL32("3IV1")
83#else
84#define IS_3IV1 0
85#endif
86
87/**
88 * Predict the dc.
89 * encoding quantized level -> quantized diff
90 * decoding quantized diff -> quantized level
91 * @param n block index (0-3 are luma, 4-5 are chroma)
92 * @param dir_ptr pointer to an integer where the prediction direction will be stored
93 */
94static inline int ff_mpeg4_pred_dc(MpegEncContext *s, int n, int level,
95                                   int *dir_ptr, int encoding)
96{
97    int a, b, c, wrap, pred, scale, ret;
98    int16_t *dc_val;
99
100    /* find prediction */
101    if (n < 4)
102        scale = s->y_dc_scale;
103    else
104        scale = s->c_dc_scale;
105    if (IS_3IV1)
106        scale = 8;
107
108    wrap   = s->block_wrap[n];
109    dc_val = s->dc_val[0] + s->block_index[n];
110
111    /* B C
112     * A X
113     */
114    a = dc_val[-1];
115    b = dc_val[-1 - wrap];
116    c = dc_val[-wrap];
117
118    /* outside slice handling (we can't do that by memset as we need the
119     * dc for error resilience) */
120    if (s->first_slice_line && n != 3) {
121        if (n != 2)
122            b = c = 1024;
123        if (n != 1 && s->mb_x == s->resync_mb_x)
124            b = a = 1024;
125    }
126    if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1) {
127        if (n == 0 || n == 4 || n == 5)
128            b = 1024;
129    }
130
131    if (abs(a - b) < abs(b - c)) {
132        pred     = c;
133        *dir_ptr = 1; /* top */
134    } else {
135        pred     = a;
136        *dir_ptr = 0; /* left */
137    }
138    /* we assume pred is positive */
139    pred = FASTDIV((pred + (scale >> 1)), scale);
140
141    if (encoding) {
142        ret = level - pred;
143    } else {
144        level += pred;
145        ret    = level;
146    }
147    level *= scale;
148    if (level & (~2047)) {
149        if (!s->encoding && (s->avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE))) {
150            if (level < 0) {
151                av_log(s->avctx, AV_LOG_ERROR,
152                       "dc<0 at %dx%d\n", s->mb_x, s->mb_y);
153                return AVERROR_INVALIDDATA;
154            }
155            if (level > 2048 + scale) {
156                av_log(s->avctx, AV_LOG_ERROR,
157                       "dc overflow at %dx%d\n", s->mb_x, s->mb_y);
158                return AVERROR_INVALIDDATA;
159            }
160        }
161        if (level < 0)
162            level = 0;
163        else if (!(s->workaround_bugs & FF_BUG_DC_CLIP))
164            level = 2047;
165    }
166    dc_val[0] = level;
167
168    return ret;
169}
170
171#endif /* AVCODEC_MPEG4VIDEO_H */
172