cleanup
[libav.git] / libavcodec / h261.c
1 /*
2 * H261 decoder
3 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
4 * Copyright (c) 2004 Maarten Daniels
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 /**
22 * @file h261.c
23 * h261codec.
24 */
25
26 #include "common.h"
27 #include "dsputil.h"
28 #include "avcodec.h"
29 #include "mpegvideo.h"
30 #include "h261data.h"
31
32
33 #define H261_MBA_VLC_BITS 9
34 #define H261_MTYPE_VLC_BITS 6
35 #define H261_MV_VLC_BITS 7
36 #define H261_CBP_VLC_BITS 9
37 #define TCOEFF_VLC_BITS 9
38
39 #define MBA_STUFFING 33
40 #define MBA_STARTCODE 34
41 #define IS_FIL(a) ((a)&MB_TYPE_H261_FIL)
42
43 /**
44 * H261Context
45 */
46 typedef struct H261Context{
47 MpegEncContext s;
48
49 int current_mba;
50 int previous_mba;
51 int mba_diff;
52 int mtype;
53 int current_mv_x;
54 int current_mv_y;
55 int gob_number;
56 int bits_left; //8 - nr of bits left of the following frame in the last byte in this frame
57 int last_bits; //bits left of the following frame in the last byte in this frame
58 int gob_start_code_skipped; // 1 if gob start code is already read before gob header is read
59 }H261Context;
60
61 void ff_h261_loop_filter(MpegEncContext *s){
62 H261Context * h= (H261Context*)s;
63 const int linesize = s->linesize;
64 const int uvlinesize= s->uvlinesize;
65 uint8_t *dest_y = s->dest[0];
66 uint8_t *dest_cb= s->dest[1];
67 uint8_t *dest_cr= s->dest[2];
68
69 if(!(IS_FIL (h->mtype)))
70 return;
71
72 s->dsp.h261_loop_filter(dest_y , linesize);
73 s->dsp.h261_loop_filter(dest_y + 8, linesize);
74 s->dsp.h261_loop_filter(dest_y + 8 * linesize , linesize);
75 s->dsp.h261_loop_filter(dest_y + 8 * linesize + 8, linesize);
76 s->dsp.h261_loop_filter(dest_cb, uvlinesize);
77 s->dsp.h261_loop_filter(dest_cr, uvlinesize);
78 }
79
80 static int ff_h261_get_picture_format(int width, int height){
81 // QCIF
82 if (width == 176 && height == 144)
83 return 0;
84 // CIF
85 else if (width == 352 && height == 288)
86 return 1;
87 // ERROR
88 else
89 return -1;
90 }
91
92 static void h261_encode_block(H261Context * h, DCTELEM * block,
93 int n);
94 static int h261_decode_block(H261Context *h, DCTELEM *block,
95 int n, int coded);
96
97 void ff_h261_encode_picture_header(MpegEncContext * s, int picture_number){
98 H261Context * h = (H261Context *) s;
99 int format, temp_ref;
100
101 align_put_bits(&s->pb);
102
103 /* Update the pointer to last GOB */
104 s->ptr_lastgob = pbBufPtr(&s->pb);
105
106 put_bits(&s->pb, 20, 0x10); /* PSC */
107
108 temp_ref= s->picture_number * (int64_t)30000 * s->avctx->frame_rate_base /
109 (1001 * (int64_t)s->avctx->frame_rate);
110 put_bits(&s->pb, 5, temp_ref & 0x1f); /* TemporalReference */
111
112 put_bits(&s->pb, 1, 0); /* split screen off */
113 put_bits(&s->pb, 1, 0); /* camera off */
114 put_bits(&s->pb, 1, 0); /* freeze picture release off */
115
116 format = ff_h261_get_picture_format(s->width, s->height);
117
118 put_bits(&s->pb, 1, format); /* 0 == QCIF, 1 == CIF */
119
120 put_bits(&s->pb, 1, 0); /* still image mode */
121 put_bits(&s->pb, 1, 0); /* reserved */
122
123 put_bits(&s->pb, 1, 0); /* no PEI */
124 if(format == 0)
125 h->gob_number = -1;
126 else
127 h->gob_number = 0;
128 h->current_mba = 0;
129 }
130
131 /**
132 * Encodes a group of blocks header.
133 */
134 static void h261_encode_gob_header(MpegEncContext * s, int mb_line){
135 H261Context * h = (H261Context *)s;
136 if(ff_h261_get_picture_format(s->width, s->height) == 0){
137 h->gob_number+=2; // QCIF
138 }
139 else{
140 h->gob_number++; // CIF
141 }
142 put_bits(&s->pb, 16, 1); /* GBSC */
143 put_bits(&s->pb, 4, h->gob_number); /* GN */
144 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
145 put_bits(&s->pb, 1, 0); /* no GEI */
146 h->current_mba = 0;
147 h->previous_mba = 0;
148 h->current_mv_x=0;
149 h->current_mv_y=0;
150 }
151
152 void ff_h261_reorder_mb_index(MpegEncContext* s){
153 int index= s->mb_x + s->mb_y*s->mb_width;
154
155 if(index % 33 == 0)
156 h261_encode_gob_header(s,0);
157
158 /* for CIF the GOB's are fragmented in the middle of a scanline
159 that's why we need to adjust the x and y index of the macroblocks */
160 if(ff_h261_get_picture_format(s->width,s->height) == 1){ // CIF
161 s->mb_x = index % 11 ; index /= 11;
162 s->mb_y = index % 3 ; index /= 3;
163 s->mb_x+= 11*(index % 2); index /= 2;
164 s->mb_y+= 3*index;
165
166 ff_init_block_index(s);
167 ff_update_block_index(s);
168 }
169 }
170
171 static void h261_encode_motion(H261Context * h, int val){
172 MpegEncContext * const s = &h->s;
173 int sign, code;
174 if(val==0){
175 code = 0;
176 put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]);
177 }
178 else{
179 if(val > 16)
180 val -=32;
181 if(val < -16)
182 val+=32;
183 sign = val < 0;
184 code = sign ? -val : val;
185 put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]);
186 put_bits(&s->pb,1,sign);
187 }
188 }
189
190 static inline int get_cbp(MpegEncContext * s,
191 DCTELEM block[6][64])
192 {
193 int i, cbp;
194 cbp= 0;
195 for (i = 0; i < 6; i++) {
196 if (s->block_last_index[i] >= 0)
197 cbp |= 1 << (5 - i);
198 }
199 return cbp;
200 }
201 void ff_h261_encode_mb(MpegEncContext * s,
202 DCTELEM block[6][64],
203 int motion_x, int motion_y)
204 {
205 H261Context * h = (H261Context *)s;
206 int mvd, mv_diff_x, mv_diff_y, i, cbp;
207 cbp = 63; // avoid warning
208 mvd = 0;
209
210 h->current_mba++;
211 h->mtype = 0;
212
213 if (!s->mb_intra){
214 /* compute cbp */
215 cbp= get_cbp(s, block);
216
217 /* mvd indicates if this block is motion compensated */
218 mvd = motion_x | motion_y;
219
220 if((cbp | mvd | s->dquant ) == 0) {
221 /* skip macroblock */
222 s->skip_count++;
223 h->current_mv_x=0;
224 h->current_mv_y=0;
225 return;
226 }
227 }
228
229 /* MB is not skipped, encode MBA */
230 put_bits(&s->pb, h261_mba_bits[(h->current_mba-h->previous_mba)-1], h261_mba_code[(h->current_mba-h->previous_mba)-1]);
231
232 /* calculate MTYPE */
233 if(!s->mb_intra){
234 h->mtype++;
235
236 if(mvd || s->loop_filter)
237 h->mtype+=3;
238 if(s->loop_filter)
239 h->mtype+=3;
240 if(cbp || s->dquant)
241 h->mtype++;
242 assert(h->mtype > 1);
243 }
244
245 if(s->dquant)
246 h->mtype++;
247
248 put_bits(&s->pb, h261_mtype_bits[h->mtype], h261_mtype_code[h->mtype]);
249
250 h->mtype = h261_mtype_map[h->mtype];
251
252 if(IS_QUANT(h->mtype)){
253 ff_set_qscale(s,s->qscale+s->dquant);
254 put_bits(&s->pb, 5, s->qscale);
255 }
256
257 if(IS_16X16(h->mtype)){
258 mv_diff_x = (motion_x >> 1) - h->current_mv_x;
259 mv_diff_y = (motion_y >> 1) - h->current_mv_y;
260 h->current_mv_x = (motion_x >> 1);
261 h->current_mv_y = (motion_y >> 1);
262 h261_encode_motion(h,mv_diff_x);
263 h261_encode_motion(h,mv_diff_y);
264 }
265
266 h->previous_mba = h->current_mba;
267
268 if(HAS_CBP(h->mtype)){
269 put_bits(&s->pb,h261_cbp_tab[cbp-1][1],h261_cbp_tab[cbp-1][0]);
270 }
271 for(i=0; i<6; i++) {
272 /* encode each block */
273 h261_encode_block(h, block[i], i);
274 }
275
276 if ( ( h->current_mba == 11 ) || ( h->current_mba == 22 ) || ( h->current_mba == 33 ) || ( !IS_16X16 ( h->mtype ) )){
277 h->current_mv_x=0;
278 h->current_mv_y=0;
279 }
280 }
281
282 void ff_h261_encode_init(MpegEncContext *s){
283 static int done = 0;
284
285 if (!done) {
286 done = 1;
287 init_rl(&h261_rl_tcoeff);
288 }
289
290 s->min_qcoeff= -127;
291 s->max_qcoeff= 127;
292 s->y_dc_scale_table=
293 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
294 }
295
296
297 /**
298 * encodes a 8x8 block.
299 * @param block the 8x8 block
300 * @param n block index (0-3 are luma, 4-5 are chroma)
301 */
302 static void h261_encode_block(H261Context * h, DCTELEM * block, int n){
303 MpegEncContext * const s = &h->s;
304 int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
305 RLTable *rl;
306
307 rl = &h261_rl_tcoeff;
308 if (s->mb_intra) {
309 /* DC coef */
310 level = block[0];
311 /* 255 cannot be represented, so we clamp */
312 if (level > 254) {
313 level = 254;
314 block[0] = 254;
315 }
316 /* 0 cannot be represented also */
317 else if (level < 1) {
318 level = 1;
319 block[0] = 1;
320 }
321 if (level == 128)
322 put_bits(&s->pb, 8, 0xff);
323 else
324 put_bits(&s->pb, 8, level);
325 i = 1;
326 } else if((block[0]==1 || block[0] == -1) && (s->block_last_index[n] > -1)){
327 //special case
328 put_bits(&s->pb,2,block[0]>0 ? 2 : 3 );
329 i = 1;
330 } else {
331 i = 0;
332 }
333
334 /* AC coefs */
335 last_index = s->block_last_index[n];
336 last_non_zero = i - 1;
337 for (; i <= last_index; i++) {
338 j = s->intra_scantable.permutated[i];
339 level = block[j];
340 if (level) {
341 run = i - last_non_zero - 1;
342 last = (i == last_index);
343 sign = 0;
344 slevel = level;
345 if (level < 0) {
346 sign = 1;
347 level = -level;
348 }
349 code = get_rl_index(rl, 0 /*no last in H.261, EOB is used*/, run, level);
350 if(run==0 && level < 16)
351 code+=1;
352 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
353 if (code == rl->n) {
354 put_bits(&s->pb, 6, run);
355 assert(slevel != 0);
356 assert(level <= 127);
357 put_bits(&s->pb, 8, slevel & 0xff);
358 } else {
359 put_bits(&s->pb, 1, sign);
360 }
361 last_non_zero = i;
362 }
363 }
364 if(last_index > -1){
365 put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]);// END OF BLOCK
366 }
367 }
368
369 /***********************************************/
370 /* decoding */
371
372 static VLC h261_mba_vlc;
373 static VLC h261_mtype_vlc;
374 static VLC h261_mv_vlc;
375 static VLC h261_cbp_vlc;
376
377 void init_vlc_rl(RLTable *rl);
378
379 static void h261_decode_init_vlc(H261Context *h){
380 static int done = 0;
381
382 if(!done){
383 done = 1;
384 init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
385 h261_mba_bits, 1, 1,
386 h261_mba_code, 1, 1);
387 init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
388 h261_mtype_bits, 1, 1,
389 h261_mtype_code, 1, 1);
390 init_vlc(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
391 &h261_mv_tab[0][1], 2, 1,
392 &h261_mv_tab[0][0], 2, 1);
393 init_vlc(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
394 &h261_cbp_tab[0][1], 2, 1,
395 &h261_cbp_tab[0][0], 2, 1);
396 init_rl(&h261_rl_tcoeff);
397 init_vlc_rl(&h261_rl_tcoeff);
398 }
399 }
400
401 static int h261_decode_init(AVCodecContext *avctx){
402 H261Context *h= avctx->priv_data;
403 MpegEncContext * const s = &h->s;
404
405 // set defaults
406 MPV_decode_defaults(s);
407 s->avctx = avctx;
408
409 s->width = s->avctx->coded_width;
410 s->height = s->avctx->coded_height;
411 s->codec_id = s->avctx->codec->id;
412
413 s->out_format = FMT_H261;
414 s->low_delay= 1;
415 avctx->pix_fmt= PIX_FMT_YUV420P;
416
417 s->codec_id= avctx->codec->id;
418
419 h261_decode_init_vlc(h);
420
421 h->gob_start_code_skipped = 0;
422
423 return 0;
424 }
425
426 /**
427 * decodes the group of blocks header or slice header.
428 * @return <0 if an error occured
429 */
430 static int h261_decode_gob_header(H261Context *h){
431 unsigned int val;
432 MpegEncContext * const s = &h->s;
433
434 if ( !h->gob_start_code_skipped ){
435 /* Check for GOB Start Code */
436 val = show_bits(&s->gb, 15);
437 if(val)
438 return -1;
439
440 /* We have a GBSC */
441 skip_bits(&s->gb, 16);
442 }
443
444 h->gob_start_code_skipped = 0;
445
446 h->gob_number = get_bits(&s->gb, 4); /* GN */
447 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
448
449 /* Check if gob_number is valid */
450 if (s->mb_height==18){ //cif
451 if ((h->gob_number<=0) || (h->gob_number>12))
452 return -1;
453 }
454 else{ //qcif
455 if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
456 return -1;
457 }
458
459 /* GEI */
460 while (get_bits1(&s->gb) != 0) {
461 skip_bits(&s->gb, 8);
462 }
463
464 if(s->qscale==0)
465 return -1;
466
467 // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
468 // subsequent macroblocks, MBA is the difference between the absolute addresses of
469 // the macroblock and the last transmitted macroblock.
470 h->current_mba = 0;
471 h->mba_diff = 0;
472
473 return 0;
474 }
475
476 /**
477 * decodes the group of blocks / video packet header.
478 * @return <0 if no resync found
479 */
480 static int ff_h261_resync(H261Context *h){
481 MpegEncContext * const s = &h->s;
482 int left, ret;
483
484 if ( h->gob_start_code_skipped ){
485 ret= h261_decode_gob_header(h);
486 if(ret>=0)
487 return 0;
488 }
489 else{
490 if(show_bits(&s->gb, 15)==0){
491 ret= h261_decode_gob_header(h);
492 if(ret>=0)
493 return 0;
494 }
495 //ok, its not where its supposed to be ...
496 s->gb= s->last_resync_gb;
497 align_get_bits(&s->gb);
498 left= s->gb.size_in_bits - get_bits_count(&s->gb);
499
500 for(;left>15+1+4+5; left-=8){
501 if(show_bits(&s->gb, 15)==0){
502 GetBitContext bak= s->gb;
503
504 ret= h261_decode_gob_header(h);
505 if(ret>=0)
506 return 0;
507
508 s->gb= bak;
509 }
510 skip_bits(&s->gb, 8);
511 }
512 }
513
514 return -1;
515 }
516
517 /**
518 * decodes skipped macroblocks
519 * @return 0
520 */
521 static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
522 {
523 MpegEncContext * const s = &h->s;
524 int i;
525
526 s->mb_intra = 0;
527
528 for(i=mba1; i<mba2; i++){
529 int j, xy;
530
531 s->mb_x= ((h->gob_number-1) % 2) * 11 + i % 11;
532 s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11;
533 xy = s->mb_x + s->mb_y * s->mb_stride;
534 ff_init_block_index(s);
535 ff_update_block_index(s);
536 s->dsp.clear_blocks(s->block[0]);
537
538 for(j=0;j<6;j++)
539 s->block_last_index[j] = -1;
540
541 s->mv_dir = MV_DIR_FORWARD;
542 s->mv_type = MV_TYPE_16X16;
543 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
544 s->mv[0][0][0] = 0;
545 s->mv[0][0][1] = 0;
546 s->mb_skiped = 1;
547 h->mtype &= ~MB_TYPE_H261_FIL;
548
549 MPV_decode_mb(s, s->block);
550 }
551
552 return 0;
553 }
554
555 static int decode_mv_component(GetBitContext *gb, int v){
556 int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
557
558 /* check if mv_diff is valid */
559 if ( mv_diff < 0 )
560 return v;
561
562 mv_diff = mvmap[mv_diff];
563
564 if(mv_diff && !get_bits1(gb))
565 mv_diff= -mv_diff;
566
567 v += mv_diff;
568 if (v <=-16) v+= 32;
569 else if(v >= 16) v-= 32;
570
571 return v;
572 }
573
574 static int h261_decode_mb(H261Context *h){
575 MpegEncContext * const s = &h->s;
576 int i, cbp, xy;
577
578 cbp = 63;
579 // Read mba
580 do{
581 h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
582
583 /* Check for slice end */
584 /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
585 if (h->mba_diff == MBA_STARTCODE){ // start code
586 h->gob_start_code_skipped = 1;
587 return SLICE_END;
588 }
589 }
590 while( h->mba_diff == MBA_STUFFING ); // stuffing
591
592 if ( h->mba_diff < 0 ){
593 if ( get_bits_count(&s->gb) + 7 >= s->gb.size_in_bits )
594 return SLICE_END;
595
596 av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
597 return SLICE_ERROR;
598 }
599
600 h->mba_diff += 1;
601 h->current_mba += h->mba_diff;
602
603 if ( h->current_mba > MBA_STUFFING )
604 return SLICE_ERROR;
605
606 s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
607 s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
608 xy = s->mb_x + s->mb_y * s->mb_stride;
609 ff_init_block_index(s);
610 ff_update_block_index(s);
611 s->dsp.clear_blocks(s->block[0]);
612
613 // Read mtype
614 h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
615 h->mtype = h261_mtype_map[h->mtype];
616
617 // Read mquant
618 if ( IS_QUANT ( h->mtype ) ){
619 ff_set_qscale(s, get_bits(&s->gb, 5));
620 }
621
622 s->mb_intra = IS_INTRA4x4(h->mtype);
623
624 // Read mv
625 if ( IS_16X16 ( h->mtype ) ){
626 // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
627 // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
628 // following three situations:
629 // 1) evaluating MVD for macroblocks 1, 12 and 23;
630 // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
631 // 3) MTYPE of the previous macroblock was not MC.
632 if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
633 ( h->mba_diff != 1))
634 {
635 h->current_mv_x = 0;
636 h->current_mv_y = 0;
637 }
638
639 h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
640 h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
641 }else{
642 h->current_mv_x = 0;
643 h->current_mv_y = 0;
644 }
645
646 // Read cbp
647 if ( HAS_CBP( h->mtype ) ){
648 cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
649 }
650
651 if(s->mb_intra){
652 s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
653 goto intra;
654 }
655
656 //set motion vectors
657 s->mv_dir = MV_DIR_FORWARD;
658 s->mv_type = MV_TYPE_16X16;
659 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
660 s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
661 s->mv[0][0][1] = h->current_mv_y * 2;
662
663 intra:
664 /* decode each block */
665 if(s->mb_intra || HAS_CBP(h->mtype)){
666 for (i = 0; i < 6; i++) {
667 if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
668 return SLICE_ERROR;
669 }
670 cbp+=cbp;
671 }
672 }
673
674 MPV_decode_mb(s, s->block);
675
676 return SLICE_OK;
677 }
678
679 /**
680 * decodes a macroblock
681 * @return <0 if an error occured
682 */
683 static int h261_decode_block(H261Context * h, DCTELEM * block,
684 int n, int coded)
685 {
686 MpegEncContext * const s = &h->s;
687 int code, level, i, j, run;
688 RLTable *rl = &h261_rl_tcoeff;
689 const uint8_t *scan_table;
690
691 // For the variable length encoding there are two code tables, one being used for
692 // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
693 // for all other LEVELs except the first one in INTRA blocks which is fixed length
694 // coded with 8 bits.
695 // NOTE: the two code tables only differ in one VLC so we handle that manually.
696 scan_table = s->intra_scantable.permutated;
697 if (s->mb_intra){
698 /* DC coef */
699 level = get_bits(&s->gb, 8);
700 // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
701 if((level&0x7F) == 0){
702 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
703 return -1;
704 }
705 // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
706 if (level == 255)
707 level = 128;
708 block[0] = level;
709 i = 1;
710 }else if(coded){
711 // Run Level Code
712 // EOB Not possible for first level when cbp is available (that's why the table is different)
713 // 0 1 1s
714 // * * 0*
715 int check = show_bits(&s->gb, 2);
716 i = 0;
717 if ( check & 0x2 ){
718 skip_bits(&s->gb, 2);
719 block[0] = ( check & 0x1 ) ? -1 : 1;
720 i = 1;
721 }
722 }else{
723 i = 0;
724 }
725 if(!coded){
726 s->block_last_index[n] = i - 1;
727 return 0;
728 }
729 for(;;){
730 code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
731 if (code < 0){
732 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
733 return -1;
734 }
735 if (code == rl->n) {
736 /* escape */
737 // 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.
738 run = get_bits(&s->gb, 6);
739 level = get_sbits(&s->gb, 8);
740 }else if(code == 0){
741 break;
742 }else{
743 run = rl->table_run[code];
744 level = rl->table_level[code];
745 if (get_bits1(&s->gb))
746 level = -level;
747 }
748 i += run;
749 if (i >= 64){
750 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
751 return -1;
752 }
753 j = scan_table[i];
754 block[j] = level;
755 i++;
756 }
757 s->block_last_index[n] = i-1;
758 return 0;
759 }
760
761 /**
762 * decodes the H261 picture header.
763 * @return <0 if no startcode found
764 */
765 int h261_decode_picture_header(H261Context *h){
766 MpegEncContext * const s = &h->s;
767 int format, i;
768 uint32_t startcode= 0;
769
770 for(i= s->gb.size_in_bits - get_bits_count(&s->gb); i>24; i-=1){
771 startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
772
773 if(startcode == 0x10)
774 break;
775 }
776
777 if (startcode != 0x10){
778 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
779 return -1;
780 }
781
782 /* temporal reference */
783 s->picture_number = get_bits(&s->gb, 5); /* picture timestamp */
784
785 /* PTYPE starts here */
786 skip_bits1(&s->gb); /* split screen off */
787 skip_bits1(&s->gb); /* camera off */
788 skip_bits1(&s->gb); /* freeze picture release off */
789
790 format = get_bits1(&s->gb);
791
792 //only 2 formats possible
793 if (format == 0){//QCIF
794 s->width = 176;
795 s->height = 144;
796 s->mb_width = 11;
797 s->mb_height = 9;
798 }else{//CIF
799 s->width = 352;
800 s->height = 288;
801 s->mb_width = 22;
802 s->mb_height = 18;
803 }
804
805 s->mb_num = s->mb_width * s->mb_height;
806
807 skip_bits1(&s->gb); /* still image mode off */
808 skip_bits1(&s->gb); /* Reserved */
809
810 /* PEI */
811 while (get_bits1(&s->gb) != 0){
812 skip_bits(&s->gb, 8);
813 }
814
815 // h261 has no I-FRAMES, but if we pass I_TYPE for the first frame, the codec crashes if it does
816 // not contain all I-blocks (e.g. when a packet is lost)
817 s->pict_type = P_TYPE;
818
819 h->gob_number = 0;
820 return 0;
821 }
822
823 static int h261_decode_gob(H261Context *h){
824 MpegEncContext * const s = &h->s;
825
826 ff_set_qscale(s, s->qscale);
827
828 /* decode mb's */
829 while(h->current_mba <= MBA_STUFFING)
830 {
831 int ret;
832 /* DCT & quantize */
833 ret= h261_decode_mb(h);
834 if(ret<0){
835 if(ret==SLICE_END){
836 h261_decode_mb_skipped(h, h->current_mba, 33);
837 return 0;
838 }
839 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
840 return -1;
841 }
842
843 h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
844 }
845
846 return -1;
847 }
848
849 static int h261_find_frame_end(ParseContext *pc, AVCodecContext* avctx, const uint8_t *buf, int buf_size){
850 int vop_found, i, j, bits_left, last_bits;
851 uint32_t state;
852
853 H261Context *h = avctx->priv_data;
854
855 if(h){
856 bits_left = h->bits_left;
857 last_bits = h->last_bits;
858 }
859 else{
860 bits_left = 0;
861 last_bits = 0;
862 }
863
864 vop_found= pc->frame_start_found;
865 state= pc->state;
866 if(bits_left!=0 && !vop_found)
867 state = state << (8-bits_left) | last_bits;
868 i=0;
869 if(!vop_found){
870 for(i=0; i<buf_size; i++){
871 state= (state<<8) | buf[i];
872 for(j=0; j<8; j++){
873 if(( ( (state<<j) | (buf[i]>>(8-j)) )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
874 i++;
875 vop_found=1;
876 break;
877 }
878 }
879 if(vop_found)
880 break;
881 }
882 }
883 if(vop_found){
884 for(; i<buf_size; i++){
885 if(avctx->flags & CODEC_FLAG_TRUNCATED)//XXX ffplay workaround, someone a better solution?
886 state= (state<<8) | buf[i];
887 for(j=0; j<8; j++){
888 if(( ( (state<<j) | (buf[i]>>(8-j)) )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
889 pc->frame_start_found=0;
890 pc->state=-1;
891 return i-3;
892 }
893 }
894 }
895 }
896
897 pc->frame_start_found= vop_found;
898 pc->state= state;
899 return END_NOT_FOUND;
900 }
901
902 static int h261_parse(AVCodecParserContext *s,
903 AVCodecContext *avctx,
904 uint8_t **poutbuf, int *poutbuf_size,
905 const uint8_t *buf, int buf_size)
906 {
907 ParseContext *pc = s->priv_data;
908 int next;
909
910 next= h261_find_frame_end(pc,avctx, buf, buf_size);
911 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
912 *poutbuf = NULL;
913 *poutbuf_size = 0;
914 return buf_size;
915 }
916 *poutbuf = (uint8_t *)buf;
917 *poutbuf_size = buf_size;
918 return next;
919 }
920
921 /**
922 * returns the number of bytes consumed for building the current frame
923 */
924 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
925 if(s->flags&CODEC_FLAG_TRUNCATED){
926 int pos= (get_bits_count(&s->gb)+7)>>3;
927 pos -= s->parse_context.last_index;
928 if(pos<0) pos=0;// padding is not really read so this might be -1
929 return pos;
930 }else{
931 int pos= get_bits_count(&s->gb)>>3;
932 if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
933 if(pos+10>buf_size) pos=buf_size; // oops ;)
934
935 return pos;
936 }
937 }
938
939 static int h261_decode_frame(AVCodecContext *avctx,
940 void *data, int *data_size,
941 uint8_t *buf, int buf_size)
942 {
943 H261Context *h= avctx->priv_data;
944 MpegEncContext *s = &h->s;
945 int ret;
946 AVFrame *pict = data;
947
948 #ifdef DEBUG
949 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
950 printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
951 #endif
952 s->flags= avctx->flags;
953 s->flags2= avctx->flags2;
954
955 /* no supplementary picture */
956 if (buf_size == 0) {
957 return 0;
958 }
959
960 if(s->flags&CODEC_FLAG_TRUNCATED){
961 int next;
962
963 next= h261_find_frame_end(&s->parse_context,avctx, buf, buf_size);
964
965 if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
966 return buf_size;
967 }
968
969
970 retry:
971
972 init_get_bits(&s->gb, buf, buf_size*8);
973
974 if(!s->context_initialized){
975 if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
976 return -1;
977 }
978
979 //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
980 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
981 int i= ff_find_unused_picture(s, 0);
982 s->current_picture_ptr= &s->picture[i];
983 }
984
985 ret = h261_decode_picture_header(h);
986
987 /* skip if the header was thrashed */
988 if (ret < 0){
989 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
990 return -1;
991 }
992
993 if (s->width != avctx->coded_width || s->height != avctx->coded_height){
994 ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
995 s->parse_context.buffer=0;
996 MPV_common_end(s);
997 s->parse_context= pc;
998 }
999 if (!s->context_initialized) {
1000 avcodec_set_dimensions(avctx, s->width, s->height);
1001
1002 goto retry;
1003 }
1004
1005 // for hurry_up==5
1006 s->current_picture.pict_type= s->pict_type;
1007 s->current_picture.key_frame= s->pict_type == I_TYPE;
1008
1009 /* skip everything if we are in a hurry>=5 */
1010 if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
1011
1012 if(MPV_frame_start(s, avctx) < 0)
1013 return -1;
1014
1015 ff_er_frame_start(s);
1016
1017 /* decode each macroblock */
1018 s->mb_x=0;
1019 s->mb_y=0;
1020
1021 while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
1022 if(ff_h261_resync(h)<0)
1023 break;
1024 h261_decode_gob(h);
1025 }
1026 MPV_frame_end(s);
1027
1028 assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
1029 assert(s->current_picture.pict_type == s->pict_type);
1030 *pict= *(AVFrame*)&s->current_picture;
1031 ff_print_debug_info(s, pict);
1032
1033 /* Return the Picture timestamp as the frame number */
1034 /* we substract 1 because it is added on utils.c */
1035 avctx->frame_number = s->picture_number - 1;
1036
1037 *data_size = sizeof(AVFrame);
1038
1039 return get_consumed_bytes(s, buf_size);
1040 }
1041
1042 static int h261_decode_end(AVCodecContext *avctx)
1043 {
1044 H261Context *h= avctx->priv_data;
1045 MpegEncContext *s = &h->s;
1046
1047 MPV_common_end(s);
1048 return 0;
1049 }
1050
1051 AVCodec h261_encoder = {
1052 "h261",
1053 CODEC_TYPE_VIDEO,
1054 CODEC_ID_H261,
1055 sizeof(H261Context),
1056 MPV_encode_init,
1057 MPV_encode_picture,
1058 MPV_encode_end,
1059 };
1060
1061 AVCodec h261_decoder = {
1062 "h261",
1063 CODEC_TYPE_VIDEO,
1064 CODEC_ID_H261,
1065 sizeof(H261Context),
1066 h261_decode_init,
1067 NULL,
1068 h261_decode_end,
1069 h261_decode_frame,
1070 CODEC_CAP_TRUNCATED,
1071 };
1072
1073 AVCodecParser h261_parser = {
1074 { CODEC_ID_H261 },
1075 sizeof(ParseContext),
1076 NULL,
1077 h261_parse,
1078 ff_parse_close,
1079 };