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