40
39
#define MBA_STUFFING 33
41
40
#define MBA_STARTCODE 34
43
extern uint8_t ff_h261_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
45
42
static VLC h261_mba_vlc;
46
43
static VLC h261_mtype_vlc;
47
44
static VLC h261_mv_vlc;
48
45
static VLC h261_cbp_vlc;
50
static int h261_decode_block(H261Context * h, DCTELEM * block, int n, int coded);
52
static av_cold void h261_decode_init_vlc(H261Context *h){
47
static av_cold void h261_decode_init_vlc(H261Context *h)
53
49
static int done = 0;
57
53
INIT_VLC_STATIC(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
59
h261_mba_code, 1, 1, 662);
54
ff_h261_mba_bits, 1, 1,
55
ff_h261_mba_code, 1, 1, 662);
60
56
INIT_VLC_STATIC(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
61
h261_mtype_bits, 1, 1,
62
h261_mtype_code, 1, 1, 80);
57
ff_h261_mtype_bits, 1, 1,
58
ff_h261_mtype_code, 1, 1, 80);
63
59
INIT_VLC_STATIC(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
64
&h261_mv_tab[0][1], 2, 1,
65
&h261_mv_tab[0][0], 2, 1, 144);
60
&ff_h261_mv_tab[0][1], 2, 1,
61
&ff_h261_mv_tab[0][0], 2, 1, 144);
66
62
INIT_VLC_STATIC(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
67
&h261_cbp_tab[0][1], 2, 1,
68
&h261_cbp_tab[0][0], 2, 1, 512);
69
ff_init_rl(&h261_rl_tcoeff, ff_h261_rl_table_store);
70
INIT_VLC_RL(h261_rl_tcoeff, 552);
63
&ff_h261_cbp_tab[0][1], 2, 1,
64
&ff_h261_cbp_tab[0][0], 2, 1, 512);
65
INIT_VLC_RL(ff_h261_rl_tcoeff, 552);
74
static av_cold int h261_decode_init(AVCodecContext *avctx){
75
H261Context *h= avctx->priv_data;
76
MpegEncContext * const s = &h->s;
69
static av_cold int h261_decode_init(AVCodecContext *avctx)
71
H261Context *h = avctx->priv_data;
72
MpegEncContext *const s = &h->s;
79
75
ff_MPV_decode_defaults(s);
82
s->width = s->avctx->coded_width;
83
s->height = s->avctx->coded_height;
84
s->codec_id = s->avctx->codec->id;
86
s->out_format = FMT_H261;
88
avctx->pix_fmt= AV_PIX_FMT_YUV420P;
90
s->codec_id= avctx->codec->id;
77
s->width = s->avctx->coded_width;
78
s->height = s->avctx->coded_height;
79
s->codec_id = s->avctx->codec->id;
80
s->out_format = FMT_H261;
82
avctx->pix_fmt = AV_PIX_FMT_YUV420P;
83
s->codec_id = avctx->codec->id;
85
ff_h261_common_init();
92
86
h261_decode_init_vlc(h);
94
88
h->gob_start_code_skipped = 0;
117
112
h->gob_start_code_skipped = 0;
119
114
h->gob_number = get_bits(&s->gb, 4); /* GN */
120
s->qscale = get_bits(&s->gb, 5); /* GQUANT */
115
s->qscale = get_bits(&s->gb, 5); /* GQUANT */
122
117
/* Check if gob_number is valid */
123
if (s->mb_height==18){ //cif
124
if ((h->gob_number<=0) || (h->gob_number>12))
118
if (s->mb_height == 18) { // CIF
119
if ((h->gob_number <= 0) || (h->gob_number > 12))
128
if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
122
if ((h->gob_number != 1) && (h->gob_number != 3) &&
123
(h->gob_number != 5))
133
while (get_bits1(&s->gb) != 0) {
128
while (get_bits1(&s->gb) != 0)
134
129
skip_bits(&s->gb, 8);
131
if (s->qscale == 0) {
138
132
av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\n");
139
133
if (s->avctx->err_recognition & AV_EF_BITSTREAM)
143
// For the first transmitted macroblock in a GOB, MBA is the absolute address. For
144
// subsequent macroblocks, MBA is the difference between the absolute addresses of
145
// the macroblock and the last transmitted macroblock.
137
/* For the first transmitted macroblock in a GOB, MBA is the absolute
138
* address. For subsequent macroblocks, MBA is the difference between
139
* the absolute addresses of the macroblock and the last transmitted
146
141
h->current_mba = 0;
153
148
* Decode the group of blocks / video packet header.
154
149
* @return <0 if no resync found
156
static int ff_h261_resync(H261Context *h){
157
MpegEncContext * const s = &h->s;
151
static int h261_resync(H261Context *h)
153
MpegEncContext *const s = &h->s;
160
if ( h->gob_start_code_skipped ){
161
ret= h261_decode_gob_header(h);
156
if (h->gob_start_code_skipped) {
157
ret = h261_decode_gob_header(h);
166
if(show_bits(&s->gb, 15)==0){
167
ret= h261_decode_gob_header(h);
161
if (show_bits(&s->gb, 15) == 0) {
162
ret = h261_decode_gob_header(h);
171
//OK, it is not where it is supposed to be ...
172
s->gb= s->last_resync_gb;
166
// OK, it is not where it is supposed to be ...
167
s->gb = s->last_resync_gb;
173
168
align_get_bits(&s->gb);
174
left= get_bits_left(&s->gb);
176
for(;left>15+1+4+5; left-=8){
177
if(show_bits(&s->gb, 15)==0){
178
GetBitContext bak= s->gb;
180
ret= h261_decode_gob_header(h);
169
left = get_bits_left(&s->gb);
171
for (; left > 15 + 1 + 4 + 5; left -= 8) {
172
if (show_bits(&s->gb, 15) == 0) {
173
GetBitContext bak = s->gb;
175
ret = h261_decode_gob_header(h);
186
181
skip_bits(&s->gb, 8);
194
189
* Decode skipped macroblocks.
197
static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
192
static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2)
199
MpegEncContext * const s = &h->s;
194
MpegEncContext *const s = &h->s;
204
for(i=mba1; i<mba2; i++){
199
for (i = mba1; i < mba2; i++) {
207
s->mb_x= ((h->gob_number-1) % 2) * 11 + i % 11;
208
s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11;
209
xy = s->mb_x + s->mb_y * s->mb_stride;
202
s->mb_x = ((h->gob_number - 1) % 2) * 11 + i % 11;
203
s->mb_y = ((h->gob_number - 1) / 2) * 3 + i / 11;
204
xy = s->mb_x + s->mb_y * s->mb_stride;
210
205
ff_init_block_index(s);
211
206
ff_update_block_index(s);
208
for (j = 0; j < 6; j++)
214
209
s->block_last_index[j] = -1;
216
s->mv_dir = MV_DIR_FORWARD;
217
s->mv_type = MV_TYPE_16X16;
218
s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
222
h->mtype &= ~MB_TYPE_H261_FIL;
211
s->mv_dir = MV_DIR_FORWARD;
212
s->mv_type = MV_TYPE_16X16;
213
s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
217
h->mtype &= ~MB_TYPE_H261_FIL;
224
219
ff_MPV_decode_mb(s, s->block);
230
static int decode_mv_component(GetBitContext *gb, int v){
225
static const int mvmap[17] = {
226
0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16
229
static int decode_mv_component(GetBitContext *gb, int v)
231
231
int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
233
233
/* check if mv_diff is valid */
237
237
mv_diff = mvmap[mv_diff];
239
if(mv_diff && !get_bits1(gb))
239
if (mv_diff && !get_bits1(gb))
244
else if(v >= 16) v-= 32;
249
static int h261_decode_mb(H261Context *h){
250
MpegEncContext * const s = &h->s;
252
* Decode a macroblock.
253
* @return <0 if an error occurred
255
static int h261_decode_block(H261Context *h, int16_t *block, int n, int coded)
257
MpegEncContext *const s = &h->s;
258
int code, level, i, j, run;
259
RLTable *rl = &ff_h261_rl_tcoeff;
260
const uint8_t *scan_table;
262
/* For the variable length encoding there are two code tables, one being
263
* used for the first transmitted LEVEL in INTER, INTER + MC and
264
* INTER + MC + FIL blocks, the second for all other LEVELs except the
265
* first one in INTRA blocks which is fixed length coded with 8 bits.
266
* NOTE: The two code tables only differ in one VLC so we handle that
268
scan_table = s->intra_scantable.permutated;
271
level = get_bits(&s->gb, 8);
272
// 0 (00000000b) and -128 (10000000b) are FORBIDDEN
273
if ((level & 0x7F) == 0) {
274
av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n",
275
level, s->mb_x, s->mb_y);
278
/* The code 1000 0000 is not used, the reconstruction level of 1024
279
* being coded as 1111 1111. */
286
// EOB Not possible for first level when cbp is available (that's why the table is different)
289
int check = show_bits(&s->gb, 2);
292
skip_bits(&s->gb, 2);
293
block[0] = (check & 0x1) ? -1 : 1;
300
s->block_last_index[n] = i - 1;
304
code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
306
av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n",
312
/* The remaining combinations of (run, level) are encoded with a
313
* 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits
315
run = get_bits(&s->gb, 6);
316
level = get_sbits(&s->gb, 8);
317
} else if (code == 0) {
320
run = rl->table_run[code];
321
level = rl->table_level[code];
322
if (get_bits1(&s->gb))
327
av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n",
335
s->block_last_index[n] = i - 1;
339
static int h261_decode_mb(H261Context *h)
341
MpegEncContext *const s = &h->s;
256
h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
347
h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table,
348
H261_MBA_VLC_BITS, 2);
258
350
/* Check for slice end */
259
351
/* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
260
if (h->mba_diff == MBA_STARTCODE){ // start code
352
if (h->mba_diff == MBA_STARTCODE) { // start code
261
353
h->gob_start_code_skipped = 1;
262
354
return SLICE_END;
265
while( h->mba_diff == MBA_STUFFING ); // stuffing
356
} while (h->mba_diff == MBA_STUFFING); // stuffing
267
if ( h->mba_diff < 0 ){
358
if (h->mba_diff < 0) {
268
359
if (get_bits_left(&s->gb) <= 7)
269
360
return SLICE_END;
272
363
return SLICE_ERROR;
276
367
h->current_mba += h->mba_diff;
278
if ( h->current_mba > MBA_STUFFING )
369
if (h->current_mba > MBA_STUFFING)
279
370
return SLICE_ERROR;
281
s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
282
s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
283
xy = s->mb_x + s->mb_y * s->mb_stride;
372
s->mb_x = ((h->gob_number - 1) % 2) * 11 + ((h->current_mba - 1) % 11);
373
s->mb_y = ((h->gob_number - 1) / 2) * 3 + ((h->current_mba - 1) / 11);
374
xy = s->mb_x + s->mb_y * s->mb_stride;
284
375
ff_init_block_index(s);
285
376
ff_update_block_index(s);
288
379
h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
289
if (h->mtype < 0 || h->mtype >= FF_ARRAY_ELEMS(h261_mtype_map)) {
380
if (h->mtype < 0 || h->mtype >= FF_ARRAY_ELEMS(ff_h261_mtype_map)) {
290
381
av_log(s->avctx, AV_LOG_ERROR, "Invalid mtype index %d\n",
292
383
return SLICE_ERROR;
294
h->mtype = h261_mtype_map[h->mtype];
385
h->mtype = ff_h261_mtype_map[h->mtype];
297
if ( IS_QUANT ( h->mtype ) ){
388
if (IS_QUANT(h->mtype))
298
389
ff_set_qscale(s, get_bits(&s->gb, 5));
301
391
s->mb_intra = IS_INTRA4x4(h->mtype);
304
if ( IS_16X16 ( h->mtype ) ){
305
// Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
306
// vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
307
// following three situations:
308
// 1) evaluating MVD for macroblocks 1, 12 and 23;
309
// 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
310
// 3) MTYPE of the previous macroblock was not MC.
311
if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
394
if (IS_16X16(h->mtype)) {
395
/* Motion vector data is included for all MC macroblocks. MVD is
396
* obtained from the macroblock vector by subtracting the vector
397
* of the preceding macroblock. For this calculation the vector
398
* of the preceding macroblock is regarded as zero in the
399
* following three situations:
400
* 1) evaluating MVD for macroblocks 1, 12 and 23;
401
* 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
402
* 3) MTYPE of the previous macroblock was not MC. */
403
if ((h->current_mba == 1) || (h->current_mba == 12) ||
404
(h->current_mba == 23) || (h->mba_diff != 1)) {
314
405
h->current_mv_x = 0;
315
406
h->current_mv_y = 0;
318
h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
319
h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
409
h->current_mv_x = decode_mv_component(&s->gb, h->current_mv_x);
410
h->current_mv_y = decode_mv_component(&s->gb, h->current_mv_y);
321
412
h->current_mv_x = 0;
322
413
h->current_mv_y = 0;
326
if ( HAS_CBP( h->mtype ) ){
417
if (HAS_CBP(h->mtype))
327
418
cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
331
s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
421
s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
335
425
//set motion vectors
336
s->mv_dir = MV_DIR_FORWARD;
337
s->mv_type = MV_TYPE_16X16;
338
s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
339
s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
340
s->mv[0][0][1] = h->current_mv_y * 2;
426
s->mv_dir = MV_DIR_FORWARD;
427
s->mv_type = MV_TYPE_16X16;
428
s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
429
s->mv[0][0][0] = h->current_mv_x * 2; // gets divided by 2 in motion compensation
430
s->mv[0][0][1] = h->current_mv_y * 2;
343
433
/* decode each block */
344
if(s->mb_intra || HAS_CBP(h->mtype)){
434
if (s->mb_intra || HAS_CBP(h->mtype)) {
345
435
s->dsp.clear_blocks(s->block[0]);
346
436
for (i = 0; i < 6; i++) {
347
if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
437
if (h261_decode_block(h, s->block[i], i, cbp & 32) < 0)
348
438
return SLICE_ERROR;
353
442
for (i = 0; i < 6; i++)
354
s->block_last_index[i]= -1;
443
s->block_last_index[i] = -1;
357
446
ff_MPV_decode_mb(s, s->block);
363
* Decode a macroblock.
364
* @return <0 if an error occurred
366
static int h261_decode_block(H261Context * h, DCTELEM * block,
369
MpegEncContext * const s = &h->s;
370
int code, level, i, j, run;
371
RLTable *rl = &h261_rl_tcoeff;
372
const uint8_t *scan_table;
374
// For the variable length encoding there are two code tables, one being used for
375
// the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
376
// for all other LEVELs except the first one in INTRA blocks which is fixed length
377
// coded with 8 bits.
378
// NOTE: the two code tables only differ in one VLC so we handle that manually.
379
scan_table = s->intra_scantable.permutated;
382
level = get_bits(&s->gb, 8);
383
// 0 (00000000b) and -128 (10000000b) are FORBIDDEN
384
if((level&0x7F) == 0){
385
av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
388
// The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
395
// EOB Not possible for first level when cbp is available (that's why the table is different)
398
int check = show_bits(&s->gb, 2);
401
skip_bits(&s->gb, 2);
402
block[0] = ( check & 0x1 ) ? -1 : 1;
409
s->block_last_index[n] = i - 1;
413
code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
415
av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
420
// The remaining combinations of (run, level) are encoded with a 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits level.
421
run = get_bits(&s->gb, 6);
422
level = get_sbits(&s->gb, 8);
426
run = rl->table_run[code];
427
level = rl->table_level[code];
428
if (get_bits1(&s->gb))
433
av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
440
s->block_last_index[n] = i-1;
445
452
* Decode the H.261 picture header.
446
453
* @return <0 if no startcode found
448
static int h261_decode_picture_header(H261Context *h){
449
MpegEncContext * const s = &h->s;
455
static int h261_decode_picture_header(H261Context *h)
457
MpegEncContext *const s = &h->s;
451
uint32_t startcode= 0;
459
uint32_t startcode = 0;
453
for(i= get_bits_left(&s->gb); i>24; i-=1){
461
for (i = get_bits_left(&s->gb); i > 24; i -= 1) {
454
462
startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
456
if(startcode == 0x10)
464
if (startcode == 0x10)
460
if (startcode != 0x10){
468
if (startcode != 0x10) {
461
469
av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
465
473
/* temporal reference */
466
i= get_bits(&s->gb, 5); /* picture timestamp */
467
if(i < (s->picture_number&31))
474
i = get_bits(&s->gb, 5); /* picture timestamp */
475
if (i < (s->picture_number & 31))
469
s->picture_number = (s->picture_number&~31) + i;
471
s->avctx->time_base= (AVRational){1001, 30000};
472
s->current_picture.f.pts = s->picture_number;
477
s->picture_number = (s->picture_number & ~31) + i;
479
s->avctx->time_base = (AVRational) { 1001, 30000 };
475
481
/* PTYPE starts here */
476
482
skip_bits1(&s->gb); /* split screen off */
498
504
skip_bits1(&s->gb); /* Reserved */
501
while (get_bits1(&s->gb) != 0){
507
while (get_bits1(&s->gb) != 0)
502
508
skip_bits(&s->gb, 8);
505
// h261 has no I-FRAMES, but if we pass AV_PICTURE_TYPE_I for the first frame, the codec crashes if it does
506
// not contain all I-blocks (e.g. when a packet is lost)
510
/* H.261 has no I-frames, but if we pass AV_PICTURE_TYPE_I for the first
511
* frame, the codec crashes if it does not contain all I-blocks
512
* (e.g. when a packet is lost). */
507
513
s->pict_type = AV_PICTURE_TYPE_P;
509
515
h->gob_number = 0;
513
static int h261_decode_gob(H261Context *h){
514
MpegEncContext * const s = &h->s;
519
static int h261_decode_gob(H261Context *h)
521
MpegEncContext *const s = &h->s;
516
523
ff_set_qscale(s, s->qscale);
518
525
/* decode mb's */
519
while(h->current_mba <= MBA_STUFFING)
526
while (h->current_mba <= MBA_STUFFING) {
522
528
/* DCT & quantize */
523
ret= h261_decode_mb(h);
529
ret = h261_decode_mb(h);
531
if (ret == SLICE_END) {
526
532
h261_decode_mb_skipped(h, h->current_mba, 33);
529
av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
535
av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n",
536
s->mb_x + s->mb_y * s->mb_stride);
533
h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
540
h261_decode_mb_skipped(h,
541
h->current_mba - h->mba_diff,
540
549
* returns the number of bytes consumed for building the current frame
542
static int get_consumed_bytes(MpegEncContext *s, int buf_size){
543
int pos= get_bits_count(&s->gb)>>3;
544
if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
545
if(pos+10>buf_size) pos=buf_size; // oops ;)
551
static int get_consumed_bytes(MpegEncContext *s, int buf_size)
553
int pos = get_bits_count(&s->gb) >> 3;
555
pos = 1; // avoid infinite loops (i doubt that is needed but ...)
556
if (pos + 10 > buf_size)
557
pos = buf_size; // oops ;)
550
static int h261_decode_frame(AVCodecContext *avctx,
551
void *data, int *got_frame,
562
static int h261_decode_frame(AVCodecContext *avctx, void *data,
563
int *got_frame, AVPacket *avpkt)
554
565
const uint8_t *buf = avpkt->data;
555
int buf_size = avpkt->size;
556
H261Context *h= avctx->priv_data;
557
MpegEncContext *s = &h->s;
566
int buf_size = avpkt->size;
567
H261Context *h = avctx->priv_data;
568
MpegEncContext *s = &h->s;
559
570
AVFrame *pict = data;
561
572
av_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
562
573
av_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
563
s->flags= avctx->flags;
564
s->flags2= avctx->flags2;
574
s->flags = avctx->flags;
575
s->flags2 = avctx->flags2;
566
h->gob_start_code_skipped=0;
577
h->gob_start_code_skipped = 0;
570
init_get_bits(&s->gb, buf, buf_size*8);
572
if(!s->context_initialized){
573
if (ff_MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
580
init_get_bits(&s->gb, buf, buf_size * 8);
582
if (!s->context_initialized)
583
// we need the IDCT permutaton for reading a custom matrix
584
if (ff_MPV_common_init(s) < 0)
577
//we need to set current_picture_ptr before reading the header, otherwise we cannot store anyting im there
578
if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
579
int i= ff_find_unused_picture(s, 0);
582
s->current_picture_ptr= &s->picture[i];
585
587
ret = h261_decode_picture_header(h);
587
589
/* skip if the header was thrashed */
589
591
av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
593
if (s->width != avctx->coded_width || s->height != avctx->coded_height){
594
ParseContext pc= s->parse_context; //FIXME move this demuxing hack to libavformat
595
s->parse_context.buffer=0;
595
if (s->width != avctx->coded_width || s->height != avctx->coded_height) {
596
ParseContext pc = s->parse_context; // FIXME move this demuxing hack to libavformat
597
s->parse_context.buffer = 0;
596
598
ff_MPV_common_end(s);
597
s->parse_context= pc;
599
s->parse_context = pc;
599
601
if (!s->context_initialized) {
600
avcodec_set_dimensions(avctx, s->width, s->height);
602
ret = ff_set_dimensions(avctx, s->width, s->height);
606
610
s->current_picture.f.pict_type = s->pict_type;
607
611
s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
609
if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==AV_PICTURE_TYPE_B)
610
||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=AV_PICTURE_TYPE_I)
611
|| avctx->skip_frame >= AVDISCARD_ALL)
613
if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
614
(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
615
avctx->skip_frame >= AVDISCARD_ALL)
612
616
return get_consumed_bytes(s, buf_size);
614
if(ff_MPV_frame_start(s, avctx) < 0)
618
if (ff_MPV_frame_start(s, avctx) < 0)
617
ff_er_frame_start(s);
621
ff_mpeg_er_frame_start(s);
619
623
/* decode each macroblock */
623
while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
624
if(ff_h261_resync(h)<0)
627
while (h->gob_number < (s->mb_height == 18 ? 12 : 5)) {
628
if (h261_resync(h) < 0)
626
630
h261_decode_gob(h);
628
632
ff_MPV_frame_end(s);
630
assert(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
631
assert(s->current_picture.f.pict_type == s->pict_type);
634
assert(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
635
assert(s->current_picture.f.pict_type == s->pict_type);
633
*pict = s->current_picture_ptr->f;
634
ff_print_debug_info(s, pict);
637
if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0)
639
ff_print_debug_info(s, s->current_picture_ptr);