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