mpeg12: terminate mpeg2_video_profiles arrays
[libav.git] / libavcodec / mpeg12.c
CommitLineData
de6d9b64 1/*
054480a5 2 * MPEG-1/2 decoder
406792e7 3 * Copyright (c) 2000,2001 Fabrice Bellard
115329f1 4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
de6d9b64 5 *
2912e87a 6 * This file is part of Libav.
b78e7197 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
ff4ec49e
FB
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 12 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
de6d9b64 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
de6d9b64 17 *
ff4ec49e 18 * You should have received a copy of the GNU Lesser General Public
2912e87a 19 * License along with Libav; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 21 */
115329f1 22
983e3246 23/**
ba87f080 24 * @file
054480a5 25 * MPEG-1/2 decoder
983e3246 26 */
115329f1 27
a74127c0 28//#define DEBUG
c269cf68 29#include "internal.h"
de6d9b64
FB
30#include "avcodec.h"
31#include "dsputil.h"
32#include "mpegvideo.h"
33
a6bc5731 34#include "mpeg12.h"
de6d9b64 35#include "mpeg12data.h"
a6bc5731 36#include "mpeg12decdata.h"
92d6b7fd 37#include "bytestream.h"
d37edddc 38#include "vdpau_internal.h"
4440bd0d 39#include "xvmc_internal.h"
de6d9b64 40
bb198e19
MN
41//#undef NDEBUG
42//#include <assert.h>
43
d87c0267 44
4f68b084
MN
45#define MV_VLC_BITS 9
46#define MBINCR_VLC_BITS 9
47#define MB_PAT_VLC_BITS 9
48#define MB_PTYPE_VLC_BITS 6
49#define MB_BTYPE_VLC_BITS 6
4f68b084 50
936bb4a9
RD
51static inline int mpeg1_decode_block_intra(MpegEncContext *s,
52 DCTELEM *block,
53 int n);
115329f1
DB
54static inline int mpeg1_decode_block_inter(MpegEncContext *s,
55 DCTELEM *block,
a0201736 56 int n);
628b210f 57static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n);
115329f1
DB
58static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
59 DCTELEM *block,
de6d9b64 60 int n);
115329f1
DB
61static inline int mpeg2_decode_block_intra(MpegEncContext *s,
62 DCTELEM *block,
de6d9b64 63 int n);
6fc5b059 64static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
dff0f035 65static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
de6d9b64 66static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
57489bc5 67static void exchange_uv(MpegEncContext *s);
de6d9b64 68
7b7abda3 69static const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
530d5740
IK
70 PIX_FMT_XVMC_MPEG2_IDCT,
71 PIX_FMT_XVMC_MPEG2_MC,
eacced45 72 PIX_FMT_NONE};
11ce8834 73
eaa7557c 74uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
3502a54f 75
2d80ce2b
MN
76
77#define INIT_2D_VLC_RL(rl, static_size)\
78{\
79 static RL_VLC_ELEM rl_vlc_table[static_size];\
80 INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
81 &rl.table_vlc[0][1], 4, 2,\
82 &rl.table_vlc[0][0], 4, 2, static_size);\
83\
84 rl.rl_vlc[0]= rl_vlc_table;\
85 init_2d_vlc_rl(&rl);\
86}
87
9ef3193d 88static void init_2d_vlc_rl(RLTable *rl)
4f68b084 89{
07787186 90 int i;
115329f1 91
4f68b084
MN
92 for(i=0; i<rl->vlc.table_size; i++){
93 int code= rl->vlc.table[i][0];
94 int len = rl->vlc.table[i][1];
95 int level, run;
115329f1 96
4f68b084
MN
97 if(len==0){ // illegal code
98 run= 65;
99 level= MAX_LEVEL;
100 }else if(len<0){ //more bits needed
101 run= 0;
102 level= code;
103 }else{
104 if(code==rl->n){ //esc
105 run= 65;
106 level= 0;
107 }else if(code==rl->n+1){ //eob
a0201736
MN
108 run= 0;
109 level= 127;
4f68b084
MN
110 }else{
111 run= rl->table_run [code] + 1;
112 level= rl->table_level[code];
113 }
114 }
115 rl->rl_vlc[0][i].len= len;
116 rl->rl_vlc[0][i].level= level;
117 rl->rl_vlc[0][i].run= run;
118 }
119}
120
eaa7557c 121void ff_mpeg12_common_init(MpegEncContext *s)
de6d9b64 122{
a6b9ffbf 123
8f8402e4 124 s->y_dc_scale_table=
53f66cee 125 s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
8fe2c60e 126
8f8402e4 127}
f004ca1c 128
2a250222
MN
129void 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));
134}
135
de6d9b64
FB
136
137/******************************************/
138/* decoding */
139
6e16398a
VS
140VLC ff_dc_lum_vlc;
141VLC ff_dc_chroma_vlc;
142
de6d9b64
FB
143static VLC mv_vlc;
144static VLC mbincr_vlc;
145static VLC mb_ptype_vlc;
146static VLC mb_btype_vlc;
147static VLC mb_pat_vlc;
148
071083b4 149av_cold void ff_mpeg12_init_vlcs(void)
de6d9b64
FB
150{
151 static int done = 0;
152
153 if (!done) {
915bbac6 154 done = 1;
de6d9b64 155
6e16398a 156 INIT_VLC_STATIC(&ff_dc_lum_vlc, DC_VLC_BITS, 12,
eaa7557c 157 ff_mpeg12_vlc_dc_lum_bits, 1, 1,
f76605e8 158 ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
6e16398a 159 INIT_VLC_STATIC(&ff_dc_chroma_vlc, DC_VLC_BITS, 12,
eaa7557c 160 ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
f76605e8
MN
161 ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
162 INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 17,
eaa7557c 163 &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
f76605e8
MN
164 &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
165 INIT_VLC_STATIC(&mbincr_vlc, MBINCR_VLC_BITS, 36,
eaa7557c 166 &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
f76605e8
MN
167 &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
168 INIT_VLC_STATIC(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
eaa7557c 169 &ff_mpeg12_mbPatTable[0][1], 2, 1,
f76605e8 170 &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
115329f1 171
f76605e8 172 INIT_VLC_STATIC(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
de6d9b64 173 &table_mb_ptype[0][1], 2, 1,
f76605e8
MN
174 &table_mb_ptype[0][0], 2, 1, 64);
175 INIT_VLC_STATIC(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
de6d9b64 176 &table_mb_btype[0][1], 2, 1,
f76605e8 177 &table_mb_btype[0][0], 2, 1, 64);
eaa7557c
AJ
178 init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
179 init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
4f68b084 180
2d80ce2b
MN
181 INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
182 INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
de6d9b64
FB
183 }
184}
185
186static inline int get_dmv(MpegEncContext *s)
187{
115329f1 188 if(get_bits1(&s->gb))
612476ef 189 return 1 - (get_bits1(&s->gb) << 1);
de6d9b64
FB
190 else
191 return 0;
192}
193
0ee50938
FB
194static inline int get_qscale(MpegEncContext *s)
195{
7062fad6 196 int qscale = get_bits(&s->gb, 5);
51929fd3
MN
197 if (s->q_scale_type) {
198 return non_linear_qscale[qscale];
199 } else {
200 return qscale << 1;
0ee50938 201 }
0ee50938
FB
202}
203
054480a5 204/* motion type (for MPEG-2) */
de6d9b64
FB
205#define MT_FIELD 1
206#define MT_FRAME 2
207#define MT_16X8 2
208#define MT_DMV 3
209
210static int mpeg_decode_mb(MpegEncContext *s,
5e5c247a 211 DCTELEM block[12][64])
de6d9b64 212{
d2975f8d 213 int i, j, k, cbp, val, mb_type, motion_type;
a622dc43 214 const int mb_block_count = 4 + (1<< s->chroma_format);
715731a3 215
dfd2a005 216 av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
de6d9b64 217
160d679c 218 assert(s->mb_skipped==0);
f943e138 219
9b8709d1 220 if (s->mb_skip_run-- != 0) {
9701840b 221 if (s->pict_type == FF_P_TYPE) {
160d679c 222 s->mb_skipped = 1;
7bc9090a 223 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
de6d9b64 224 } else {
71434945 225 int mb_type;
115329f1 226
71434945
MN
227 if(s->mb_x)
228 mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
229 else
054480a5 230 mb_type= s->current_picture.mb_type[ s->mb_width + (s->mb_y-1)*s->mb_stride - 1]; // FIXME not sure if this is allowed in MPEG at all
71434945
MN
231 if(IS_INTRA(mb_type))
232 return -1;
115329f1 233
115329f1 234 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
71434945 235 mb_type | MB_TYPE_SKIP;
7bc9090a
MN
236// assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
237
115329f1 238 if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
160d679c 239 s->mb_skipped = 1;
de6d9b64 240 }
59b571c1 241
de6d9b64
FB
242 return 0;
243 }
244
245 switch(s->pict_type) {
246 default:
9701840b 247 case FF_I_TYPE:
612476ef 248 if (get_bits1(&s->gb) == 0) {
e94bc100 249 if (get_bits1(&s->gb) == 0){
9b879566 250 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
de6d9b64 251 return -1;
e94bc100 252 }
7bc9090a 253 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
de6d9b64 254 } else {
7bc9090a 255 mb_type = MB_TYPE_INTRA;
de6d9b64
FB
256 }
257 break;
9701840b 258 case FF_P_TYPE:
8ed2ddb2 259 mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
9ac7ecd6 260 if (mb_type < 0){
9b879566 261 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
de6d9b64 262 return -1;
9ac7ecd6 263 }
7bc9090a 264 mb_type = ptype2mb_type[ mb_type ];
de6d9b64 265 break;
9701840b 266 case FF_B_TYPE:
8ed2ddb2 267 mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
9ac7ecd6 268 if (mb_type < 0){
9b879566 269 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
de6d9b64 270 return -1;
9ac7ecd6 271 }
7bc9090a 272 mb_type = btype2mb_type[ mb_type ];
de6d9b64
FB
273 break;
274 }
dfd2a005 275 av_dlog(s->avctx, "mb_type=%x\n", mb_type);
7bc9090a
MN
276// motion_type = 0; /* avoid warning */
277 if (IS_INTRA(mb_type)) {
2de4ba7f 278 s->dsp.clear_blocks(s->block[0]);
115329f1 279
2de4ba7f
MN
280 if(!s->chroma_y_shift){
281 s->dsp.clear_blocks(s->block[6]);
282 }
115329f1 283
054480a5
DB
284 /* compute DCT type */
285 if (s->picture_structure == PICT_FRAME && //FIXME add an interlaced_dct coded var?
7bc9090a
MN
286 !s->frame_pred_frame_dct) {
287 s->interlaced_dct = get_bits1(&s->gb);
288 }
de6d9b64 289
7bc9090a
MN
290 if (IS_QUANT(mb_type))
291 s->qscale = get_qscale(s);
115329f1 292
de6d9b64
FB
293 if (s->concealment_motion_vectors) {
294 /* just parse them */
115329f1 295 if (s->picture_structure != PICT_FRAME)
612476ef 296 skip_bits1(&s->gb); /* field select */
115329f1
DB
297
298 s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
daab3296 299 mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
115329f1 300 s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
daab3296
MN
301 mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
302
7bc9090a 303 skip_bits1(&s->gb); /* marker */
daab3296
MN
304 }else
305 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
de6d9b64 306 s->mb_intra = 1;
054480a5 307 //if 1, we memcpy blocks in xvmcvideo
ce0e60a1 308 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1){
78f9a878 309 ff_xvmc_pack_pblocks(s,-1);//inter are always full blocks
a579db0c
IK
310 if(s->swap_uv){
311 exchange_uv(s);
312 }
313 }
7bc9090a 314
029911d1 315 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
dff0f035
MN
316 if(s->flags2 & CODEC_FLAG2_FAST){
317 for(i=0;i<6;i++) {
21effaa4 318 mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
dff0f035
MN
319 }
320 }else{
321 for(i=0;i<mb_block_count;i++) {
21effaa4 322 if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
dff0f035
MN
323 return -1;
324 }
7bc9090a
MN
325 }
326 } else {
327 for(i=0;i<6;i++) {
db2f2093 328 if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
7bc9090a
MN
329 return -1;
330 }
331 }
de6d9b64 332 } else {
7bc9090a 333 if (mb_type & MB_TYPE_ZERO_MV){
b40cd4e0 334 assert(mb_type & MB_TYPE_CBP);
7bc9090a 335
7bc9090a 336 s->mv_dir = MV_DIR_FORWARD;
fecc146b
MN
337 if(s->picture_structure == PICT_FRAME){
338 if(!s->frame_pred_frame_dct)
339 s->interlaced_dct = get_bits1(&s->gb);
4c9544d0 340 s->mv_type = MV_TYPE_16X16;
fecc146b 341 }else{
4c9544d0
MN
342 s->mv_type = MV_TYPE_FIELD;
343 mb_type |= MB_TYPE_INTERLACED;
344 s->field_select[0][0]= s->picture_structure - 1;
345 }
fecc146b
MN
346
347 if (IS_QUANT(mb_type))
348 s->qscale = get_qscale(s);
349
7bc9090a
MN
350 s->last_mv[0][0][0] = 0;
351 s->last_mv[0][0][1] = 0;
352 s->last_mv[0][1][0] = 0;
353 s->last_mv[0][1][1] = 0;
354 s->mv[0][0][0] = 0;
355 s->mv[0][0][1] = 0;
356 }else{
357 assert(mb_type & MB_TYPE_L0L1);
358//FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
e42dba48 359 /* get additional motion vector type */
115329f1 360 if (s->frame_pred_frame_dct)
7bc9090a
MN
361 motion_type = MT_FRAME;
362 else{
363 motion_type = get_bits(&s->gb, 2);
0ac6b5a3
MN
364 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
365 s->interlaced_dct = get_bits1(&s->gb);
7bc9090a
MN
366 }
367
368 if (IS_QUANT(mb_type))
369 s->qscale = get_qscale(s);
370
371 /* motion vectors */
037df60e 372 s->mv_dir= (mb_type>>13)&3;
dfd2a005 373 av_dlog(s->avctx, "motion_type=%d\n", motion_type);
356ab1da
MN
374 switch(motion_type) {
375 case MT_FRAME: /* or MT_16X8 */
f4fbcd33
MN
376 if (s->picture_structure == PICT_FRAME) {
377 mb_type |= MB_TYPE_16x16;
378 s->mv_type = MV_TYPE_16X16;
379 for(i=0;i<2;i++) {
380 if (USES_LIST(mb_type, i)) {
7bc9090a 381 /* MT_FRAME */
115329f1 382 s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
7bc9090a 383 mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
115329f1 384 s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
7bc9090a 385 mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
054480a5 386 /* full_pel: only for MPEG-1 */
7bc9090a
MN
387 if (s->full_pel[i]){
388 s->mv[i][0][0] <<= 1;
389 s->mv[i][0][1] <<= 1;
390 }
f4fbcd33
MN
391 }
392 }
393 } else {
394 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
395 s->mv_type = MV_TYPE_16X8;
396 for(i=0;i<2;i++) {
397 if (USES_LIST(mb_type, i)) {
7bc9090a 398 /* MT_16X8 */
7bc9090a
MN
399 for(j=0;j<2;j++) {
400 s->field_select[i][j] = get_bits1(&s->gb);
401 for(k=0;k<2;k++) {
402 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
403 s->last_mv[i][j][k]);
404 s->last_mv[i][j][k] = val;
405 s->mv[i][j][k] = val;
406 }
407 }
de6d9b64 408 }
356ab1da
MN
409 }
410 }
411 break;
412 case MT_FIELD:
413 s->mv_type = MV_TYPE_FIELD;
04b502fa
MN
414 if (s->picture_structure == PICT_FRAME) {
415 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
416 for(i=0;i<2;i++) {
417 if (USES_LIST(mb_type, i)) {
7bc9090a
MN
418 for(j=0;j<2;j++) {
419 s->field_select[i][j] = get_bits1(&s->gb);
420 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
421 s->last_mv[i][j][0]);
422 s->last_mv[i][j][0] = val;
423 s->mv[i][j][0] = val;
dfd2a005 424 av_dlog(s->avctx, "fmx=%d\n", val);
7bc9090a
MN
425 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
426 s->last_mv[i][j][1] >> 1);
427 s->last_mv[i][j][1] = val << 1;
428 s->mv[i][j][1] = val;
dfd2a005 429 av_dlog(s->avctx, "fmy=%d\n", val);
7bc9090a 430 }
04b502fa
MN
431 }
432 }
433 } else {
434 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
435 for(i=0;i<2;i++) {
436 if (USES_LIST(mb_type, i)) {
7bc9090a 437 s->field_select[i][0] = get_bits1(&s->gb);
de6d9b64
FB
438 for(k=0;k<2;k++) {
439 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
7bc9090a
MN
440 s->last_mv[i][0][k]);
441 s->last_mv[i][0][k] = val;
442 s->last_mv[i][1][k] = val;
443 s->mv[i][0][k] = val;
de6d9b64
FB
444 }
445 }
356ab1da
MN
446 }
447 }
448 break;
449 case MT_DMV:
450 s->mv_type = MV_TYPE_DMV;
451 for(i=0;i<2;i++) {
452 if (USES_LIST(mb_type, i)) {
453 int dmx, dmy, mx, my, m;
f0ec2394
MN
454 const int my_shift= s->picture_structure == PICT_FRAME;
455
356ab1da
MN
456 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
457 s->last_mv[i][0][0]);
458 s->last_mv[i][0][0] = mx;
459 s->last_mv[i][1][0] = mx;
460 dmx = get_dmv(s);
461 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
f0ec2394 462 s->last_mv[i][0][1] >> my_shift);
356ab1da
MN
463 dmy = get_dmv(s);
464
465
f0ec2394
MN
466 s->last_mv[i][0][1] = my<<my_shift;
467 s->last_mv[i][1][1] = my<<my_shift;
356ab1da
MN
468
469 s->mv[i][0][0] = mx;
470 s->mv[i][0][1] = my;
471 s->mv[i][1][0] = mx;//not used
472 s->mv[i][1][1] = my;//not used
473
474 if (s->picture_structure == PICT_FRAME) {
475 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
476
477 //m = 1 + 2 * s->top_field_first;
478 m = s->top_field_first ? 1 : 3;
479
480 /* top -> top pred */
481 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
482 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
483 m = 4 - m;
484 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
485 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
486 } else {
487 mb_type |= MB_TYPE_16x16;
488
489 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
490 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
491 if(s->picture_structure == PICT_TOP_FIELD)
492 s->mv[i][2][1]--;
493 else
494 s->mv[i][2][1]++;
de6d9b64
FB
495 }
496 }
de6d9b64 497 }
356ab1da
MN
498 break;
499 default:
500 av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
501 return -1;
de6d9b64
FB
502 }
503 }
115329f1 504
7bc9090a 505 s->mb_intra = 0;
b40cd4e0 506 if (HAS_CBP(mb_type)) {
2de4ba7f 507 s->dsp.clear_blocks(s->block[0]);
115329f1 508
7bc9090a 509 cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
715731a3 510 if(mb_block_count > 6){
bb270c08
DB
511 cbp<<= mb_block_count-6;
512 cbp |= get_bits(&s->gb, mb_block_count-6);
2952d13a 513 s->dsp.clear_blocks(s->block[6]);
5e5c247a 514 }
09d1bee8
MN
515 if (cbp <= 0){
516 av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
517 return -1;
518 }
7bc9090a 519
054480a5 520 //if 1, we memcpy blocks in xvmcvideo
ce0e60a1 521 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1){
78f9a878 522 ff_xvmc_pack_pblocks(s,cbp);
a579db0c
IK
523 if(s->swap_uv){
524 exchange_uv(s);
525 }
115329f1 526 }
a579db0c 527
029911d1 528 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
6fc5b059
MN
529 if(s->flags2 & CODEC_FLAG2_FAST){
530 for(i=0;i<6;i++) {
531 if(cbp & 32) {
21effaa4 532 mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
6fc5b059
MN
533 } else {
534 s->block_last_index[i] = -1;
535 }
536 cbp+=cbp;
537 }
538 }else{
539 cbp<<= 12-mb_block_count;
115329f1 540
6fc5b059
MN
541 for(i=0;i<mb_block_count;i++) {
542 if ( cbp & (1<<11) ) {
21effaa4 543 if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
6fc5b059
MN
544 return -1;
545 } else {
546 s->block_last_index[i] = -1;
547 }
548 cbp+=cbp;
7bc9090a 549 }
de6d9b64 550 }
7bc9090a 551 } else {
628b210f
MN
552 if(s->flags2 & CODEC_FLAG2_FAST){
553 for(i=0;i<6;i++) {
554 if (cbp & 32) {
21effaa4 555 mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
628b210f
MN
556 } else {
557 s->block_last_index[i] = -1;
558 }
559 cbp+=cbp;
560 }
561 }else{
562 for(i=0;i<6;i++) {
563 if (cbp & 32) {
21effaa4 564 if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
628b210f
MN
565 return -1;
566 } else {
567 s->block_last_index[i] = -1;
568 }
569 cbp+=cbp;
7bc9090a 570 }
a0201736 571 }
de6d9b64 572 }
7bc9090a 573 }else{
31a78b71 574 for(i=0;i<12;i++)
7bc9090a 575 s->block_last_index[i] = -1;
de6d9b64
FB
576 }
577 }
7bc9090a
MN
578
579 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
580
de6d9b64
FB
581 return 0;
582}
583
054480a5 584/* as H.263, but only 17 codes */
de6d9b64
FB
585static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
586{
05858889 587 int code, sign, val, l, shift;
de6d9b64 588
8ed2ddb2 589 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
de6d9b64
FB
590 if (code == 0) {
591 return pred;
592 }
7bc9090a
MN
593 if (code < 0) {
594 return 0xffff;
595 }
596
612476ef 597 sign = get_bits1(&s->gb);
de6d9b64 598 shift = fcode - 1;
05858889
B
599 val = code;
600 if (shift) {
601 val = (val - 1) << shift;
de6d9b64 602 val |= get_bits(&s->gb, shift);
05858889
B
603 val++;
604 }
de6d9b64
FB
605 if (sign)
606 val = -val;
607 val += pred;
115329f1 608
de6d9b64 609 /* modulo decoding */
0ff93477
MN
610 l= INT_BIT - 5 - shift;
611 val = (val<<l)>>l;
de6d9b64
FB
612 return val;
613}
614
db2f2093 615static inline int mpeg1_decode_block_intra(MpegEncContext *s,
115329f1 616 DCTELEM *block,
de6d9b64
FB
617 int n)
618{
619 int level, dc, diff, i, j, run;
a0201736 620 int component;
eaa7557c 621 RLTable *rl = &ff_rl_mpeg1;
0c1a9eda
ZK
622 uint8_t * const scantable= s->intra_scantable.permutated;
623 const uint16_t *quant_matrix= s->intra_matrix;
a0201736 624 const int qscale= s->qscale;
de6d9b64 625
054480a5 626 /* DC coefficient */
a0201736 627 component = (n <= 3 ? 0 : n - 4 + 1);
c3bf0288 628 diff = decode_dc(&s->gb, component);
a0201736
MN
629 if (diff >= 0xffff)
630 return -1;
631 dc = s->last_dc[component];
632 dc += diff;
633 s->last_dc[component] = dc;
44ba8b65 634 block[0] = dc*quant_matrix[0];
dfd2a005 635 av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
a0201736
MN
636 i = 0;
637 {
115329f1 638 OPEN_READER(re, &s->gb);
054480a5 639 /* now quantify & encode AC coefficients */
a0201736
MN
640 for(;;) {
641 UPDATE_CACHE(re, &s->gb);
e91f4bf1 642 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
115329f1 643
a0201736
MN
644 if(level == 127){
645 break;
646 } else if(level != 0) {
647 i += run;
648 j = scantable[i];
51929fd3 649 level= (level*qscale*quant_matrix[j])>>4;
a0201736
MN
650 level= (level-1)|1;
651 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
652 LAST_SKIP_BITS(re, &s->gb, 1);
653 } else {
654 /* escape */
655 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
656 UPDATE_CACHE(re, &s->gb);
657 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
658 if (level == -128) {
659 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
660 } else if (level == 0) {
661 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
662 }
663 i += run;
664 j = scantable[i];
665 if(level<0){
666 level= -level;
51929fd3 667 level= (level*qscale*quant_matrix[j])>>4;
a0201736
MN
668 level= (level-1)|1;
669 level= -level;
670 }else{
51929fd3 671 level= (level*qscale*quant_matrix[j])>>4;
a0201736
MN
672 level= (level-1)|1;
673 }
674 }
675 if (i > 63){
9b879566 676 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
a0201736
MN
677 return -1;
678 }
679
680 block[j] = level;
681 }
682 CLOSE_READER(re, &s->gb);
683 }
684 s->block_last_index[n] = i;
685 return 0;
686}
687
db2f2093
MR
688int ff_mpeg1_decode_block_intra(MpegEncContext *s,
689 DCTELEM *block,
690 int n)
691{
692 return mpeg1_decode_block_intra(s, block, n);
693}
694
115329f1
DB
695static inline int mpeg1_decode_block_inter(MpegEncContext *s,
696 DCTELEM *block,
a0201736
MN
697 int n)
698{
699 int level, i, j, run;
eaa7557c 700 RLTable *rl = &ff_rl_mpeg1;
0c1a9eda
ZK
701 uint8_t * const scantable= s->intra_scantable.permutated;
702 const uint16_t *quant_matrix= s->inter_matrix;
a0201736
MN
703 const int qscale= s->qscale;
704
705 {
8db1a1dd 706 OPEN_READER(re, &s->gb);
a0201736 707 i = -1;
054480a5 708 // special case for first coefficient, no need to add second VLC table
8db1a1dd 709 UPDATE_CACHE(re, &s->gb);
29df2599 710 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
51929fd3 711 level= (3*qscale*quant_matrix[0])>>5;
a0201736 712 level= (level-1)|1;
29df2599 713 if(GET_CACHE(re, &s->gb)&0x40000000)
a0201736 714 level= -level;
3729c912 715 block[0] = level;
a0201736 716 i++;
29df2599
MN
717 SKIP_BITS(re, &s->gb, 2);
718 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
719 goto end;
de6d9b64 720 }
054480a5 721 /* now quantify & encode AC coefficients */
a0201736 722 for(;;) {
e91f4bf1 723 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
115329f1 724
29df2599 725 if(level != 0) {
a0201736
MN
726 i += run;
727 j = scantable[i];
51929fd3 728 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
a0201736
MN
729 level= (level-1)|1;
730 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
29df2599 731 SKIP_BITS(re, &s->gb, 1);
a0201736
MN
732 } else {
733 /* escape */
734 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
735 UPDATE_CACHE(re, &s->gb);
736 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
737 if (level == -128) {
29df2599 738 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
a0201736 739 } else if (level == 0) {
29df2599 740 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
a0201736
MN
741 }
742 i += run;
743 j = scantable[i];
744 if(level<0){
745 level= -level;
51929fd3 746 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
a0201736
MN
747 level= (level-1)|1;
748 level= -level;
749 }else{
51929fd3 750 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
a0201736
MN
751 level= (level-1)|1;
752 }
de6d9b64 753 }
a0201736 754 if (i > 63){
9b879566 755 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
a0201736
MN
756 return -1;
757 }
758
759 block[j] = level;
29df2599
MN
760 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
761 break;
762 UPDATE_CACHE(re, &s->gb);
de6d9b64 763 }
29df2599
MN
764end:
765 LAST_SKIP_BITS(re, &s->gb, 2);
a0201736 766 CLOSE_READER(re, &s->gb);
de6d9b64 767 }
a0201736 768 s->block_last_index[n] = i;
de6d9b64
FB
769 return 0;
770}
771
628b210f
MN
772static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
773{
774 int level, i, j, run;
eaa7557c 775 RLTable *rl = &ff_rl_mpeg1;
628b210f
MN
776 uint8_t * const scantable= s->intra_scantable.permutated;
777 const int qscale= s->qscale;
778
779 {
628b210f
MN
780 OPEN_READER(re, &s->gb);
781 i = -1;
054480a5 782 // special case for first coefficient, no need to add second VLC table
628b210f 783 UPDATE_CACHE(re, &s->gb);
29df2599 784 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
3cb01872 785 level= (3*qscale)>>1;
628b210f 786 level= (level-1)|1;
29df2599 787 if(GET_CACHE(re, &s->gb)&0x40000000)
628b210f
MN
788 level= -level;
789 block[0] = level;
790 i++;
29df2599
MN
791 SKIP_BITS(re, &s->gb, 2);
792 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
793 goto end;
628b210f
MN
794 }
795
054480a5 796 /* now quantify & encode AC coefficients */
628b210f 797 for(;;) {
e91f4bf1 798 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
115329f1 799
29df2599 800 if(level != 0) {
628b210f
MN
801 i += run;
802 j = scantable[i];
803 level= ((level*2+1)*qscale)>>1;
804 level= (level-1)|1;
805 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
29df2599 806 SKIP_BITS(re, &s->gb, 1);
628b210f
MN
807 } else {
808 /* escape */
809 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
810 UPDATE_CACHE(re, &s->gb);
811 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
812 if (level == -128) {
29df2599 813 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
628b210f 814 } else if (level == 0) {
29df2599 815 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
628b210f
MN
816 }
817 i += run;
818 j = scantable[i];
819 if(level<0){
820 level= -level;
821 level= ((level*2+1)*qscale)>>1;
822 level= (level-1)|1;
823 level= -level;
824 }else{
825 level= ((level*2+1)*qscale)>>1;
826 level= (level-1)|1;
827 }
828 }
829
830 block[j] = level;
29df2599
MN
831 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
832 break;
833 UPDATE_CACHE(re, &s->gb);
628b210f 834 }
29df2599
MN
835end:
836 LAST_SKIP_BITS(re, &s->gb, 2);
628b210f
MN
837 CLOSE_READER(re, &s->gb);
838 }
839 s->block_last_index[n] = i;
840 return 0;
841}
842
843
115329f1
DB
844static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
845 DCTELEM *block,
3729c912 846 int n)
de6d9b64
FB
847{
848 int level, i, j, run;
eaa7557c 849 RLTable *rl = &ff_rl_mpeg1;
0c1a9eda
ZK
850 uint8_t * const scantable= s->intra_scantable.permutated;
851 const uint16_t *quant_matrix;
3729c912 852 const int qscale= s->qscale;
de6d9b64
FB
853 int mismatch;
854
de6d9b64
FB
855 mismatch = 1;
856
857 {
8db1a1dd 858 OPEN_READER(re, &s->gb);
3729c912 859 i = -1;
8db1a1dd 860 if (n < 4)
3729c912 861 quant_matrix = s->inter_matrix;
de6d9b64 862 else
3729c912 863 quant_matrix = s->chroma_inter_matrix;
8db1a1dd 864
054480a5 865 // special case for first coefficient, no need to add second VLC table
8db1a1dd 866 UPDATE_CACHE(re, &s->gb);
29df2599 867 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
3729c912 868 level= (3*qscale*quant_matrix[0])>>5;
29df2599 869 if(GET_CACHE(re, &s->gb)&0x40000000)
3729c912
MN
870 level= -level;
871 block[0] = level;
872 mismatch ^= level;
873 i++;
29df2599
MN
874 SKIP_BITS(re, &s->gb, 2);
875 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
876 goto end;
de6d9b64 877 }
de6d9b64 878
054480a5 879 /* now quantify & encode AC coefficients */
3729c912 880 for(;;) {
e91f4bf1 881 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
115329f1 882
29df2599 883 if(level != 0) {
3729c912
MN
884 i += run;
885 j = scantable[i];
886 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
887 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
29df2599 888 SKIP_BITS(re, &s->gb, 1);
3729c912
MN
889 } else {
890 /* escape */
891 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
892 UPDATE_CACHE(re, &s->gb);
893 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
894
895 i += run;
896 j = scantable[i];
897 if(level<0){
898 level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
899 level= -level;
900 }else{
901 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
902 }
903 }
904 if (i > 63){
9b879566 905 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
3729c912
MN
906 return -1;
907 }
115329f1 908
3729c912
MN
909 mismatch ^= level;
910 block[j] = level;
29df2599
MN
911 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
912 break;
913 UPDATE_CACHE(re, &s->gb);
25ed7f92 914 }
29df2599
MN
915end:
916 LAST_SKIP_BITS(re, &s->gb, 2);
3729c912 917 CLOSE_READER(re, &s->gb);
de6d9b64
FB
918 }
919 block[63] ^= (mismatch & 1);
115329f1 920
de6d9b64
FB
921 s->block_last_index[n] = i;
922 return 0;
923}
924
115329f1
DB
925static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
926 DCTELEM *block,
6fc5b059
MN
927 int n)
928{
929 int level, i, j, run;
eaa7557c 930 RLTable *rl = &ff_rl_mpeg1;
6fc5b059
MN
931 uint8_t * const scantable= s->intra_scantable.permutated;
932 const int qscale= s->qscale;
6fc5b059
MN
933 OPEN_READER(re, &s->gb);
934 i = -1;
935
054480a5 936 // special case for first coefficient, no need to add second VLC table
6fc5b059 937 UPDATE_CACHE(re, &s->gb);
29df2599 938 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
6fc5b059 939 level= (3*qscale)>>1;
29df2599 940 if(GET_CACHE(re, &s->gb)&0x40000000)
6fc5b059
MN
941 level= -level;
942 block[0] = level;
943 i++;
29df2599
MN
944 SKIP_BITS(re, &s->gb, 2);
945 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
946 goto end;
6fc5b059
MN
947 }
948
054480a5 949 /* now quantify & encode AC coefficients */
6fc5b059 950 for(;;) {
e91f4bf1 951 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
115329f1 952
29df2599 953 if(level != 0) {
6fc5b059
MN
954 i += run;
955 j = scantable[i];
956 level= ((level*2+1)*qscale)>>1;
957 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
29df2599 958 SKIP_BITS(re, &s->gb, 1);
6fc5b059
MN
959 } else {
960 /* escape */
961 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
962 UPDATE_CACHE(re, &s->gb);
963 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
964
965 i += run;
966 j = scantable[i];
967 if(level<0){
968 level= ((-level*2+1)*qscale)>>1;
969 level= -level;
970 }else{
971 level= ((level*2+1)*qscale)>>1;
972 }
973 }
115329f1 974
6fc5b059 975 block[j] = level;
29df2599
MN
976 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
977 break;
978 UPDATE_CACHE(re, &s->gb);
6fc5b059 979 }
29df2599 980end:
115329f1 981 LAST_SKIP_BITS(re, &s->gb, 2);
6fc5b059
MN
982 CLOSE_READER(re, &s->gb);
983 s->block_last_index[n] = i;
984 return 0;
985}
986
987
115329f1
DB
988static inline int mpeg2_decode_block_intra(MpegEncContext *s,
989 DCTELEM *block,
3729c912 990 int n)
de6d9b64
FB
991{
992 int level, dc, diff, i, j, run;
3729c912 993 int component;
de6d9b64 994 RLTable *rl;
0c1a9eda
ZK
995 uint8_t * const scantable= s->intra_scantable.permutated;
996 const uint16_t *quant_matrix;
3729c912 997 const int qscale= s->qscale;
de6d9b64
FB
998 int mismatch;
999
054480a5 1000 /* DC coefficient */
3729c912
MN
1001 if (n < 4){
1002 quant_matrix = s->intra_matrix;
115329f1 1003 component = 0;
3729c912
MN
1004 }else{
1005 quant_matrix = s->chroma_intra_matrix;
5e5c247a 1006 component = (n&1) + 1;
3729c912 1007 }
c3bf0288 1008 diff = decode_dc(&s->gb, component);
de6d9b64
FB
1009 if (diff >= 0xffff)
1010 return -1;
1011 dc = s->last_dc[component];
1012 dc += diff;
1013 s->last_dc[component] = dc;
1014 block[0] = dc << (3 - s->intra_dc_precision);
dfd2a005 1015 av_dlog(s->avctx, "dc=%d\n", block[0]);
d753173a 1016 mismatch = block[0] ^ 1;
3729c912 1017 i = 0;
de6d9b64 1018 if (s->intra_vlc_format)
eaa7557c 1019 rl = &ff_rl_mpeg2;
de6d9b64 1020 else
eaa7557c 1021 rl = &ff_rl_mpeg1;
25ed7f92 1022
3729c912 1023 {
115329f1 1024 OPEN_READER(re, &s->gb);
054480a5 1025 /* now quantify & encode AC coefficients */
3729c912
MN
1026 for(;;) {
1027 UPDATE_CACHE(re, &s->gb);
e91f4bf1 1028 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
115329f1 1029
3729c912
MN
1030 if(level == 127){
1031 break;
1032 } else if(level != 0) {
1033 i += run;
1034 j = scantable[i];
1035 level= (level*qscale*quant_matrix[j])>>4;
1036 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1037 LAST_SKIP_BITS(re, &s->gb, 1);
1038 } else {
1039 /* escape */
1040 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1041 UPDATE_CACHE(re, &s->gb);
1042 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1043 i += run;
1044 j = scantable[i];
1045 if(level<0){
1046 level= (-level*qscale*quant_matrix[j])>>4;
1047 level= -level;
1048 }else{
1049 level= (level*qscale*quant_matrix[j])>>4;
1050 }
1051 }
1052 if (i > 63){
9b879566 1053 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
3729c912
MN
1054 return -1;
1055 }
115329f1 1056
3729c912
MN
1057 mismatch^= level;
1058 block[j] = level;
9ac7ecd6 1059 }
3729c912 1060 CLOSE_READER(re, &s->gb);
de6d9b64 1061 }
3729c912 1062 block[63]^= mismatch&1;
115329f1 1063
de6d9b64
FB
1064 s->block_last_index[n] = i;
1065 return 0;
1066}
1067
115329f1
DB
1068static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
1069 DCTELEM *block,
dff0f035
MN
1070 int n)
1071{
1072 int level, dc, diff, j, run;
1073 int component;
1074 RLTable *rl;
1075 uint8_t * scantable= s->intra_scantable.permutated;
1076 const uint16_t *quant_matrix;
1077 const int qscale= s->qscale;
1078
054480a5 1079 /* DC coefficient */
dff0f035
MN
1080 if (n < 4){
1081 quant_matrix = s->intra_matrix;
115329f1 1082 component = 0;
dff0f035
MN
1083 }else{
1084 quant_matrix = s->chroma_intra_matrix;
1085 component = (n&1) + 1;
1086 }
1087 diff = decode_dc(&s->gb, component);
1088 if (diff >= 0xffff)
1089 return -1;
1090 dc = s->last_dc[component];
1091 dc += diff;
1092 s->last_dc[component] = dc;
1093 block[0] = dc << (3 - s->intra_dc_precision);
1094 if (s->intra_vlc_format)
eaa7557c 1095 rl = &ff_rl_mpeg2;
dff0f035 1096 else
eaa7557c 1097 rl = &ff_rl_mpeg1;
dff0f035
MN
1098
1099 {
115329f1 1100 OPEN_READER(re, &s->gb);
054480a5 1101 /* now quantify & encode AC coefficients */
dff0f035
MN
1102 for(;;) {
1103 UPDATE_CACHE(re, &s->gb);
1104 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
115329f1 1105
dff0f035
MN
1106 if(level == 127){
1107 break;
1108 } else if(level != 0) {
1109 scantable += run;
1110 j = *scantable;
1111 level= (level*qscale*quant_matrix[j])>>4;
1112 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1113 LAST_SKIP_BITS(re, &s->gb, 1);
1114 } else {
1115 /* escape */
1116 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1117 UPDATE_CACHE(re, &s->gb);
1118 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1119 scantable += run;
1120 j = *scantable;
1121 if(level<0){
1122 level= (-level*qscale*quant_matrix[j])>>4;
1123 level= -level;
1124 }else{
1125 level= (level*qscale*quant_matrix[j])>>4;
1126 }
1127 }
115329f1 1128
dff0f035
MN
1129 block[j] = level;
1130 }
1131 CLOSE_READER(re, &s->gb);
1132 }
115329f1 1133
dff0f035
MN
1134 s->block_last_index[n] = scantable - s->intra_scantable.permutated;
1135 return 0;
1136}
1137
de6d9b64
FB
1138typedef struct Mpeg1Context {
1139 MpegEncContext mpeg_enc_ctx;
de6d9b64 1140 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
1cb0edb4 1141 int repeat_field; /* true if we must repeat the field */
a20df858 1142 AVPanScan pan_scan; /**< some temporary storage for the panscan */
c62c07d3 1143 int slice_count;
a6b9ffbf
IK
1144 int swap_uv;//indicate VCR2
1145 int save_aspect_info;
0127b861 1146 int save_width, save_height, save_progressive_seq;
c32b14bb 1147 AVRational frame_rate_ext; ///< MPEG-2 specific framerate modificator
ab8c48de 1148 int sync; ///< Did we reach a sync point like a GOP/SEQ/KEYFrame?
de6d9b64
FB
1149} Mpeg1Context;
1150
98a6fff9 1151static av_cold int mpeg_decode_init(AVCodecContext *avctx)
de6d9b64
FB
1152{
1153 Mpeg1Context *s = avctx->priv_data;
3edcacde 1154 MpegEncContext *s2 = &s->mpeg_enc_ctx;
d1700ead 1155 int i;
115329f1 1156
054480a5
DB
1157 /* we need some permutation to store matrices,
1158 * until MPV_common_init() sets the real permutation. */
d1700ead
IK
1159 for(i=0;i<64;i++)
1160 s2->dsp.idct_permutation[i]=i;
1161
3edcacde 1162 MPV_decode_defaults(s2);
115329f1 1163
303aebf9 1164 s->mpeg_enc_ctx.avctx= avctx;
94aec31f 1165 s->mpeg_enc_ctx.flags= avctx->flags;
303e50e6 1166 s->mpeg_enc_ctx.flags2= avctx->flags2;
eaa7557c 1167 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
071083b4 1168 ff_mpeg12_init_vlcs();
de6d9b64 1169
de6d9b64 1170 s->mpeg_enc_ctx_allocated = 0;
de6d9b64 1171 s->mpeg_enc_ctx.picture_number = 0;
1cb0edb4 1172 s->repeat_field = 0;
d7e9533a 1173 s->mpeg_enc_ctx.codec_id= avctx->codec->id;
9c24cd72 1174 avctx->color_range= AVCOL_RANGE_MPEG;
580a7465
DC
1175 if (avctx->codec->id == CODEC_ID_MPEG1VIDEO)
1176 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1177 else
1178 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
de6d9b64
FB
1179 return 0;
1180}
1181
115329f1 1182static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
a6b9ffbf 1183 const uint8_t *new_perm){
c32b14bb
MN
1184 uint16_t temp_matrix[64];
1185 int i;
a6b9ffbf
IK
1186
1187 memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
115329f1 1188
a6b9ffbf
IK
1189 for(i=0;i<64;i++){
1190 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
115329f1 1191 }
a6b9ffbf
IK
1192}
1193
448ecb68 1194static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx){
93c69a94
CEH
1195 Mpeg1Context *s1 = avctx->priv_data;
1196 MpegEncContext *s = &s1->mpeg_enc_ctx;
1197
1198 if(avctx->xvmc_acceleration)
1199 return avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
d37edddc
NC
1200 else if(avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
1201 if(avctx->codec_id == CODEC_ID_MPEG1VIDEO)
1202 return PIX_FMT_VDPAU_MPEG1;
1203 else
1204 return PIX_FMT_VDPAU_MPEG2;
1205 }else{
93c69a94 1206 if(s->chroma_format < 2)
6aca2c67 1207 return avctx->get_format(avctx,ff_hwaccel_pixfmt_list_420);
93c69a94
CEH
1208 else if(s->chroma_format == 2)
1209 return PIX_FMT_YUV422P;
1210 else
1211 return PIX_FMT_YUV444P;
1212 }
1213}
1214
054480a5
DB
1215/* Call this function when we know all parameters.
1216 * It may be called in different places for MPEG-1 and MPEG-2. */
a6b9ffbf 1217static int mpeg_decode_postinit(AVCodecContext *avctx){
c32b14bb
MN
1218 Mpeg1Context *s1 = avctx->priv_data;
1219 MpegEncContext *s = &s1->mpeg_enc_ctx;
1220 uint8_t old_permutation[64];
a6b9ffbf
IK
1221
1222 if (
bb270c08 1223 (s1->mpeg_enc_ctx_allocated == 0)||
21adafec
MN
1224 avctx->coded_width != s->width ||
1225 avctx->coded_height != s->height||
29644cb5
HL
1226 s1->save_width != s->width ||
1227 s1->save_height != s->height ||
aa26aa78 1228 s1->save_aspect_info != s->aspect_ratio_info||
0127b861 1229 s1->save_progressive_seq != s->progressive_sequence ||
a6b9ffbf
IK
1230 0)
1231 {
115329f1 1232
a6b9ffbf 1233 if (s1->mpeg_enc_ctx_allocated) {
89ba9eed
MN
1234 ParseContext pc= s->parse_context;
1235 s->parse_context.buffer=0;
a6b9ffbf 1236 MPV_common_end(s);
89ba9eed 1237 s->parse_context= pc;
a6b9ffbf
IK
1238 }
1239
bb270c08
DB
1240 if( (s->width == 0 )||(s->height == 0))
1241 return -2;
a6b9ffbf 1242
21adafec 1243 avcodec_set_dimensions(avctx, s->width, s->height);
a6b9ffbf
IK
1244 avctx->bit_rate = s->bit_rate;
1245 s1->save_aspect_info = s->aspect_ratio_info;
29644cb5
HL
1246 s1->save_width = s->width;
1247 s1->save_height = s->height;
0127b861 1248 s1->save_progressive_seq = s->progressive_sequence;
a6b9ffbf 1249
054480a5
DB
1250 /* low_delay may be forced, in this case we will have B-frames
1251 * that behave like P-frames. */
a6b9ffbf
IK
1252 avctx->has_b_frames = !(s->low_delay);
1253
062e7c3c
MN
1254 assert((avctx->sub_id==1) == (avctx->codec_id==CODEC_ID_MPEG1VIDEO));
1255 if(avctx->codec_id==CODEC_ID_MPEG1VIDEO){
054480a5 1256 //MPEG-1 fps
761089b0
SG
1257 avctx->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num;
1258 avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den;
054480a5 1259 //MPEG-1 aspect
a6b9ffbf 1260 avctx->sample_aspect_ratio= av_d2q(
eaa7557c 1261 1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
3797c74b 1262 avctx->ticks_per_frame=1;
054480a5
DB
1263 }else{//MPEG-2
1264 //MPEG-2 fps
a6b9ffbf 1265 av_reduce(
115329f1
DB
1266 &s->avctx->time_base.den,
1267 &s->avctx->time_base.num,
edbd72ac 1268 ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
761089b0 1269 ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
a6b9ffbf 1270 1<<30);
3797c74b 1271 avctx->ticks_per_frame=2;
054480a5 1272 //MPEG-2 aspect
a6b9ffbf 1273 if(s->aspect_ratio_info > 1){
395206f6
MN
1274 //we ignore the spec here as reality does not match the spec, see for example
1275 // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
1276 if( (s1->pan_scan.width == 0 )||(s1->pan_scan.height == 0) || 1){
115329f1 1277 s->avctx->sample_aspect_ratio=
5ac47683 1278 av_div_q(
eaa7557c 1279 ff_mpeg2_aspect[s->aspect_ratio_info],
5ac47683
IK
1280 (AVRational){s->width, s->height}
1281 );
a6b9ffbf 1282 }else{
115329f1 1283 s->avctx->sample_aspect_ratio=
a6b9ffbf 1284 av_div_q(
eaa7557c 1285 ff_mpeg2_aspect[s->aspect_ratio_info],
6fe1a1a5 1286 (AVRational){s1->pan_scan.width, s1->pan_scan.height}
a6b9ffbf 1287 );
bb270c08 1288 }
a6b9ffbf 1289 }else{
115329f1 1290 s->avctx->sample_aspect_ratio=
eaa7557c 1291 ff_mpeg2_aspect[s->aspect_ratio_info];
a6b9ffbf 1292 }
054480a5 1293 }//MPEG-2
a6b9ffbf 1294
448ecb68 1295 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
a05aa821 1296 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
a6b9ffbf 1297 //until then pix_fmt may be changed right after codec init
d37edddc 1298 if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ||
c269cf68 1299 avctx->hwaccel ||
d37edddc 1300 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU )
a6b9ffbf
IK
1301 if( avctx->idct_algo == FF_IDCT_AUTO )
1302 avctx->idct_algo = FF_IDCT_SIMPLE;
1303
054480a5
DB
1304 /* Quantization matrices may need reordering
1305 * if DCT permutation is changed. */
a6b9ffbf
IK
1306 memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
1307
1308 if (MPV_common_init(s) < 0)
1309 return -2;
1310
1311 quant_matrix_rebuild(s->intra_matrix, old_permutation,s->dsp.idct_permutation);
1312 quant_matrix_rebuild(s->inter_matrix, old_permutation,s->dsp.idct_permutation);
1313 quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
1314 quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
1315
1316 s1->mpeg_enc_ctx_allocated = 1;
1317 }
1318 return 0;
1319}
1320
115329f1 1321static int mpeg1_decode_picture(AVCodecContext *avctx,
5f0f7713 1322 const uint8_t *buf, int buf_size)
de6d9b64
FB
1323{
1324 Mpeg1Context *s1 = avctx->priv_data;
1325 MpegEncContext *s = &s1->mpeg_enc_ctx;
d60a8f85 1326 int ref, f_code, vbv_delay;
de6d9b64 1327
68f593b4 1328 init_get_bits(&s->gb, buf, buf_size*8);
de6d9b64
FB
1329
1330 ref = get_bits(&s->gb, 10); /* temporal ref */
1331 s->pict_type = get_bits(&s->gb, 3);
71434945
MN
1332 if(s->pict_type == 0 || s->pict_type > 3)
1333 return -1;
58bfafbe 1334
d60a8f85 1335 vbv_delay= get_bits(&s->gb, 16);
9701840b 1336 if (s->pict_type == FF_P_TYPE || s->pict_type == FF_B_TYPE) {
612476ef 1337 s->full_pel[0] = get_bits1(&s->gb);
de6d9b64 1338 f_code = get_bits(&s->gb, 3);
047599a4 1339 if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
de6d9b64
FB
1340 return -1;
1341 s->mpeg_f_code[0][0] = f_code;
1342 s->mpeg_f_code[0][1] = f_code;
1343 }
9701840b 1344 if (s->pict_type == FF_B_TYPE) {
612476ef 1345 s->full_pel[1] = get_bits1(&s->gb);
de6d9b64 1346 f_code = get_bits(&s->gb, 3);
047599a4 1347 if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
de6d9b64
FB
1348 return -1;
1349 s->mpeg_f_code[1][0] = f_code;
1350 s->mpeg_f_code[1][1] = f_code;
1351 }
1e491e29 1352 s->current_picture.pict_type= s->pict_type;
9701840b 1353 s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
115329f1 1354
71434945
MN
1355 if(avctx->debug & FF_DEBUG_PICT_INFO)
1356 av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
115329f1 1357
de6d9b64
FB
1358 s->y_dc_scale = 8;
1359 s->c_dc_scale = 8;
de6d9b64
FB
1360 return 0;
1361}
1362
c32b14bb 1363static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
de6d9b64 1364{
c32b14bb 1365 MpegEncContext *s= &s1->mpeg_enc_ctx;
de6d9b64 1366 int horiz_size_ext, vert_size_ext;
baaf3f46 1367 int bit_rate_ext;
de6d9b64 1368
054480a5 1369 skip_bits(&s->gb, 1); /* profile and level esc*/
baced9f5
MN
1370 s->avctx->profile= get_bits(&s->gb, 3);
1371 s->avctx->level= get_bits(&s->gb, 4);
1cb0edb4 1372 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
5e5c247a 1373 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
de6d9b64
FB
1374 horiz_size_ext = get_bits(&s->gb, 2);
1375 vert_size_ext = get_bits(&s->gb, 2);
1376 s->width |= (horiz_size_ext << 12);
1377 s->height |= (vert_size_ext << 12);
1378 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
27ef18d1 1379 s->bit_rate += (bit_rate_ext << 18) * 400;
612476ef 1380 skip_bits1(&s->gb); /* marker */
0368c72d 1381 s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
e0560448 1382
945f15b7 1383 s->low_delay = get_bits1(&s->gb);
e0560448
MN
1384 if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
1385
c32b14bb
MN
1386 s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
1387 s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
14bea432 1388
dfd2a005 1389 av_dlog(s->avctx, "sequence extension\n");
029911d1 1390 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
054480a5 1391 s->avctx->sub_id = 2; /* indicates MPEG-2 found */
945f15b7 1392
029911d1 1393 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
115329f1 1394 av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
baced9f5 1395 s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
a6b9ffbf 1396
de6d9b64
FB
1397}
1398
fa384dcc
MN
1399static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1400{
1401 MpegEncContext *s= &s1->mpeg_enc_ctx;
1402 int color_description, w, h;
1403
1404 skip_bits(&s->gb, 3); /* video format */
1405 color_description= get_bits1(&s->gb);
1406 if(color_description){
9c24cd72
MN
1407 s->avctx->color_primaries= get_bits(&s->gb, 8);
1408 s->avctx->color_trc = get_bits(&s->gb, 8);
1409 s->avctx->colorspace = get_bits(&s->gb, 8);
fa384dcc
MN
1410 }
1411 w= get_bits(&s->gb, 14);
1412 skip_bits(&s->gb, 1); //marker
1413 h= get_bits(&s->gb, 14);
0db2d3cf 1414 // remaining 3 bits are zero padding
115329f1 1415
fa384dcc
MN
1416 s1->pan_scan.width= 16*w;
1417 s1->pan_scan.height=16*h;
115329f1 1418
fa384dcc 1419 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
9b879566 1420 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
fa384dcc
MN
1421}
1422
1423static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1424{
1425 MpegEncContext *s= &s1->mpeg_enc_ctx;
6fe1a1a5
IK
1426 int i,nofco;
1427
1428 nofco = 1;
1429 if(s->progressive_sequence){
1430 if(s->repeat_first_field){
bb270c08
DB
1431 nofco++;
1432 if(s->top_field_first)
1433 nofco++;
1434 }
6fe1a1a5
IK
1435 }else{
1436 if(s->picture_structure == PICT_FRAME){
1437 nofco++;
bb270c08
DB
1438 if(s->repeat_first_field)
1439 nofco++;
1440 }
6fe1a1a5
IK
1441 }
1442 for(i=0; i<nofco; i++){
fa384dcc
MN
1443 s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
1444 skip_bits(&s->gb, 1); //marker
1445 s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
1446 skip_bits(&s->gb, 1); //marker
1447 }
115329f1 1448
fa384dcc 1449 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
115329f1
DB
1450 av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
1451 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1452 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
fa384dcc
MN
1453 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
1454 );
1455}
1456
45ccc61a
MN
1457static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra){
1458 int i;
1459
1460 for(i=0; i<64; i++) {
1461 int j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1462 int v = get_bits(&s->gb, 8);
1463 if(v==0){
1464 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1465 return -1;
1466 }
71412781
MN
1467 if(intra && i==0 && v!=8){
1468 av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
1469 v= 8; // needed by pink.mpg / issue1046
1470 }
45ccc61a
MN
1471 matrix0[j] = v;
1472 if(matrix1)
1473 matrix1[j] = v;
1474 }
1475 return 0;
1476}
1477
de6d9b64
FB
1478static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1479{
dfd2a005 1480 av_dlog(s->avctx, "matrix extension\n");
25ed7f92 1481
45ccc61a
MN
1482 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1483 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1484 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL , 1);
1485 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL , 0);
de6d9b64
FB
1486}
1487
ed16f91f 1488static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
de6d9b64 1489{
ed16f91f
MN
1490 MpegEncContext *s= &s1->mpeg_enc_ctx;
1491
de6d9b64
FB
1492 s->full_pel[0] = s->full_pel[1] = 0;
1493 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1494 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1495 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1496 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
ed16f91f
MN
1497 if(!s->pict_type && s1->mpeg_enc_ctx_allocated){
1498 av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
1499 if(s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1]==15){
1500 if(s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1501 s->pict_type= FF_I_TYPE;
1502 else
1503 s->pict_type= FF_P_TYPE;
1504 }else
1505 s->pict_type= FF_B_TYPE;
1506 s->current_picture.pict_type= s->pict_type;
1507 s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
ed16f91f 1508 }
de6d9b64
FB
1509 s->intra_dc_precision = get_bits(&s->gb, 2);
1510 s->picture_structure = get_bits(&s->gb, 2);
612476ef
A
1511 s->top_field_first = get_bits1(&s->gb);
1512 s->frame_pred_frame_dct = get_bits1(&s->gb);
1513 s->concealment_motion_vectors = get_bits1(&s->gb);
1514 s->q_scale_type = get_bits1(&s->gb);
1515 s->intra_vlc_format = get_bits1(&s->gb);
1516 s->alternate_scan = get_bits1(&s->gb);
1517 s->repeat_first_field = get_bits1(&s->gb);
1518 s->chroma_420_type = get_bits1(&s->gb);
1519 s->progressive_frame = get_bits1(&s->gb);
bb198e19 1520
aeaef4ed 1521 if(s->progressive_sequence && !s->progressive_frame){
87f68060 1522 s->progressive_frame= 1;
aeaef4ed
MN
1523 av_log(s->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
1524 }
1525
1526 if(s->picture_structure==0 || (s->progressive_frame && s->picture_structure!=PICT_FRAME)){
1527 av_log(s->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s->picture_structure);
87f68060 1528 s->picture_structure= PICT_FRAME;
aeaef4ed
MN
1529 }
1530
7a14430e 1531 if(s->progressive_sequence && !s->frame_pred_frame_dct){
aeaef4ed 1532 av_log(s->avctx, AV_LOG_ERROR, "invalid frame_pred_frame_dct\n");
87f68060
MN
1533 s->frame_pred_frame_dct= 1;
1534 }
1535
5d48f0cb 1536 if(s->picture_structure == PICT_FRAME){
dfb476cb 1537 s->first_field=0;
5d48f0cb
MN
1538 s->v_edge_pos= 16*s->mb_height;
1539 }else{
dfb476cb 1540 s->first_field ^= 1;
5d48f0cb 1541 s->v_edge_pos= 8*s->mb_height;
7bc9090a 1542 memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
dfb476cb 1543 }
115329f1 1544
acf44abb 1545 if(s->alternate_scan){
3d2e8cce
MN
1546 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
1547 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
acf44abb 1548 }else{
3d2e8cce
MN
1549 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
1550 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
acf44abb 1551 }
115329f1 1552
de6d9b64 1553 /* composite display not parsed */
dfd2a005
LB
1554 av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1555 av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1556 av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1557 av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1558 av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1559 av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1560 av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1561 av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1562 av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
de6d9b64
FB
1563}
1564
a579db0c 1565static void exchange_uv(MpegEncContext *s){
21effaa4
IK
1566 DCTELEM (*tmp)[64];
1567
1568 tmp = s->pblocks[4];
a579db0c
IK
1569 s->pblocks[4] = s->pblocks[5];
1570 s->pblocks[5] = tmp;
ff862be5
MN
1571}
1572
765e94ef 1573static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size){
c62c07d3
MN
1574 AVCodecContext *avctx= s->avctx;
1575 Mpeg1Context *s1 = (Mpeg1Context*)s;
de6d9b64 1576
de6d9b64 1577 /* start frame decoding */
c62c07d3 1578 if(s->first_field || s->picture_structure==PICT_FRAME){
d6db1c9c 1579 if(MPV_frame_start(s, avctx) < 0)
c62c07d3 1580 return -1;
7bc9090a
MN
1581
1582 ff_er_frame_start(s);
1583
2ec23b6d 1584 /* first check if we must repeat the frame */
9ee2c20e 1585 s->current_picture_ptr->repeat_pict = 0;
2ec23b6d
MN
1586 if (s->repeat_first_field) {
1587 if (s->progressive_sequence) {
1588 if (s->top_field_first)
9ee2c20e 1589 s->current_picture_ptr->repeat_pict = 4;
2ec23b6d 1590 else
9ee2c20e 1591 s->current_picture_ptr->repeat_pict = 2;
2ec23b6d 1592 } else if (s->progressive_frame) {
9ee2c20e 1593 s->current_picture_ptr->repeat_pict = 1;
2ec23b6d 1594 }
115329f1 1595 }
fa384dcc
MN
1596
1597 *s->current_picture_ptr->pan_scan= s1->pan_scan;
c62c07d3 1598 }else{ //second field
b536d0aa 1599 int i;
115329f1 1600
37b787f1 1601 if(!s->current_picture_ptr){
9b879566 1602 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
37b787f1
MN
1603 return -1;
1604 }
115329f1 1605
b536d0aa
MN
1606 for(i=0; i<4; i++){
1607 s->current_picture.data[i] = s->current_picture_ptr->data[i];
1608 if(s->picture_structure == PICT_BOTTOM_FIELD){
1609 s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
115329f1 1610 }
b536d0aa 1611 }
c62c07d3 1612 }
765e94ef
MN
1613
1614 if (avctx->hwaccel) {
1615 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
1616 return -1;
1617 }
1618
2e7b4c84
IK
1619// MPV_frame_start will call this function too,
1620// but we need to call it on every field
ce0e60a1 1621 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
4acfdcab 1622 if(ff_xvmc_field_start(s,avctx) < 0)
9e494ab7 1623 return -1;
de6d9b64 1624
c62c07d3
MN
1625 return 0;
1626}
1627
1628#define DECODE_SLICE_ERROR -1
1629#define DECODE_SLICE_OK 0
1630
1631/**
1632 * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
1633 * @return DECODE_SLICE_ERROR if the slice is damaged<br>
1634 * DECODE_SLICE_OK if this slice is ok<br>
1635 */
1636static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
5f0f7713 1637 const uint8_t **buf, int buf_size)
c62c07d3
MN
1638{
1639 MpegEncContext *s = &s1->mpeg_enc_ctx;
1640 AVCodecContext *avctx= s->avctx;
c62c07d3 1641 const int field_pic= s->picture_structure != PICT_FRAME;
178fcca8 1642 const int lowres= s->avctx->lowres;
c62c07d3
MN
1643
1644 s->resync_mb_x=
1645 s->resync_mb_y= -1;
1646
52069c4d 1647 assert(mb_y < s->mb_height);
115329f1 1648
80097bbf 1649 init_get_bits(&s->gb, *buf, buf_size*8);
de6d9b64 1650
c62c07d3
MN
1651 ff_mpeg1_clean_buffers(s);
1652 s->interlaced_dct = 0;
1653
0ee50938 1654 s->qscale = get_qscale(s);
a4337a51 1655
7bc9090a 1656 if(s->qscale == 0){
9b879566 1657 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
db6e7795 1658 return -1;
7bc9090a 1659 }
115329f1 1660
de6d9b64 1661 /* extra slice info */
612476ef
A
1662 while (get_bits1(&s->gb) != 0) {
1663 skip_bits(&s->gb, 8);
de6d9b64 1664 }
115329f1 1665
ce5b7c5e 1666 s->mb_x=0;
7bc9090a 1667
483aad71
MN
1668 if(mb_y==0 && s->codec_tag == AV_RL32("SLIF")){
1669 skip_bits1(&s->gb);
1670 }else{
6e4f40e1
CEH
1671 for(;;) {
1672 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1673 if (code < 0){
1674 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1675 return -1;
1676 }
1677 if (code >= 33) {
1678 if (code == 33) {
1679 s->mb_x += 33;
1680 }
1681 /* otherwise, stuffing, nothing to do */
1682 } else {
1683 s->mb_x += code;
1684 break;
ce5b7c5e 1685 }
ce5b7c5e
MN
1686 }
1687 }
483aad71 1688
960964f5
MN
1689 if(s->mb_x >= (unsigned)s->mb_width){
1690 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1691 return -1;
1692 }
c62c07d3 1693
25450bb2
GB
1694 if (avctx->hwaccel) {
1695 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1696 int start_code = -1;
1697 buf_end = ff_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1698 if (buf_end < *buf + buf_size)
1699 buf_end -= 4;
1700 s->mb_y = mb_y;
1701 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1702 return DECODE_SLICE_ERROR;
1703 *buf = buf_end;
1704 return DECODE_SLICE_OK;
1705 }
1706
7bc9090a 1707 s->resync_mb_x= s->mb_x;
c62c07d3 1708 s->resync_mb_y= s->mb_y= mb_y;
9b8709d1 1709 s->mb_skip_run= 0;
7d1c3fc1 1710 ff_init_block_index(s);
ce5b7c5e 1711
c62c07d3
MN
1712 if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
1713 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
115329f1 1714 av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%2d%2d%2d%2d %s %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
c62c07d3 1715 s->qscale, s->mpeg_f_code[0][0],s->mpeg_f_code[0][1],s->mpeg_f_code[1][0],s->mpeg_f_code[1][1],
9701840b 1716 s->pict_type == FF_I_TYPE ? "I" : (s->pict_type == FF_P_TYPE ? "P" : (s->pict_type == FF_B_TYPE ? "B" : "S")),
115329f1 1717 s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
c62c07d3
MN
1718 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
1719 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
1720 }
115329f1
DB
1721 }
1722
de6d9b64 1723 for(;;) {
054480a5 1724 //If 1, we memcpy blocks in xvmcvideo.
ce0e60a1 1725 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
78f9a878 1726 ff_xvmc_init_block(s);//set s->block
a579db0c 1727
4152c5ce 1728 if(mpeg_decode_mb(s, s->block) < 0)
de6d9b64 1729 return -1;
8d7ec294 1730
31b1ec5d 1731 if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
078cdecf 1732 const int wrap = s->b8_stride;
137c8468 1733 int xy = s->mb_x*2 + s->mb_y*2*wrap;
fc4a2d1e 1734 int b8_xy= 4*(s->mb_x + s->mb_y*s->mb_stride);
4c9544d0 1735 int motion_x, motion_y, dir, i;
31b1ec5d 1736
50c93f74
MN
1737 for(i=0; i<2; i++){
1738 for(dir=0; dir<2; dir++){
9701840b 1739 if (s->mb_intra || (dir==1 && s->pict_type != FF_B_TYPE)) {
4c9544d0 1740 motion_x = motion_y = 0;
0c352cad 1741 }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
4c9544d0
MN
1742 motion_x = s->mv[dir][0][0];
1743 motion_y = s->mv[dir][0][1];
1744 } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
1745 motion_x = s->mv[dir][i][0];
1746 motion_y = s->mv[dir][i][1];
1747 }
7c4f71c4 1748
4c9544d0
MN
1749 s->current_picture.motion_val[dir][xy ][0] = motion_x;
1750 s->current_picture.motion_val[dir][xy ][1] = motion_y;
1751 s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
1752 s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
fc4a2d1e
MN
1753 s->current_picture.ref_index [dir][b8_xy ]=
1754 s->current_picture.ref_index [dir][b8_xy + 1]= s->field_select[dir][i];
4ad8ecd1 1755 assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
4c9544d0 1756 }
50c93f74 1757 xy += wrap;
fc4a2d1e 1758 b8_xy +=2;
7bc9090a 1759 }
7bc9090a 1760 }
8d7ec294 1761
178fcca8 1762 s->dest[0] += 16 >> lowres;
7ceab4af
MN
1763 s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
1764 s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
ce5b7c5e 1765
7d1c3fc1 1766 MPV_decode_mb(s, s->block);
115329f1 1767
ce5b7c5e 1768 if (++s->mb_x >= s->mb_width) {
af2a9e8f 1769 const int mb_size= 16>>s->avctx->lowres;
ff862be5 1770
078cdecf 1771 ff_draw_horiz_band(s, mb_size*(s->mb_y>>field_pic), mb_size);
ce5b7c5e
MN
1772
1773 s->mb_x = 0;
078cdecf 1774 s->mb_y += 1<<field_pic;
cf713bb8 1775
078cdecf 1776 if(s->mb_y >= s->mb_height){
6e44ba15 1777 int left= get_bits_left(&s->gb);
9701840b 1778 int is_d10= s->chroma_format==2 && s->pict_type==FF_I_TYPE && avctx->profile==0 && avctx->level==5
7923a3f9
MN
1779 && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
1780 && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
cf713bb8 1781
7923a3f9 1782 if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
047599a4 1783 || (avctx->error_recognition >= FF_ER_AGGRESSIVE && left>8)){
7923a3f9 1784 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
cf713bb8
MN
1785 return -1;
1786 }else
1787 goto eos;
1788 }
115329f1 1789
7d1c3fc1 1790 ff_init_block_index(s);
ce5b7c5e 1791 }
ce5b7c5e
MN
1792
1793 /* skip mb handling */
9b8709d1 1794 if (s->mb_skip_run == -1) {
054480a5 1795 /* read increment again */
9b8709d1 1796 s->mb_skip_run = 0;
ce5b7c5e
MN
1797 for(;;) {
1798 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
7bc9090a 1799 if (code < 0){
9b879566 1800 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
9c00c3af 1801 return -1;
7bc9090a 1802 }
ce5b7c5e
MN
1803 if (code >= 33) {
1804 if (code == 33) {
9b8709d1 1805 s->mb_skip_run += 33;
9c00c3af
MN
1806 }else if(code == 35){
1807 if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
9b879566 1808 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
9c00c3af
MN
1809 return -1;
1810 }
1811 goto eos; /* end of slice */
ce5b7c5e
MN
1812 }
1813 /* otherwise, stuffing, nothing to do */
1814 } else {
9b8709d1 1815 s->mb_skip_run += code;
ce5b7c5e
MN
1816 break;
1817 }
1818 }
01e795ab
MN
1819 if(s->mb_skip_run){
1820 int i;
9701840b 1821 if(s->pict_type == FF_I_TYPE){
01e795ab
MN
1822 av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1823 return -1;
1824 }
1825
1826 /* skip mb */
1827 s->mb_intra = 0;
1828 for(i=0;i<12;i++)
1829 s->block_last_index[i] = -1;
1830 if(s->picture_structure == PICT_FRAME)
1831 s->mv_type = MV_TYPE_16X16;
1832 else
1833 s->mv_type = MV_TYPE_FIELD;
9701840b 1834 if (s->pict_type == FF_P_TYPE) {
01e795ab
MN
1835 /* if P type, zero motion vector is implied */
1836 s->mv_dir = MV_DIR_FORWARD;
1837 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1838 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1839 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
9d130166 1840 s->field_select[0][0]= (s->picture_structure - 1) & 1;
01e795ab
MN
1841 } else {
1842 /* if B type, reuse previous vectors and directions */
1843 s->mv[0][0][0] = s->last_mv[0][0][0];
1844 s->mv[0][0][1] = s->last_mv[0][0][1];
1845 s->mv[1][0][0] = s->last_mv[1][0][0];
1846 s->mv[1][0][1] = s->last_mv[1][0][1];
1847 }
1848 }
ce5b7c5e 1849 }
de6d9b64 1850 }
db6e7795 1851eos: // end of slice
05218081 1852 *buf += (get_bits_count(&s->gb)-1)/8;
7bc9090a 1853//printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
db6e7795
MN
1854 return 0;
1855}
7bc9090a 1856
c62c07d3 1857static int slice_decode_thread(AVCodecContext *c, void *arg){
3a84713a 1858 MpegEncContext *s= *(void**)arg;
49a37411 1859 const uint8_t *buf= s->gb.buffer;
c62c07d3 1860 int mb_y= s->start_mb_y;
078cdecf 1861 const int field_pic= s->picture_structure != PICT_FRAME;
c62c07d3 1862
078cdecf 1863 s->error_count= (3*(s->end_mb_y - s->start_mb_y)*s->mb_width) >> field_pic;
c62c07d3
MN
1864
1865 for(;;){
191e8ca7
MR
1866 uint32_t start_code;
1867 int ret;
c62c07d3
MN
1868
1869 ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
1870 emms_c();
115329f1 1871//av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
c62c07d3
MN
1872//ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, s->start_mb_y, s->end_mb_y, s->error_count);
1873 if(ret < 0){
1874 if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
1875 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
1876 }else{
1877 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
1878 }
115329f1 1879
c62c07d3
MN
1880 if(s->mb_y == s->end_mb_y)
1881 return 0;
115329f1 1882
82fcbc14
MN
1883 start_code= -1;
1884 buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
f7114249
RB
1885 mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
1886 if (s->picture_structure == PICT_BOTTOM_FIELD)
1887 mb_y++;
c62c07d3
MN
1888 if(mb_y < 0 || mb_y >= s->end_mb_y)
1889 return -1;
1890 }
115329f1 1891
c62c07d3
MN
1892 return 0; //not reached
1893}
1894
db6e7795 1895/**
49bd8e4b 1896 * Handle slice ends.
6be8b204 1897 * @return 1 if it seems to be the last slice
db6e7795
MN
1898 */
1899static int slice_end(AVCodecContext *avctx, AVFrame *pict)
1900{
1901 Mpeg1Context *s1 = avctx->priv_data;
1902 MpegEncContext *s = &s1->mpeg_enc_ctx;
115329f1 1903
36b58e85 1904 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
855127bc
FB
1905 return 0;
1906
6aca2c67
GB
1907 if (s->avctx->hwaccel) {
1908 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1909 av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
1910 }
1911
ce0e60a1 1912 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
78f9a878 1913 ff_xvmc_field_end(s);
ce0e60a1 1914
de6d9b64 1915 /* end of slice reached */
db6e7795 1916 if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
de6d9b64 1917 /* end of image */
1e491e29 1918
51929fd3 1919 s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
0426af31 1920
7bc9090a 1921 ff_er_frame_end(s);
de6d9b64
FB
1922
1923 MPV_frame_end(s);
1924
9701840b 1925 if (s->pict_type == FF_B_TYPE || s->low_delay) {
8e937a4a 1926 *pict= *(AVFrame*)s->current_picture_ptr;
0c9bbaec 1927 ff_print_debug_info(s, pict);
de6d9b64 1928 } else {
1e491e29 1929 s->picture_number++;
054480a5 1930 /* latency of 1 frame for I- and P-frames */
de6d9b64 1931 /* XXX: use another variable than picture_number */
80097bbf 1932 if (s->last_picture_ptr != NULL) {
8e937a4a 1933 *pict= *(AVFrame*)s->last_picture_ptr;
0c9bbaec 1934 ff_print_debug_info(s, pict);
de6d9b64 1935 }
de6d9b64 1936 }
6be8b204
BC
1937
1938 return 1;
1939 } else {
1940 return 0;
de6d9b64
FB
1941 }
1942}
1943
115329f1 1944static int mpeg1_decode_sequence(AVCodecContext *avctx,
5f0f7713 1945 const uint8_t *buf, int buf_size)
de6d9b64
FB
1946{
1947 Mpeg1Context *s1 = avctx->priv_data;
1948 MpegEncContext *s = &s1->mpeg_enc_ctx;
5ac47683 1949 int width,height;
a6b9ffbf 1950 int i, v, j;
fb4a4a56 1951
68f593b4 1952 init_get_bits(&s->gb, buf, buf_size*8);
de6d9b64 1953
5ac47683
IK
1954 width = get_bits(&s->gb, 12);
1955 height = get_bits(&s->gb, 12);
16e30b7a 1956 if (width <= 0 || height <= 0)
5ac47683 1957 return -1;
945f15b7 1958 s->aspect_ratio_info= get_bits(&s->gb, 4);
99c763d1
BC
1959 if (s->aspect_ratio_info == 0) {
1960 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
047599a4 1961 if (avctx->error_recognition >= FF_ER_COMPLIANT)
99c763d1
BC
1962 return -1;
1963 }
de6d9b64 1964 s->frame_rate_index = get_bits(&s->gb, 4);
8d52ec7e 1965 if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
de6d9b64
FB
1966 return -1;
1967 s->bit_rate = get_bits(&s->gb, 18) * 400;
612476ef 1968 if (get_bits1(&s->gb) == 0) /* marker */
de6d9b64 1969 return -1;
5ac47683
IK
1970 s->width = width;
1971 s->height = height;
de6d9b64 1972
0368c72d 1973 s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
612476ef 1974 skip_bits(&s->gb, 1);
de6d9b64
FB
1975
1976 /* get matrix */
612476ef 1977 if (get_bits1(&s->gb)) {
45ccc61a 1978 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
de6d9b64
FB
1979 } else {
1980 for(i=0;i<64;i++) {
a6b9ffbf 1981 j = s->dsp.idct_permutation[i];
adc09b2e 1982 v = ff_mpeg1_default_intra_matrix[i];
2ad1516a
MN
1983 s->intra_matrix[j] = v;
1984 s->chroma_intra_matrix[j] = v;
de6d9b64
FB
1985 }
1986 }
612476ef 1987 if (get_bits1(&s->gb)) {
45ccc61a 1988 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
de6d9b64
FB
1989 } else {
1990 for(i=0;i<64;i++) {
b0368839 1991 int j= s->dsp.idct_permutation[i];
adc09b2e 1992 v = ff_mpeg1_default_non_intra_matrix[i];
2ad1516a
MN
1993 s->inter_matrix[j] = v;
1994 s->chroma_inter_matrix[j] = v;
de6d9b64
FB
1995 }
1996 }
115329f1 1997
e6dc9c6f
MN
1998 if(show_bits(&s->gb, 23) != 0){
1999 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2000 return -1;
2001 }
de6d9b64 2002
054480a5 2003 /* we set MPEG-2 parameters so that it emulates MPEG-1 */
de6d9b64
FB
2004 s->progressive_sequence = 1;
2005 s->progressive_frame = 1;
2006 s->picture_structure = PICT_FRAME;
2007 s->frame_pred_frame_dct = 1;
4eaad9c0 2008 s->chroma_format = 1;
029911d1 2009 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
054480a5 2010 avctx->sub_id = 1; /* indicates MPEG-1 */
a6b9ffbf 2011 s->out_format = FMT_MPEG1;
054480a5 2012 s->swap_uv = 0;//AFAIK VCR2 does not have SEQ_HEADER
248a189a 2013 if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
115329f1 2014
0368c72d 2015 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
115329f1 2016 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
0368c72d 2017 s->avctx->rc_buffer_size, s->bit_rate);
115329f1 2018
de6d9b64
FB
2019 return 0;
2020}
2021
e94bc100
MN
2022static int vcr2_init_sequence(AVCodecContext *avctx)
2023{
2024 Mpeg1Context *s1 = avctx->priv_data;
2025 MpegEncContext *s = &s1->mpeg_enc_ctx;
008f0851 2026 int i, v;
e94bc100 2027
054480a5 2028 /* start new MPEG-1 context decoding */
e94bc100
MN
2029 s->out_format = FMT_MPEG1;
2030 if (s1->mpeg_enc_ctx_allocated) {
2031 MPV_common_end(s);
2032 }
21adafec
MN
2033 s->width = avctx->coded_width;
2034 s->height = avctx->coded_height;
e94bc100 2035 avctx->has_b_frames= 0; //true?
ff862be5 2036 s->low_delay= 1;
2e7b4c84 2037
448ecb68 2038 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
a05aa821 2039 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
530d5740 2040
765e94ef 2041 if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel ||
d37edddc 2042 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU )
a579db0c
IK
2043 if( avctx->idct_algo == FF_IDCT_AUTO )
2044 avctx->idct_algo = FF_IDCT_SIMPLE;
115329f1 2045
e94bc100
MN
2046 if (MPV_common_init(s) < 0)
2047 return -1;
a579db0c 2048 exchange_uv(s);//common init reset pblocks, so we swap them here
115329f1 2049 s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
e94bc100
MN
2050 s1->mpeg_enc_ctx_allocated = 1;
2051
2052 for(i=0;i<64;i++) {
2053 int j= s->dsp.idct_permutation[i];
2054 v = ff_mpeg1_default_intra_matrix[i];
2055 s->intra_matrix[j] = v;
2056 s->chroma_intra_matrix[j] = v;
2057
2058 v = ff_mpeg1_default_non_intra_matrix[i];
2059 s->inter_matrix[j] = v;
2060 s->chroma_inter_matrix[j] = v;
2061 }
2062
e94bc100
MN
2063 s->progressive_sequence = 1;
2064 s->progressive_frame = 1;
2065 s->picture_structure = PICT_FRAME;
2066 s->frame_pred_frame_dct = 1;
4eaad9c0 2067 s->chroma_format = 1;
029911d1 2068 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
054480a5 2069 avctx->sub_id = 2; /* indicates MPEG-2 */
b52fcba9
CEH
2070 s1->save_width = s->width;
2071 s1->save_height = s->height;
2072 s1->save_progressive_seq = s->progressive_sequence;
e94bc100
MN
2073 return 0;
2074}
2075
2076
115329f1 2077static void mpeg_decode_user_data(AVCodecContext *avctx,
191ad11e 2078 const uint8_t *p, int buf_size)
e2f9490e 2079{
191ad11e 2080 const uint8_t *buf_end = p+buf_size;
e2f9490e
FB
2081
2082 /* we parse the DTG active format information */
191ad11e 2083 if (buf_end - p >= 5 &&
e2f9490e 2084 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
191ad11e 2085 int flags = p[4];
e2f9490e 2086 p += 5;
e2f9490e
FB
2087 if (flags & 0x80) {
2088 /* skip event id */
e2f9490e 2089 p += 2;
e2f9490e
FB
2090 }
2091 if (flags & 0x40) {
191ad11e 2092 if (buf_end - p < 1)
e2f9490e
FB
2093 return;
2094 avctx->dtg_active_format = p[0] & 0x0f;
2095 }
2096 }
2097}
2098
115329f1 2099static void mpeg_decode_gop(AVCodecContext *avctx,
5ac47683
IK
2100 const uint8_t *buf, int buf_size){
2101 Mpeg1Context *s1 = avctx->priv_data;
2102 MpegEncContext *s = &s1->mpeg_enc_ctx;
2103
2104 int drop_frame_flag;
2105 int time_code_hours, time_code_minutes;
2106 int time_code_seconds, time_code_pictures;
9d9a6239 2107 int broken_link;
5ac47683
IK
2108
2109 init_get_bits(&s->gb, buf, buf_size*8);
2110
2111 drop_frame_flag = get_bits1(&s->gb);
115329f1 2112
5ac47683
IK
2113 time_code_hours=get_bits(&s->gb,5);
2114 time_code_minutes = get_bits(&s->gb,6);
2115 skip_bits1(&s->gb);//marker bit
2116 time_code_seconds = get_bits(&s->gb,6);
2117 time_code_pictures = get_bits(&s->gb,6);
2118
9d9a6239 2119 s->closed_gop = get_bits1(&s->gb);
5ac47683
IK
2120 /*broken_link indicate that after editing the
2121 reference frames of the first B-Frames after GOP I-Frame
2122 are missing (open gop)*/
2123 broken_link = get_bits1(&s->gb);
2124
2125 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
ce8f4fb3 2126 av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
bb270c08 2127 time_code_hours, time_code_minutes, time_code_seconds,
9d9a6239 2128 time_code_pictures, s->closed_gop, broken_link);
5ac47683 2129}
80097bbf 2130/**
49bd8e4b 2131 * Find the end of the current frame in the bitstream.
80097bbf
MN
2132 * @return the position of the first byte of the next frame, or -1
2133 */
a4c7a5ea 2134int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
e4cb187d 2135{
80097bbf 2136 int i;
82fcbc14 2137 uint32_t state= pc->state;
115329f1 2138
21da962c
MN
2139 /* EOF considered as end of frame */
2140 if (buf_size == 0)
2141 return 0;
2142
2143/*
2144 0 frame start -> 1/4
2145 1 first_SEQEXT -> 0/2
2146 2 first field start -> 3/0
2147 3 second_SEQEXT -> 2/0
2148 4 searching end
2149*/
2150
2151 for(i=0; i<buf_size; i++){
2152 assert(pc->frame_start_found>=0 && pc->frame_start_found<=4);
2153 if(pc->frame_start_found&1){
2154 if(state == EXT_START_CODE && (buf[i]&0xF0) != 0x80)
2155 pc->frame_start_found--;
2156 else if(state == EXT_START_CODE+2){
2157 if((buf[i]&3) == 3) pc->frame_start_found= 0;
2158 else pc->frame_start_found= (pc->frame_start_found+1)&3;
2159 }
2160 state++;
2161 }else{
82fcbc14 2162 i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
21da962c 2163 if(pc->frame_start_found==0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
80097bbf 2164 i++;
21da962c 2165 pc->frame_start_found=4;
80097bbf 2166 }
392b7482
MN
2167 if(state == SEQ_END_CODE){
2168 pc->state=-1;
2169 return i+1;
2170 }
21da962c
MN
2171 if(pc->frame_start_found==2 && state == SEQ_START_CODE)
2172 pc->frame_start_found= 0;
2173 if(pc->frame_start_found<4 && state == EXT_START_CODE)
2174 pc->frame_start_found++;
2175 if(pc->frame_start_found == 4 && (state&0xFFFFFF00) == 0x100){
80097bbf
MN
2176 if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
2177 pc->frame_start_found=0;
115329f1 2178 pc->state=-1;
80097bbf
MN
2179 return i-3;
2180 }
2181 }
acbb378d 2182 if(pc->frame_start_found == 0 && s && state == PICTURE_START_CODE){
ed1dc74e 2183 ff_fetch_timestamp(s, i-3, 1);
a4c7a5ea 2184 }
80097bbf 2185 }
115329f1 2186 }
80097bbf 2187 pc->state= state;
bb463d81 2188 return END_NOT_FOUND;
80097bbf
MN
2189}
2190
7f8ef975
MN
2191static int decode_chunks(AVCodecContext *avctx,
2192 AVFrame *picture, int *data_size,
2193 const uint8_t *buf, int buf_size);
2194
de6d9b64 2195/* handle buffering and image synchronisation */
115329f1 2196static int mpeg_decode_frame(AVCodecContext *avctx,
de6d9b64 2197 void *data, int *data_size,
7a00bbad 2198 AVPacket *avpkt)
de6d9b64 2199{
7a00bbad
TB
2200 const uint8_t *buf = avpkt->data;
2201 int buf_size = avpkt->size;
de6d9b64 2202 Mpeg1Context *s = avctx->priv_data;
492cd3a9 2203 AVFrame *picture = data;
1cb0edb4 2204 MpegEncContext *s2 = &s->mpeg_enc_ctx;
dfd2a005 2205 av_dlog(avctx, "fill_buffer\n");
de6d9b64 2206
392b7482 2207 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
bb270c08
DB
2208 /* special case for last picture */
2209 if (s2->low_delay==0 && s2->next_picture_ptr) {
2210 *picture= *(AVFrame*)s2->next_picture_ptr;
2211 s2->next_picture_ptr= NULL;
1e491e29 2212
bb270c08
DB
2213 *data_size = sizeof(AVFrame);
2214 }
b8a9dfb7 2215 return buf_size;
de6d9b64
FB
2216 }
2217
80097bbf 2218 if(s2->flags&CODEC_FLAG_TRUNCATED){
a4c7a5ea 2219 int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
115329f1 2220
c53d2d90 2221 if( ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
80097bbf 2222 return buf_size;
115329f1
DB
2223 }
2224
115329f1
DB
2225#if 0
2226 if (s->repeat_field % 2 == 1) {
1cb0edb4
J
2227 s->repeat_field++;
2228 //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
e0a3d744
J
2229 // s2->picture_number, s->repeat_field);
2230 if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
2231 *data_size = sizeof(AVPicture);
2232 goto the_end;
2233 }
1cb0edb4 2234 }
e0a3d744 2235#endif
e94bc100 2236
2bb6eba2 2237 if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == AV_RL32("VCR2"))
e94bc100 2238 vcr2_init_sequence(avctx);
115329f1 2239
c62c07d3 2240 s->slice_count= 0;
115329f1 2241
9b7ca3b7
MN
2242 if(avctx->extradata && !avctx->frame_number)
2243 decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
2244
7f8ef975
MN
2245 return decode_chunks(avctx, picture, data_size, buf, buf_size);
2246}
2247
2248static int decode_chunks(AVCodecContext *avctx,
2249 AVFrame *picture, int *data_size,
2250 const uint8_t *buf, int buf_size)
2251{
2252 Mpeg1Context *s = avctx->priv_data;
2253 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2254 const uint8_t *buf_ptr = buf;
2255 const uint8_t *buf_end = buf + buf_size;
2256 int ret, input_size;
fc23d843 2257 int last_code= 0;
7f8ef975 2258
80097bbf 2259 for(;;) {
054480a5 2260 /* find next start code */
7f8ef975 2261 uint32_t start_code = -1;
82fcbc14 2262 buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
191e8ca7 2263 if (start_code > 0x1ff){
9701840b 2264 if(s2->pict_type != FF_B_TYPE || avctx->skip_frame <= AVDISCARD_DEFAULT){
c62c07d3
MN
2265 if(avctx->thread_count > 1){
2266 int i;
2267
01418506 2268 avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
c62c07d3
MN
2269 for(i=0; i<s->slice_count; i++)
2270 s2->error_count += s2->thread_context[i]->error_count;
2271 }
d37edddc 2272
e7edb2ea 2273 if (CONFIG_MPEG_VDPAU_DECODER && avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
d37edddc
NC
2274 ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
2275
6be8b204
BC
2276 if (slice_end(avctx, picture)) {
2277 if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
2278 *data_size = sizeof(AVPicture);
2279 }
db6e7795 2280 }
ed16f91f 2281 s2->pict_type= 0;
cfcff636 2282 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
de6d9b64 2283 }
115329f1 2284
cf713bb8
MN
2285 input_size = buf_end - buf_ptr;
2286
2287 if(avctx->debug & FF_DEBUG_STARTCODE){
b2c26e27 2288 av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
cf713bb8 2289 }
80097bbf 2290
e344c1ea
SH
2291 /* prepare data for next start code */
2292 switch(start_code) {
2293 case SEQ_START_CODE:
fc23d843 2294 if(last_code == 0){
e344c1ea
SH
2295 mpeg1_decode_sequence(avctx, buf_ptr,
2296 input_size);
ab8c48de 2297 s->sync=1;
fc23d843
MN
2298 }else{
2299 av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
2300 }
e344c1ea 2301 break;
115329f1 2302
e344c1ea 2303 case PICTURE_START_CODE:
f82163cf 2304 if (HAVE_THREADS && (avctx->active_thread_type&FF_THREAD_SLICE) && s->slice_count) {
f7114249
RB
2305 int i;
2306
2307 avctx->execute(avctx, slice_decode_thread,
2308 s2->thread_context, NULL,
2309 s->slice_count, sizeof(void*));
2310 for (i = 0; i < s->slice_count; i++)
2311 s2->error_count += s2->thread_context[i]->error_count;
2312 s->slice_count = 0;
2313 }
88eba670 2314 if(last_code == 0 || last_code == SLICE_MIN_START_CODE){
bbf266fd
MN
2315 if(mpeg_decode_postinit(avctx) < 0){
2316 av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
2317 return -1;
2318 }
2319
054480a5 2320 /* we have a complete image: we try to decompress it */
ed16f91f
MN
2321 if(mpeg1_decode_picture(avctx,
2322 buf_ptr, input_size) < 0)
2323 s2->pict_type=0;
1c326093 2324 s2->first_slice = 1;
fc23d843 2325 last_code= PICTURE_START_CODE;
88eba670
MN
2326 }else{
2327 av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
2328 }
e344c1ea
SH
2329 break;
2330 case EXT_START_CODE:
1206f1d6
MN
2331 init_get_bits(&s2->gb, buf_ptr, input_size*8);
2332
2333 switch(get_bits(&s2->gb, 4)) {
2334 case 0x1:
fc23d843 2335 if(last_code == 0){
1206f1d6 2336 mpeg_decode_sequence_extension(s);
fc23d843
MN
2337 }else{
2338 av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
2339 }
1206f1d6
MN
2340 break;
2341 case 0x2:
2342 mpeg_decode_sequence_display_extension(s);
2343 break;
2344 case 0x3:
2345 mpeg_decode_quant_matrix_extension(s2);
2346 break;
2347 case 0x7:
2348 mpeg_decode_picture_display_extension(s);
2349 break;
2350 case 0x8:
88eba670 2351 if(last_code == PICTURE_START_CODE){
1206f1d6 2352 mpeg_decode_picture_coding_extension(s);
88eba670
MN
2353 }else{
2354 av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
2355 }
1206f1d6
MN
2356 break;
2357 }
e344c1ea
SH
2358 break;
2359 case USER_START_CODE:
2360 mpeg_decode_user_data(avctx,
2361 buf_ptr, input_size);
2362 break;
2363 case GOP_START_CODE:
fc23d843 2364 if(last_code == 0){
e344c1ea
SH
2365 s2->first_field=0;
2366 mpeg_decode_gop(avctx,
2367 buf_ptr, input_size);
ab8c48de 2368 s->sync=1;
fc23d843
MN
2369 }else{
2370 av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
2371 }
e344c1ea
SH
2372 break;
2373 default:
2374 if (start_code >= SLICE_MIN_START_CODE &&
fc23d843 2375 start_code <= SLICE_MAX_START_CODE && last_code!=0) {
078cdecf
MN
2376 const int field_pic= s2->picture_structure != PICT_FRAME;
2377 int mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
fc23d843 2378 last_code= SLICE_MIN_START_CODE;
e344c1ea 2379
078cdecf
MN
2380 if(s2->picture_structure == PICT_BOTTOM_FIELD)
2381 mb_y++;
2382
52069c4d
MN
2383 if (mb_y >= s2->mb_height){
2384 av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2385 return -1;
2386 }
2387
e344c1ea 2388 if(s2->last_picture_ptr==NULL){
9d9a6239
BC
2389 /* Skip B-frames if we do not have reference frames and gop is not closed */
2390 if(s2->pict_type==FF_B_TYPE){
9d9a6239
BC
2391 if(!s2->closed_gop)
2392 break;
9d9a6239 2393 }
9bd005bd 2394 }
ab8c48de
MN
2395 if(s2->pict_type==FF_I_TYPE)
2396 s->sync=1;
9bd005bd 2397 if(s2->next_picture_ptr==NULL){
054480a5 2398 /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
ab8c48de 2399 if(s2->pict_type==FF_P_TYPE && !s->sync) break;
e344c1ea 2400 }
9701840b
AJ
2401 if( (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==FF_B_TYPE)
2402 ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=FF_I_TYPE)
e344c1ea 2403 || avctx->skip_frame >= AVDISCARD_ALL)
05fd1577 2404 break;
115329f1 2405
e344c1ea
SH
2406 if (!s->mpeg_enc_ctx_allocated) break;
2407
2408 if(s2->codec_id == CODEC_ID_MPEG2VIDEO){
2409 if(mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
2410 break;
2411 }
115329f1 2412
ed16f91f
MN
2413 if(!s2->pict_type){
2414 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2415 break;
2416 }
2417
e344c1ea
SH
2418 if(s2->first_slice){
2419 s2->first_slice=0;
765e94ef 2420 if(mpeg_field_start(s2, buf, buf_size) < 0)
e344c1ea 2421 return -1;
4acfdcab 2422 }
0e066acb 2423 if(!s2->current_picture_ptr){
90b5b51e 2424 av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
0e066acb
MN
2425 return -1;
2426 }
115329f1 2427
d37edddc
NC
2428 if (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
2429 s->slice_count++;
2430 break;
2431 }
2432
e344c1ea
SH
2433 if(avctx->thread_count > 1){
2434 int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
2435 if(threshold <= mb_y){
2436 MpegEncContext *thread_context= s2->thread_context[s->slice_count];
2437
2438 thread_context->start_mb_y= mb_y;
2439 thread_context->end_mb_y = s2->mb_height;
2440 if(s->slice_count){
2441 s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
2442 ff_update_duplicate_context(thread_context, s2);
de6d9b64 2443 }
e344c1ea
SH
2444 init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
2445 s->slice_count++;
2446 }
054480a5 2447 buf_ptr += 2; //FIXME add minimum number of bytes per slice
e344c1ea
SH
2448 }else{
2449 ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
2450 emms_c();
2451
2452 if(ret < 0){
2453 if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
2454 ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
2455 }else{
2456 ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, AC_END|DC_END|MV_END);
de6d9b64 2457 }
de6d9b64 2458 }
e344c1ea
SH
2459 }
2460 break;
2461 }
de6d9b64 2462 }
de6d9b64
FB
2463}
2464
ab8c48de
MN
2465static void flush(AVCodecContext *avctx){
2466 Mpeg1Context *s = avctx->priv_data;
2467
2468 s->sync=0;
2469
2470 ff_mpeg_flush(avctx);
2471}
2472
de6d9b64
FB
2473static int mpeg_decode_end(AVCodecContext *avctx)
2474{
2475 Mpeg1Context *s = avctx->priv_data;
2476
2477 if (s->mpeg_enc_ctx_allocated)
2478 MPV_common_end(&s->mpeg_enc_ctx);
2479 return 0;
2480}
2481
aecd0a44
BL
2482static const AVProfile mpeg2_video_profiles[] = {
2483 { FF_PROFILE_MPEG2_422, "4:2:2" },
2484 { FF_PROFILE_MPEG2_HIGH, "High" },
2485 { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
2486 { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
2487 { FF_PROFILE_MPEG2_MAIN, "Main" },
2488 { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
2489 { FF_PROFILE_RESERVED, "Reserved" },
2490 { FF_PROFILE_RESERVED, "Reserved" },
62d33dbc 2491 { FF_PROFILE_UNKNOWN },
aecd0a44
BL
2492};
2493
2494
d36beb3f 2495AVCodec ff_mpeg1video_decoder = {
922bc38d 2496 "mpeg1video",
72415b2a 2497 AVMEDIA_TYPE_VIDEO,
de6d9b64
FB
2498 CODEC_ID_MPEG1VIDEO,
2499 sizeof(Mpeg1Context),
2500 mpeg_decode_init,
2501 NULL,
2502 mpeg_decode_end,
2503 mpeg_decode_frame,
934982c4 2504 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
ab8c48de 2505 .flush= flush,
0fd0ef79 2506 .max_lowres= 3,
fe4bf374 2507 .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
922bc38d
MN
2508};
2509
d36beb3f 2510AVCodec ff_mpeg2video_decoder = {
922bc38d 2511 "mpeg2video",
72415b2a 2512 AVMEDIA_TYPE_VIDEO,
922bc38d
MN
2513 CODEC_ID_MPEG2VIDEO,
2514 sizeof(Mpeg1Context),
2515 mpeg_decode_init,
2516 NULL,
2517 mpeg_decode_end,
2518 mpeg_decode_frame,
94f7451a 2519 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
ab8c48de 2520 .flush= flush,
0fd0ef79 2521 .max_lowres= 3,
fe4bf374 2522 .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
aecd0a44 2523 .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
c512b303
IK
2524};
2525
2526//legacy decoder
d36beb3f 2527AVCodec ff_mpegvideo_decoder = {
c512b303 2528 "mpegvideo",
72415b2a 2529 AVMEDIA_TYPE_VIDEO,
c512b303
IK
2530 CODEC_ID_MPEG2VIDEO,
2531 sizeof(Mpeg1Context),
2532 mpeg_decode_init,
2533 NULL,
2534 mpeg_decode_end,
2535 mpeg_decode_frame,
94f7451a 2536 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
ab8c48de 2537 .flush= flush,
0fd0ef79 2538 .max_lowres= 3,
fe4bf374 2539 .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
de6d9b64 2540};
2e7b4c84 2541
fc2dd7e3 2542#if CONFIG_MPEG_XVMC_DECODER
98a6fff9 2543static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx){
115329f1 2544 if( avctx->thread_count > 1)
715731a3 2545 return -1;
2e7b4c84
IK
2546 if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
2547 return -1;
a579db0c 2548 if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
dfd2a005 2549 av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
a579db0c 2550 }
2e7b4c84 2551 mpeg_decode_init(avctx);
2e7b4c84
IK
2552
2553 avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
a579db0c 2554 avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
2e7b4c84
IK
2555
2556 return 0;
2557}
2558
d36beb3f 2559AVCodec ff_mpeg_xvmc_decoder = {
2e7b4c84 2560 "mpegvideo_xvmc",
72415b2a 2561 AVMEDIA_TYPE_VIDEO,
2e7b4c84
IK
2562 CODEC_ID_MPEG2VIDEO_XVMC,
2563 sizeof(Mpeg1Context),
2564 mpeg_mc_decode_init,
2565 NULL,
2566 mpeg_decode_end,
2567 mpeg_decode_frame,
934982c4 2568 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
ab8c48de 2569 .flush= flush,
326b554c 2570 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2e7b4c84
IK
2571};
2572
2573#endif
d37edddc
NC
2574
2575#if CONFIG_MPEG_VDPAU_DECODER
d36beb3f 2576AVCodec ff_mpeg_vdpau_decoder = {
d37edddc 2577 "mpegvideo_vdpau",
72415b2a 2578 AVMEDIA_TYPE_VIDEO,
d37edddc
NC
2579 CODEC_ID_MPEG2VIDEO,
2580 sizeof(Mpeg1Context),
2581 mpeg_decode_init,
2582 NULL,
2583 mpeg_decode_end,
2584 mpeg_decode_frame,
2585 CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
ab8c48de 2586 .flush= flush,
d37edddc
NC
2587 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
2588};
2589#endif
2590
2b324225 2591#if CONFIG_MPEG1_VDPAU_DECODER
d36beb3f 2592AVCodec ff_mpeg1_vdpau_decoder = {
2b324225 2593 "mpeg1video_vdpau",
72415b2a 2594 AVMEDIA_TYPE_VIDEO,
2b324225
CEH
2595 CODEC_ID_MPEG1VIDEO,
2596 sizeof(Mpeg1Context),
2597 mpeg_decode_init,
2598 NULL,
2599 mpeg_decode_end,
2600 mpeg_decode_frame,
2601 CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
ab8c48de 2602 .flush= flush,
2b324225
CEH
2603 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
2604};
2605#endif
2606