3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
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.
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.
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
28 #define UNCHECKED_BITSTREAM_READER 1
30 #include "libavutil/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/thread.h"
34 #include "mpegvideo.h"
35 #include "mpeg12codecs.h"
36 #include "mpeg12data.h"
37 #include "mpeg12dec.h"
38 #include "mpegutils.h"
41 static const uint8_t table_mb_ptype
[7][2] = {
42 { 3, 5 }, // 0x01 MB_INTRA
43 { 1, 2 }, // 0x02 MB_PAT
44 { 1, 3 }, // 0x08 MB_FOR
45 { 1, 1 }, // 0x0A MB_FOR|MB_PAT
46 { 1, 6 }, // 0x11 MB_QUANT|MB_INTRA
47 { 1, 5 }, // 0x12 MB_QUANT|MB_PAT
48 { 2, 5 }, // 0x1A MB_QUANT|MB_FOR|MB_PAT
51 static const uint8_t table_mb_btype
[11][2] = {
52 { 3, 5 }, // 0x01 MB_INTRA
53 { 2, 3 }, // 0x04 MB_BACK
54 { 3, 3 }, // 0x06 MB_BACK|MB_PAT
55 { 2, 4 }, // 0x08 MB_FOR
56 { 3, 4 }, // 0x0A MB_FOR|MB_PAT
57 { 2, 2 }, // 0x0C MB_FOR|MB_BACK
58 { 3, 2 }, // 0x0E MB_FOR|MB_BACK|MB_PAT
59 { 1, 6 }, // 0x11 MB_QUANT|MB_INTRA
60 { 2, 6 }, // 0x16 MB_QUANT|MB_BACK|MB_PAT
61 { 3, 6 }, // 0x1A MB_QUANT|MB_FOR|MB_PAT
62 { 2, 5 }, // 0x1E MB_QUANT|MB_FOR|MB_BACK|MB_PAT
65 static const int16_t ptype2mb_type
[7] = {
67 MB_TYPE_FORWARD_MV
| MB_TYPE_CBP
| MB_TYPE_ZERO_MV
| MB_TYPE_16x16
,
69 MB_TYPE_FORWARD_MV
| MB_TYPE_CBP
,
70 MB_TYPE_QUANT
| MB_TYPE_INTRA
,
71 MB_TYPE_QUANT
| MB_TYPE_FORWARD_MV
| MB_TYPE_CBP
| MB_TYPE_ZERO_MV
| MB_TYPE_16x16
,
72 MB_TYPE_QUANT
| MB_TYPE_FORWARD_MV
| MB_TYPE_CBP
,
75 static const int16_t btype2mb_type
[11] = {
78 MB_TYPE_BACKWARD_MV
| MB_TYPE_CBP
,
80 MB_TYPE_FORWARD_MV
| MB_TYPE_CBP
,
82 MB_TYPE_BIDIR_MV
| MB_TYPE_CBP
,
83 MB_TYPE_QUANT
| MB_TYPE_INTRA
,
84 MB_TYPE_QUANT
| MB_TYPE_BACKWARD_MV
| MB_TYPE_CBP
,
85 MB_TYPE_QUANT
| MB_TYPE_FORWARD_MV
| MB_TYPE_CBP
,
86 MB_TYPE_QUANT
| MB_TYPE_BIDIR_MV
| MB_TYPE_CBP
,
89 av_cold
void ff_init_2d_vlc_rl(const uint16_t table_vlc
[][2], RL_VLC_ELEM rl_vlc
[],
90 const int8_t table_run
[], const uint8_t table_level
[],
91 int n
, unsigned static_size
, int flags
)
94 VLCElem table
[680] = { 0 };
95 VLC vlc
= { .table
= table
, .table_allocated
= static_size
};
96 av_assert0(static_size
<= FF_ARRAY_ELEMS(table
));
97 vlc_init(&vlc
, TEX_VLC_BITS
, n
+ 2, &table_vlc
[0][1], 4, 2, &table_vlc
[0][0], 4, 2, VLC_INIT_USE_STATIC
| flags
);
99 for (i
= 0; i
< vlc
.table_size
; i
++) {
100 int code
= vlc
.table
[i
].sym
;
101 int len
= vlc
.table
[i
].len
;
104 if (len
== 0) { // illegal code
107 } else if (len
<0) { //more bits needed
111 if (code
== n
) { //esc
114 } else if (code
== n
+ 1) { //eob
118 run
= table_run
[code
] + 1;
119 level
= table_level
[code
];
123 rl_vlc
[i
].level
= level
;
128 void ff_mpeg1_clean_buffers(MpegEncContext
*s
)
130 s
->last_dc
[0] = 1 << (7 + s
->intra_dc_precision
);
131 s
->last_dc
[1] = s
->last_dc
[0];
132 s
->last_dc
[2] = s
->last_dc
[0];
133 memset(s
->last_mv
, 0, sizeof(s
->last_mv
));
137 /******************************************/
140 VLCElem ff_mv_vlc
[266];
142 VLCElem ff_dc_lum_vlc
[512];
143 VLCElem ff_dc_chroma_vlc
[514];
145 VLCElem ff_mbincr_vlc
[538];
146 VLCElem ff_mb_ptype_vlc
[64];
147 VLCElem ff_mb_btype_vlc
[64];
148 VLCElem ff_mb_pat_vlc
[512];
150 RL_VLC_ELEM ff_mpeg1_rl_vlc
[680];
151 RL_VLC_ELEM ff_mpeg2_rl_vlc
[674];
153 static av_cold
void mpeg12_init_vlcs(void)
155 VLC_INIT_STATIC_TABLE(ff_dc_lum_vlc
, DC_VLC_BITS
, 12,
156 ff_mpeg12_vlc_dc_lum_bits
, 1, 1,
157 ff_mpeg12_vlc_dc_lum_code
, 2, 2, 0);
158 VLC_INIT_STATIC_TABLE(ff_dc_chroma_vlc
, DC_VLC_BITS
, 12,
159 ff_mpeg12_vlc_dc_chroma_bits
, 1, 1,
160 ff_mpeg12_vlc_dc_chroma_code
, 2, 2, 0);
161 VLC_INIT_STATIC_TABLE(ff_mv_vlc
, MV_VLC_BITS
, 17,
162 &ff_mpeg12_mbMotionVectorTable
[0][1], 2, 1,
163 &ff_mpeg12_mbMotionVectorTable
[0][0], 2, 1, 0);
164 VLC_INIT_STATIC_TABLE(ff_mbincr_vlc
, MBINCR_VLC_BITS
, 36,
165 &ff_mpeg12_mbAddrIncrTable
[0][1], 2, 1,
166 &ff_mpeg12_mbAddrIncrTable
[0][0], 2, 1, 0);
167 VLC_INIT_STATIC_TABLE(ff_mb_pat_vlc
, MB_PAT_VLC_BITS
, 64,
168 &ff_mpeg12_mbPatTable
[0][1], 2, 1,
169 &ff_mpeg12_mbPatTable
[0][0], 2, 1, 0);
171 VLC_INIT_STATIC_SPARSE_TABLE(ff_mb_ptype_vlc
, MB_PTYPE_VLC_BITS
, 7,
172 &table_mb_ptype
[0][1], 2, 1,
173 &table_mb_ptype
[0][0], 2, 1,
174 ptype2mb_type
, 2, 2, 0);
175 VLC_INIT_STATIC_SPARSE_TABLE(ff_mb_btype_vlc
, MB_BTYPE_VLC_BITS
, 11,
176 &table_mb_btype
[0][1], 2, 1,
177 &table_mb_btype
[0][0], 2, 1,
178 btype2mb_type
, 2, 2, 0);
180 ff_init_2d_vlc_rl(ff_mpeg1_vlc_table
, ff_mpeg1_rl_vlc
, ff_mpeg12_run
,
181 ff_mpeg12_level
, MPEG12_RL_NB_ELEMS
,
182 FF_ARRAY_ELEMS(ff_mpeg1_rl_vlc
), 0);
183 ff_init_2d_vlc_rl(ff_mpeg2_vlc_table
, ff_mpeg2_rl_vlc
, ff_mpeg12_run
,
184 ff_mpeg12_level
, MPEG12_RL_NB_ELEMS
,
185 FF_ARRAY_ELEMS(ff_mpeg2_rl_vlc
), 0);
188 av_cold
void ff_mpeg12_init_vlcs(void)
190 static AVOnce init_static_once
= AV_ONCE_INIT
;
191 ff_thread_once(&init_static_once
, mpeg12_init_vlcs
);
194 #define MAX_INDEX (64 - 1)
196 int ff_mpeg1_decode_block_intra(GetBitContext
*gb
,
197 const uint16_t *quant_matrix
,
198 const uint8_t *scantable
, int last_dc
[3],
199 int16_t *block
, int index
, int qscale
)
201 int dc
, diff
, i
= 0, component
;
204 component
= index
<= 3 ? 0 : index
- 4 + 1;
206 diff
= decode_dc(gb
, component
);
208 dc
= last_dc
[component
];
210 last_dc
[component
] = dc
;
212 block
[0] = dc
* quant_matrix
[0];
216 UPDATE_CACHE(re
, gb
);
217 if (((int32_t)GET_CACHE(re
, gb
)) <= (int32_t)0xBFFFFFFF)
220 /* now quantify & encode AC coefficients */
224 GET_RL_VLC(level
, run
, re
, gb
, ff_mpeg1_rl_vlc
,
233 level
= (level
* qscale
* quant_matrix
[j
]) >> 4;
234 level
= (level
- 1) | 1;
235 level
= (level
^ SHOW_SBITS(re
, gb
, 1)) -
236 SHOW_SBITS(re
, gb
, 1);
237 SKIP_BITS(re
, gb
, 1);
240 run
= SHOW_UBITS(re
, gb
, 6) + 1;
241 LAST_SKIP_BITS(re
, gb
, 6);
242 UPDATE_CACHE(re
, gb
);
243 level
= SHOW_SBITS(re
, gb
, 8);
244 SKIP_BITS(re
, gb
, 8);
247 level
= SHOW_UBITS(re
, gb
, 8) - 256;
248 SKIP_BITS(re
, gb
, 8);
249 } else if (level
== 0) {
250 level
= SHOW_UBITS(re
, gb
, 8);
251 SKIP_BITS(re
, gb
, 8);
261 level
= (level
* qscale
* quant_matrix
[j
]) >> 4;
262 level
= (level
- 1) | 1;
265 level
= (level
* qscale
* quant_matrix
[j
]) >> 4;
266 level
= (level
- 1) | 1;
271 if (((int32_t)GET_CACHE(re
, gb
)) <= (int32_t)0xBFFFFFFF)
274 UPDATE_CACHE(re
, gb
);
277 LAST_SKIP_BITS(re
, gb
, 2);
278 CLOSE_READER(re
, gb
);
282 i
= AVERROR_INVALIDDATA
;