simplify ff_h261_reorder_mb_index
[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,1,1);
329 put_bits(&s->pb,1,block[0]>0 ? 0 : 1 );
330 i = 1;
331 } else {
332 i = 0;
333 }
334
335 /* AC coefs */
336 last_index = s->block_last_index[n];
337 last_non_zero = i - 1;
338 for (; i <= last_index; i++) {
339 j = s->intra_scantable.permutated[i];
340 level = block[j];
341 if (level) {
342 run = i - last_non_zero - 1;
343 last = (i == last_index);
344 sign = 0;
345 slevel = level;
346 if (level < 0) {
347 sign = 1;
348 level = -level;
349 }
350 code = get_rl_index(rl, 0 /*no last in H.261, EOB is used*/, run, level);
351 if(run==0 && level < 16)
352 code+=1;
353 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
354 if (code == rl->n) {
355 put_bits(&s->pb, 6, run);
356 assert(slevel != 0);
357 if(slevel < -127){
358 slevel = -127;
359 }
360 else if(slevel > 127){
361 slevel = 127;
362 }
363 put_bits(&s->pb, 8, slevel & 0xff);
364 } else {
365 put_bits(&s->pb, 1, sign);
366 }
367 last_non_zero = i;
368 }
369 }
370 if(last_index > -1){
371 put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]);// END OF BLOCK
372 }
373 }
374
375 /***********************************************/
376 /* decoding */
377
378 static VLC h261_mba_vlc;
379 static VLC h261_mtype_vlc;
380 static VLC h261_mv_vlc;
381 static VLC h261_cbp_vlc;
382
383 void init_vlc_rl(RLTable *rl);
384
385 static void h261_decode_init_vlc(H261Context *h){
386 static int done = 0;
387
388 if(!done){
389 done = 1;
390 init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
391 h261_mba_bits, 1, 1,
392 h261_mba_code, 1, 1);
393 init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
394 h261_mtype_bits, 1, 1,
395 h261_mtype_code, 1, 1);
396 init_vlc(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
397 &h261_mv_tab[0][1], 2, 1,
398 &h261_mv_tab[0][0], 2, 1);
399 init_vlc(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
400 &h261_cbp_tab[0][1], 2, 1,
401 &h261_cbp_tab[0][0], 2, 1);
402 init_rl(&h261_rl_tcoeff);
403 init_vlc_rl(&h261_rl_tcoeff);
404 }
405 }
406
407 static int h261_decode_init(AVCodecContext *avctx){
408 H261Context *h= avctx->priv_data;
409 MpegEncContext * const s = &h->s;
410
411 // set defaults
412 MPV_decode_defaults(s);
413 s->avctx = avctx;
414
415 s->width = s->avctx->coded_width;
416 s->height = s->avctx->coded_height;
417 s->codec_id = s->avctx->codec->id;
418
419 s->out_format = FMT_H261;
420 s->low_delay= 1;
421 avctx->pix_fmt= PIX_FMT_YUV420P;
422
423 s->codec_id= avctx->codec->id;
424
425 h261_decode_init_vlc(h);
426
427 h->gob_start_code_skipped = 0;
428
429 return 0;
430 }
431
432 /**
433 * decodes the group of blocks header or slice header.
434 * @return <0 if an error occured
435 */
436 static int h261_decode_gob_header(H261Context *h){
437 unsigned int val;
438 MpegEncContext * const s = &h->s;
439
440 if ( !h->gob_start_code_skipped ){
441 /* Check for GOB Start Code */
442 val = show_bits(&s->gb, 15);
443 if(val)
444 return -1;
445
446 /* We have a GBSC */
447 skip_bits(&s->gb, 16);
448 }
449
450 h->gob_start_code_skipped = 0;
451
452 h->gob_number = get_bits(&s->gb, 4); /* GN */
453 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
454
455 /* Check if gob_number is valid */
456 if (s->mb_height==18){ //cif
457 if ((h->gob_number<=0) || (h->gob_number>12))
458 return -1;
459 }
460 else{ //qcif
461 if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
462 return -1;
463 }
464
465 /* GEI */
466 while (get_bits1(&s->gb) != 0) {
467 skip_bits(&s->gb, 8);
468 }
469
470 if(s->qscale==0)
471 return -1;
472
473 // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
474 // subsequent macroblocks, MBA is the difference between the absolute addresses of
475 // the macroblock and the last transmitted macroblock.
476 h->current_mba = 0;
477 h->mba_diff = 0;
478
479 return 0;
480 }
481
482 /**
483 * decodes the group of blocks / video packet header.
484 * @return <0 if no resync found
485 */
486 static int ff_h261_resync(H261Context *h){
487 MpegEncContext * const s = &h->s;
488 int left, ret;
489
490 if ( h->gob_start_code_skipped ){
491 ret= h261_decode_gob_header(h);
492 if(ret>=0)
493 return 0;
494 }
495 else{
496 if(show_bits(&s->gb, 15)==0){
497 ret= h261_decode_gob_header(h);
498 if(ret>=0)
499 return 0;
500 }
501 //ok, its not where its supposed to be ...
502 s->gb= s->last_resync_gb;
503 align_get_bits(&s->gb);
504 left= s->gb.size_in_bits - get_bits_count(&s->gb);
505
506 for(;left>15+1+4+5; left-=8){
507 if(show_bits(&s->gb, 15)==0){
508 GetBitContext bak= s->gb;
509
510 ret= h261_decode_gob_header(h);
511 if(ret>=0)
512 return 0;
513
514 s->gb= bak;
515 }
516 skip_bits(&s->gb, 8);
517 }
518 }
519
520 return -1;
521 }
522
523 /**
524 * decodes skipped macroblocks
525 * @return 0
526 */
527 static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
528 {
529 MpegEncContext * const s = &h->s;
530 int i;
531
532 s->mb_intra = 0;
533
534 for(i=mba1; i<mba2; i++){
535 int j, xy;
536
537 s->mb_x= ((h->gob_number-1) % 2) * 11 + i % 11;
538 s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11;
539 xy = s->mb_x + s->mb_y * s->mb_stride;
540 ff_init_block_index(s);
541 ff_update_block_index(s);
542 s->dsp.clear_blocks(s->block[0]);
543
544 for(j=0;j<6;j++)
545 s->block_last_index[j] = -1;
546
547 s->mv_dir = MV_DIR_FORWARD;
548 s->mv_type = MV_TYPE_16X16;
549 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
550 s->mv[0][0][0] = 0;
551 s->mv[0][0][1] = 0;
552 s->mb_skiped = 1;
553 h->mtype &= ~MB_TYPE_H261_FIL;
554
555 MPV_decode_mb(s, s->block);
556 }
557
558 return 0;
559 }
560
561 static int decode_mv_component(GetBitContext *gb, int v){
562 int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
563
564 /* check if mv_diff is valid */
565 if ( mv_diff < 0 )
566 return v;
567
568 mv_diff = mvmap[mv_diff];
569
570 if(mv_diff && !get_bits1(gb))
571 mv_diff= -mv_diff;
572
573 v += mv_diff;
574 if (v <=-16) v+= 32;
575 else if(v >= 16) v-= 32;
576
577 return v;
578 }
579
580 static int h261_decode_mb(H261Context *h){
581 MpegEncContext * const s = &h->s;
582 int i, cbp, xy;
583
584 cbp = 63;
585 // Read mba
586 do{
587 h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
588
589 /* Check for slice end */
590 /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
591 if (h->mba_diff == MBA_STARTCODE){ // start code
592 h->gob_start_code_skipped = 1;
593 return SLICE_END;
594 }
595 }
596 while( h->mba_diff == MBA_STUFFING ); // stuffing
597
598 if ( h->mba_diff < 0 ){
599 if ( get_bits_count(&s->gb) + 7 >= s->gb.size_in_bits )
600 return SLICE_END;
601
602 av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
603 return SLICE_ERROR;
604 }
605
606 h->mba_diff += 1;
607 h->current_mba += h->mba_diff;
608
609 if ( h->current_mba > MBA_STUFFING )
610 return SLICE_ERROR;
611
612 s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
613 s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
614 xy = s->mb_x + s->mb_y * s->mb_stride;
615 ff_init_block_index(s);
616 ff_update_block_index(s);
617 s->dsp.clear_blocks(s->block[0]);
618
619 // Read mtype
620 h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
621 h->mtype = h261_mtype_map[h->mtype];
622
623 // Read mquant
624 if ( IS_QUANT ( h->mtype ) ){
625 ff_set_qscale(s, get_bits(&s->gb, 5));
626 }
627
628 s->mb_intra = IS_INTRA4x4(h->mtype);
629
630 // Read mv
631 if ( IS_16X16 ( h->mtype ) ){
632 // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
633 // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
634 // following three situations:
635 // 1) evaluating MVD for macroblocks 1, 12 and 23;
636 // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
637 // 3) MTYPE of the previous macroblock was not MC.
638 if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
639 ( h->mba_diff != 1))
640 {
641 h->current_mv_x = 0;
642 h->current_mv_y = 0;
643 }
644
645 h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
646 h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
647 }else{
648 h->current_mv_x = 0;
649 h->current_mv_y = 0;
650 }
651
652 // Read cbp
653 if ( HAS_CBP( h->mtype ) ){
654 cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
655 }
656
657 if(s->mb_intra){
658 s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
659 goto intra;
660 }
661
662 //set motion vectors
663 s->mv_dir = MV_DIR_FORWARD;
664 s->mv_type = MV_TYPE_16X16;
665 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
666 s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
667 s->mv[0][0][1] = h->current_mv_y * 2;
668
669 intra:
670 /* decode each block */
671 if(s->mb_intra || HAS_CBP(h->mtype)){
672 for (i = 0; i < 6; i++) {
673 if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
674 return SLICE_ERROR;
675 }
676 cbp+=cbp;
677 }
678 }
679
680 MPV_decode_mb(s, s->block);
681
682 return SLICE_OK;
683 }
684
685 /**
686 * decodes a macroblock
687 * @return <0 if an error occured
688 */
689 static int h261_decode_block(H261Context * h, DCTELEM * block,
690 int n, int coded)
691 {
692 MpegEncContext * const s = &h->s;
693 int code, level, i, j, run;
694 RLTable *rl = &h261_rl_tcoeff;
695 const uint8_t *scan_table;
696
697 // For the variable length encoding there are two code tables, one being used for
698 // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
699 // for all other LEVELs except the first one in INTRA blocks which is fixed length
700 // coded with 8 bits.
701 // NOTE: the two code tables only differ in one VLC so we handle that manually.
702 scan_table = s->intra_scantable.permutated;
703 if (s->mb_intra){
704 /* DC coef */
705 level = get_bits(&s->gb, 8);
706 // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
707 if((level&0x7F) == 0){
708 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
709 return -1;
710 }
711 // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
712 if (level == 255)
713 level = 128;
714 block[0] = level;
715 i = 1;
716 }else if(coded){
717 // Run Level Code
718 // EOB Not possible for first level when cbp is available (that's why the table is different)
719 // 0 1 1s
720 // * * 0*
721 int check = show_bits(&s->gb, 2);
722 i = 0;
723 if ( check & 0x2 ){
724 skip_bits(&s->gb, 2);
725 block[0] = ( check & 0x1 ) ? -1 : 1;
726 i = 1;
727 }
728 }else{
729 i = 0;
730 }
731 if(!coded){
732 s->block_last_index[n] = i - 1;
733 return 0;
734 }
735 for(;;){
736 code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
737 if (code < 0){
738 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
739 return -1;
740 }
741 if (code == rl->n) {
742 /* escape */
743 // 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.
744 run = get_bits(&s->gb, 6);
745 level = (int8_t)get_bits(&s->gb, 8);
746 }else if(code == 0){
747 break;
748 }else{
749 run = rl->table_run[code];
750 level = rl->table_level[code];
751 if (get_bits1(&s->gb))
752 level = -level;
753 }
754 i += run;
755 if (i >= 64){
756 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
757 return -1;
758 }
759 j = scan_table[i];
760 block[j] = level;
761 i++;
762 }
763 s->block_last_index[n] = i-1;
764 return 0;
765 }
766
767 /**
768 * decodes the H261 picture header.
769 * @return <0 if no startcode found
770 */
771 int h261_decode_picture_header(H261Context *h){
772 MpegEncContext * const s = &h->s;
773 int format, i;
774 uint32_t startcode= 0;
775
776 for(i= s->gb.size_in_bits - get_bits_count(&s->gb); i>24; i-=1){
777 startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
778
779 if(startcode == 0x10)
780 break;
781 }
782
783 if (startcode != 0x10){
784 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
785 return -1;
786 }
787
788 /* temporal reference */
789 s->picture_number = get_bits(&s->gb, 5); /* picture timestamp */
790
791 /* PTYPE starts here */
792 skip_bits1(&s->gb); /* split screen off */
793 skip_bits1(&s->gb); /* camera off */
794 skip_bits1(&s->gb); /* freeze picture release off */
795
796 format = get_bits1(&s->gb);
797
798 //only 2 formats possible
799 if (format == 0){//QCIF
800 s->width = 176;
801 s->height = 144;
802 s->mb_width = 11;
803 s->mb_height = 9;
804 }else{//CIF
805 s->width = 352;
806 s->height = 288;
807 s->mb_width = 22;
808 s->mb_height = 18;
809 }
810
811 s->mb_num = s->mb_width * s->mb_height;
812
813 skip_bits1(&s->gb); /* still image mode off */
814 skip_bits1(&s->gb); /* Reserved */
815
816 /* PEI */
817 while (get_bits1(&s->gb) != 0){
818 skip_bits(&s->gb, 8);
819 }
820
821 // h261 has no I-FRAMES, but if we pass I_TYPE for the first frame, the codec crashes if it does
822 // not contain all I-blocks (e.g. when a packet is lost)
823 s->pict_type = P_TYPE;
824
825 h->gob_number = 0;
826 return 0;
827 }
828
829 static int h261_decode_gob(H261Context *h){
830 MpegEncContext * const s = &h->s;
831
832 ff_set_qscale(s, s->qscale);
833
834 /* decode mb's */
835 while(h->current_mba <= MBA_STUFFING)
836 {
837 int ret;
838 /* DCT & quantize */
839 ret= h261_decode_mb(h);
840 if(ret<0){
841 if(ret==SLICE_END){
842 h261_decode_mb_skipped(h, h->current_mba, 33);
843 return 0;
844 }
845 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
846 return -1;
847 }
848
849 h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
850 }
851
852 return -1;
853 }
854
855 static int h261_find_frame_end(ParseContext *pc, AVCodecContext* avctx, const uint8_t *buf, int buf_size){
856 int vop_found, i, j, bits_left, last_bits;
857 uint32_t state;
858
859 H261Context *h = avctx->priv_data;
860
861 if(h){
862 bits_left = h->bits_left;
863 last_bits = h->last_bits;
864 }
865 else{
866 bits_left = 0;
867 last_bits = 0;
868 }
869
870 vop_found= pc->frame_start_found;
871 state= pc->state;
872 if(bits_left!=0 && !vop_found)
873 state = state << (8-bits_left) | last_bits;
874 i=0;
875 if(!vop_found){
876 for(i=0; i<buf_size; i++){
877 state= (state<<8) | buf[i];
878 for(j=0; j<8; j++){
879 if(( ( (state<<j) | (buf[i]>>(8-j)) )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
880 i++;
881 vop_found=1;
882 break;
883 }
884 }
885 if(vop_found)
886 break;
887 }
888 }
889 if(vop_found){
890 for(; i<buf_size; i++){
891 if(avctx->flags & CODEC_FLAG_TRUNCATED)//XXX ffplay workaround, someone a better solution?
892 state= (state<<8) | buf[i];
893 for(j=0; j<8; j++){
894 if(( ( (state<<j) | (buf[i]>>(8-j)) )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
895 pc->frame_start_found=0;
896 pc->state=-1;
897 return i-3;
898 }
899 }
900 }
901 }
902
903 pc->frame_start_found= vop_found;
904 pc->state= state;
905 return END_NOT_FOUND;
906 }
907
908 static int h261_parse(AVCodecParserContext *s,
909 AVCodecContext *avctx,
910 uint8_t **poutbuf, int *poutbuf_size,
911 const uint8_t *buf, int buf_size)
912 {
913 ParseContext *pc = s->priv_data;
914 int next;
915
916 next= h261_find_frame_end(pc,avctx, buf, buf_size);
917 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
918 *poutbuf = NULL;
919 *poutbuf_size = 0;
920 return buf_size;
921 }
922 *poutbuf = (uint8_t *)buf;
923 *poutbuf_size = buf_size;
924 return next;
925 }
926
927 /**
928 * returns the number of bytes consumed for building the current frame
929 */
930 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
931 if(s->flags&CODEC_FLAG_TRUNCATED){
932 int pos= (get_bits_count(&s->gb)+7)>>3;
933 pos -= s->parse_context.last_index;
934 if(pos<0) pos=0;// padding is not really read so this might be -1
935 return pos;
936 }else{
937 int pos= get_bits_count(&s->gb)>>3;
938 if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
939 if(pos+10>buf_size) pos=buf_size; // oops ;)
940
941 return pos;
942 }
943 }
944
945 static int h261_decode_frame(AVCodecContext *avctx,
946 void *data, int *data_size,
947 uint8_t *buf, int buf_size)
948 {
949 H261Context *h= avctx->priv_data;
950 MpegEncContext *s = &h->s;
951 int ret;
952 AVFrame *pict = data;
953
954 #ifdef DEBUG
955 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
956 printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
957 #endif
958 s->flags= avctx->flags;
959 s->flags2= avctx->flags2;
960
961 /* no supplementary picture */
962 if (buf_size == 0) {
963 return 0;
964 }
965
966 if(s->flags&CODEC_FLAG_TRUNCATED){
967 int next;
968
969 next= h261_find_frame_end(&s->parse_context,avctx, buf, buf_size);
970
971 if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
972 return buf_size;
973 }
974
975
976 retry:
977
978 init_get_bits(&s->gb, buf, buf_size*8);
979
980 if(!s->context_initialized){
981 if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
982 return -1;
983 }
984
985 //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
986 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
987 int i= ff_find_unused_picture(s, 0);
988 s->current_picture_ptr= &s->picture[i];
989 }
990
991 ret = h261_decode_picture_header(h);
992
993 /* skip if the header was thrashed */
994 if (ret < 0){
995 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
996 return -1;
997 }
998
999 if (s->width != avctx->coded_width || s->height != avctx->coded_height){
1000 ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
1001 s->parse_context.buffer=0;
1002 MPV_common_end(s);
1003 s->parse_context= pc;
1004 }
1005 if (!s->context_initialized) {
1006 avcodec_set_dimensions(avctx, s->width, s->height);
1007
1008 goto retry;
1009 }
1010
1011 // for hurry_up==5
1012 s->current_picture.pict_type= s->pict_type;
1013 s->current_picture.key_frame= s->pict_type == I_TYPE;
1014
1015 /* skip everything if we are in a hurry>=5 */
1016 if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
1017
1018 if(MPV_frame_start(s, avctx) < 0)
1019 return -1;
1020
1021 ff_er_frame_start(s);
1022
1023 /* decode each macroblock */
1024 s->mb_x=0;
1025 s->mb_y=0;
1026
1027 while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
1028 if(ff_h261_resync(h)<0)
1029 break;
1030 h261_decode_gob(h);
1031 }
1032 MPV_frame_end(s);
1033
1034 assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
1035 assert(s->current_picture.pict_type == s->pict_type);
1036 *pict= *(AVFrame*)&s->current_picture;
1037 ff_print_debug_info(s, pict);
1038
1039 /* Return the Picture timestamp as the frame number */
1040 /* we substract 1 because it is added on utils.c */
1041 avctx->frame_number = s->picture_number - 1;
1042
1043 *data_size = sizeof(AVFrame);
1044
1045 return get_consumed_bytes(s, buf_size);
1046 }
1047
1048 static int h261_decode_end(AVCodecContext *avctx)
1049 {
1050 H261Context *h= avctx->priv_data;
1051 MpegEncContext *s = &h->s;
1052
1053 MPV_common_end(s);
1054 return 0;
1055 }
1056
1057 AVCodec h261_encoder = {
1058 "h261",
1059 CODEC_TYPE_VIDEO,
1060 CODEC_ID_H261,
1061 sizeof(H261Context),
1062 MPV_encode_init,
1063 MPV_encode_picture,
1064 MPV_encode_end,
1065 };
1066
1067 AVCodec h261_decoder = {
1068 "h261",
1069 CODEC_TYPE_VIDEO,
1070 CODEC_ID_H261,
1071 sizeof(H261Context),
1072 h261_decode_init,
1073 NULL,
1074 h261_decode_end,
1075 h261_decode_frame,
1076 CODEC_CAP_TRUNCATED,
1077 };
1078
1079 AVCodecParser h261_parser = {
1080 { CODEC_ID_H261 },
1081 sizeof(ParseContext),
1082 NULL,
1083 h261_parse,
1084 ff_parse_close,
1085 };