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