You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

803 lines
22KB

  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. * @file h261.c
  22. * h261codec.
  23. */
  24. #include "common.h"
  25. #include "dsputil.h"
  26. #include "avcodec.h"
  27. #include "mpegvideo.h"
  28. #include "h261data.h"
  29. #define H261_MBA_VLC_BITS 9
  30. #define H261_MTYPE_VLC_BITS 6
  31. #define H261_MV_VLC_BITS 7
  32. #define H261_CBP_VLC_BITS 9
  33. #define TCOEFF_VLC_BITS 9
  34. #define MAX_MBA 33
  35. #define IS_FIL(a) ((a)&MB_TYPE_H261_FIL)
  36. /**
  37. * H261Context
  38. */
  39. typedef struct H261Context{
  40. MpegEncContext s;
  41. int current_mba;
  42. int mba_diff;
  43. int mtype;
  44. int current_mv_x;
  45. int current_mv_y;
  46. int gob_number;
  47. int loop_filter;
  48. int bits_left; //8 - nr of bits left of the following frame in the last byte in this frame
  49. int last_bits; //bits left of the following frame in the last byte in this frame
  50. }H261Context;
  51. void ff_h261_loop_filter(H261Context * h){
  52. MpegEncContext * const s = &h->s;
  53. const int linesize = s->linesize;
  54. const int uvlinesize= s->uvlinesize;
  55. uint8_t *dest_y = s->dest[0];
  56. uint8_t *dest_cb= s->dest[1];
  57. uint8_t *dest_cr= s->dest[2];
  58. s->dsp.h261_loop_filter(dest_y , linesize);
  59. s->dsp.h261_loop_filter(dest_y + 8, linesize);
  60. s->dsp.h261_loop_filter(dest_y + 8 * linesize , linesize);
  61. s->dsp.h261_loop_filter(dest_y + 8 * linesize + 8, linesize);
  62. s->dsp.h261_loop_filter(dest_cb, uvlinesize);
  63. s->dsp.h261_loop_filter(dest_cr, uvlinesize);
  64. }
  65. static int h261_decode_block(H261Context *h, DCTELEM *block,
  66. int n, int coded);
  67. static int h261_decode_mb(H261Context *h,
  68. DCTELEM block[6][64]);
  69. void ff_set_qscale(MpegEncContext * s, int qscale);
  70. /***********************************************/
  71. /* decoding */
  72. static VLC h261_mba_vlc;
  73. static VLC h261_mtype_vlc;
  74. static VLC h261_mv_vlc;
  75. static VLC h261_cbp_vlc;
  76. void init_vlc_rl(RLTable *rl);
  77. static void h261_decode_init_vlc(H261Context *h){
  78. static int done = 0;
  79. if(!done){
  80. done = 1;
  81. init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 34,
  82. h261_mba_bits, 1, 1,
  83. h261_mba_code, 1, 1);
  84. init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
  85. h261_mtype_bits, 1, 1,
  86. h261_mtype_code, 1, 1);
  87. init_vlc(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
  88. &h261_mv_tab[0][1], 2, 1,
  89. &h261_mv_tab[0][0], 2, 1);
  90. init_vlc(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
  91. &h261_cbp_tab[0][1], 2, 1,
  92. &h261_cbp_tab[0][0], 2, 1);
  93. init_rl(&h261_rl_tcoeff);
  94. init_vlc_rl(&h261_rl_tcoeff);
  95. }
  96. }
  97. static int h261_decode_init(AVCodecContext *avctx){
  98. H261Context *h= avctx->priv_data;
  99. MpegEncContext * const s = &h->s;
  100. // set defaults
  101. MPV_decode_defaults(s);
  102. s->avctx = avctx;
  103. s->width = s->avctx->width;
  104. s->height = s->avctx->height;
  105. s->codec_id = s->avctx->codec->id;
  106. s->out_format = FMT_H261;
  107. s->low_delay= 1;
  108. avctx->pix_fmt= PIX_FMT_YUV420P;
  109. s->codec_id= avctx->codec->id;
  110. h261_decode_init_vlc(h);
  111. h->bits_left = 0;
  112. h->last_bits = 0;
  113. return 0;
  114. }
  115. /**
  116. * decodes the group of blocks header or slice header.
  117. * @return <0 if an error occured
  118. */
  119. static int h261_decode_gob_header(H261Context *h){
  120. unsigned int val;
  121. MpegEncContext * const s = &h->s;
  122. /* Check for GOB Start Code */
  123. val = show_bits(&s->gb, 15);
  124. if(val)
  125. return -1;
  126. /* We have a GBSC */
  127. skip_bits(&s->gb, 16);
  128. h->gob_number = get_bits(&s->gb, 4); /* GN */
  129. s->qscale = get_bits(&s->gb, 5); /* GQUANT */
  130. /* GEI */
  131. while (get_bits1(&s->gb) != 0) {
  132. skip_bits(&s->gb, 8);
  133. }
  134. if(s->qscale==0)
  135. return -1;
  136. // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
  137. // subsequent macroblocks, MBA is the difference between the absolute addresses of
  138. // the macroblock and the last transmitted macroblock.
  139. h->current_mba = 0;
  140. h->mba_diff = 0;
  141. return 0;
  142. }
  143. /**
  144. * decodes the group of blocks / video packet header.
  145. * @return <0 if no resync found
  146. */
  147. static int ff_h261_resync(H261Context *h){
  148. MpegEncContext * const s = &h->s;
  149. int left, ret;
  150. if(show_bits(&s->gb, 15)==0){
  151. ret= h261_decode_gob_header(h);
  152. if(ret>=0)
  153. return 0;
  154. }
  155. //ok, its not where its supposed to be ...
  156. s->gb= s->last_resync_gb;
  157. align_get_bits(&s->gb);
  158. left= s->gb.size_in_bits - get_bits_count(&s->gb);
  159. for(;left>15+1+4+5; left-=8){
  160. if(show_bits(&s->gb, 15)==0){
  161. GetBitContext bak= s->gb;
  162. ret= h261_decode_gob_header(h);
  163. if(ret>=0)
  164. return 0;
  165. s->gb= bak;
  166. }
  167. skip_bits(&s->gb, 8);
  168. }
  169. return -1;
  170. }
  171. /**
  172. * decodes a skipped macroblock, called when when mba_diff > 1.
  173. * @return 0
  174. */
  175. static int h261_decode_mb_skipped(H261Context *h,
  176. DCTELEM block[6][64])
  177. {
  178. MpegEncContext * const s = &h->s;
  179. int i;
  180. const int xy = s->mb_x + s->mb_y * s->mb_stride;
  181. s->mb_intra = 0;
  182. for(i=0;i<6;i++)
  183. s->block_last_index[i] = -1;
  184. s->mv_dir = MV_DIR_FORWARD;
  185. s->mv_type = MV_TYPE_16X16;
  186. s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
  187. s->mv[0][0][0] = 0;
  188. s->mv[0][0][1] = 0;
  189. s->mb_skiped = 1;
  190. return 0;
  191. }
  192. static int decode_mv_component(GetBitContext *gb, int v){
  193. int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
  194. mv_diff = mvmap[mv_diff];
  195. if(mv_diff && !get_bits1(gb))
  196. mv_diff= -mv_diff;
  197. v += mv_diff;
  198. if (v <=-16) v+= 32;
  199. else if(v >= 16) v-= 32;
  200. return v;
  201. }
  202. static int h261_decode_mb(H261Context *h,
  203. DCTELEM block[6][64])
  204. {
  205. MpegEncContext * const s = &h->s;
  206. int i, cbp, xy, old_mtype;
  207. cbp = 63;
  208. // Read mba
  209. do{
  210. h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2)+1;
  211. }
  212. while( h->mba_diff == MAX_MBA + 1 ); // stuffing
  213. if ( h->mba_diff < 0 )
  214. return -1;
  215. h->current_mba += h->mba_diff;
  216. if ( h->current_mba > MAX_MBA )
  217. return -1;
  218. s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
  219. s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
  220. xy = s->mb_x + s->mb_y * s->mb_stride;
  221. ff_init_block_index(s);
  222. ff_update_block_index(s);
  223. // Read mtype
  224. old_mtype = h->mtype;
  225. h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
  226. h->mtype = h261_mtype_map[h->mtype];
  227. if (IS_FIL (h->mtype))
  228. h->loop_filter = 1;
  229. // Read mquant
  230. if ( IS_QUANT ( h->mtype ) ){
  231. s->qscale = get_bits(&s->gb, 5);
  232. }
  233. s->mb_intra = IS_INTRA4x4(h->mtype);
  234. // Read mv
  235. if ( IS_16X16 ( h->mtype ) ){
  236. // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
  237. // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
  238. // following three situations:
  239. // 1) evaluating MVD for macroblocks 1, 12 and 23;
  240. // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
  241. // 3) MTYPE of the previous macroblock was not MC.
  242. if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
  243. ( h->mba_diff != 1) || ( !IS_16X16 ( old_mtype ) ))
  244. {
  245. h->current_mv_x = 0;
  246. h->current_mv_y = 0;
  247. }
  248. h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
  249. h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
  250. }
  251. // Read cbp
  252. if ( HAS_CBP( h->mtype ) ){
  253. cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
  254. }
  255. if(s->mb_intra){
  256. s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
  257. goto intra;
  258. }
  259. //set motion vectors
  260. s->mv_dir = MV_DIR_FORWARD;
  261. s->mv_type = MV_TYPE_16X16;
  262. s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
  263. if(IS_16X16 ( h->mtype )){
  264. s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
  265. s->mv[0][0][1] = h->current_mv_y * 2;
  266. }
  267. else{
  268. h->current_mv_x = s->mv[0][0][0] = 0;
  269. h->current_mv_x = s->mv[0][0][1] = 0;
  270. }
  271. intra:
  272. /* decode each block */
  273. if(s->mb_intra || HAS_CBP(h->mtype)){
  274. for (i = 0; i < 6; i++) {
  275. if (h261_decode_block(h, block[i], i, cbp&32) < 0){
  276. return -1;
  277. }
  278. cbp+=cbp;
  279. }
  280. }
  281. /* per-MB end of slice check */
  282. {
  283. int v= show_bits(&s->gb, 15);
  284. if(get_bits_count(&s->gb) + 15 > s->gb.size_in_bits){
  285. v>>= get_bits_count(&s->gb) + 15 - s->gb.size_in_bits;
  286. }
  287. if(v==0){
  288. return SLICE_END;
  289. }
  290. }
  291. return SLICE_OK;
  292. }
  293. /**
  294. * decodes a macroblock
  295. * @return <0 if an error occured
  296. */
  297. static int h261_decode_block(H261Context * h, DCTELEM * block,
  298. int n, int coded)
  299. {
  300. MpegEncContext * const s = &h->s;
  301. int code, level, i, j, run;
  302. RLTable *rl = &h261_rl_tcoeff;
  303. const uint8_t *scan_table;
  304. // For the variable length encoding there are two code tables, one being used for
  305. // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
  306. // for all other LEVELs except the first one in INTRA blocks which is fixed length
  307. // coded with 8 bits.
  308. // NOTE: the two code tables only differ in one VLC so we handle that manually.
  309. scan_table = s->intra_scantable.permutated;
  310. if (s->mb_intra){
  311. /* DC coef */
  312. level = get_bits(&s->gb, 8);
  313. // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
  314. if((level&0x7F) == 0){
  315. av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
  316. return -1;
  317. }
  318. // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
  319. if (level == 255)
  320. level = 128;
  321. block[0] = level;
  322. i = 1;
  323. }else if(coded){
  324. // Run Level Code
  325. // EOB Not possible for first level when cbp is available (that's why the table is different)
  326. // 0 1 1s
  327. // * * 0*
  328. int check = show_bits(&s->gb, 2);
  329. i = 0;
  330. if ( check & 0x2 ){
  331. skip_bits(&s->gb, 2);
  332. block[0] = ( check & 0x1 ) ? -1 : 1;
  333. i = 1;
  334. }
  335. }else{
  336. i = 0;
  337. }
  338. if(!coded){
  339. s->block_last_index[n] = i - 1;
  340. return 0;
  341. }
  342. for(;;){
  343. code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
  344. if (code < 0){
  345. av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
  346. return -1;
  347. }
  348. if (code == rl->n) {
  349. /* escape */
  350. // 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.
  351. run = get_bits(&s->gb, 6);
  352. level = (int8_t)get_bits(&s->gb, 8);
  353. }else if(code == 0){
  354. break;
  355. }else{
  356. run = rl->table_run[code];
  357. level = rl->table_level[code];
  358. if (get_bits1(&s->gb))
  359. level = -level;
  360. }
  361. i += run;
  362. if (i >= 64){
  363. av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
  364. return -1;
  365. }
  366. j = scan_table[i];
  367. block[j] = level;
  368. i++;
  369. }
  370. s->block_last_index[n] = i;
  371. return 0;
  372. }
  373. /**
  374. * decodes the H261 picture header.
  375. * @return <0 if no startcode found
  376. */
  377. int h261_decode_picture_header(H261Context *h){
  378. MpegEncContext * const s = &h->s;
  379. int format, i;
  380. static int h261_framecounter = 0;
  381. uint32_t startcode;
  382. align_get_bits(&s->gb);
  383. startcode = (h->last_bits << (12 - (8-h->bits_left))) | get_bits(&s->gb, 20-8 - (8- h->bits_left));
  384. for(i= s->gb.size_in_bits - get_bits_count(&s->gb); i>24; i-=1){
  385. startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
  386. if(startcode == 0x10)
  387. break;
  388. }
  389. if (startcode != 0x10){
  390. av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
  391. return -1;
  392. }
  393. /* temporal reference */
  394. s->picture_number = get_bits(&s->gb, 5); /* picture timestamp */
  395. /* PTYPE starts here */
  396. skip_bits1(&s->gb); /* split screen off */
  397. skip_bits1(&s->gb); /* camera off */
  398. skip_bits1(&s->gb); /* freeze picture release off */
  399. format = get_bits1(&s->gb);
  400. //only 2 formats possible
  401. if (format == 0){//QCIF
  402. s->width = 176;
  403. s->height = 144;
  404. s->mb_width = 11;
  405. s->mb_height = 9;
  406. }else{//CIF
  407. s->width = 352;
  408. s->height = 288;
  409. s->mb_width = 22;
  410. s->mb_height = 18;
  411. }
  412. s->mb_num = s->mb_width * s->mb_height;
  413. skip_bits1(&s->gb); /* still image mode off */
  414. skip_bits1(&s->gb); /* Reserved */
  415. /* PEI */
  416. while (get_bits1(&s->gb) != 0){
  417. skip_bits(&s->gb, 8);
  418. }
  419. //h261 has no I-FRAMES, pass the test in MPV_frame_start in mpegvideo.c
  420. if(h261_framecounter > 1)
  421. s->pict_type = P_TYPE;
  422. else
  423. s->pict_type = I_TYPE;
  424. h261_framecounter++;
  425. h->gob_number = 0;
  426. return 0;
  427. }
  428. static int h261_decode_gob(H261Context *h){
  429. MpegEncContext * const s = &h->s;
  430. int i;
  431. ff_set_qscale(s, s->qscale);
  432. while(h->current_mba <= MAX_MBA)
  433. {
  434. int ret;
  435. /* DCT & quantize */
  436. s->dsp.clear_blocks(s->block[0]);
  437. ret= h261_decode_mb(h, s->block);
  438. if(ret<0){
  439. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  440. if(ret==SLICE_END){
  441. MPV_decode_mb(s, s->block);
  442. if(h->loop_filter){
  443. ff_h261_loop_filter(h);
  444. }
  445. h->loop_filter = 0;
  446. for(i=1; i<h->mba_diff; i++){
  447. s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1-i) % 11);
  448. s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1-i) / 11);
  449. ff_init_block_index(s);
  450. ff_update_block_index(s);
  451. s->dsp.clear_blocks(s->block[0]);
  452. ret= h261_decode_mb_skipped(h, s->block);
  453. MPV_decode_mb(s, s->block);
  454. }
  455. return 0;
  456. }else if(ret==SLICE_NOEND){
  457. av_log(s->avctx, AV_LOG_ERROR, "Slice mismatch at MB: %d\n", xy);
  458. return -1;
  459. }
  460. av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
  461. return -1;
  462. }
  463. MPV_decode_mb(s, s->block);
  464. if(h->loop_filter){
  465. ff_h261_loop_filter(h);
  466. }
  467. h->loop_filter = 0;
  468. for(i=1; i<h->mba_diff; i++){
  469. s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1-i) % 11);
  470. s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1-i) / 11);
  471. ff_init_block_index(s);
  472. ff_update_block_index(s);
  473. s->dsp.clear_blocks(s->block[0]);
  474. ret= h261_decode_mb_skipped(h, s->block);
  475. MPV_decode_mb(s, s->block);
  476. }
  477. }
  478. return -1;
  479. }
  480. static int h261_find_frame_end(ParseContext *pc, AVCodecContext* avctx, const uint8_t *buf, int buf_size){
  481. int vop_found, i, j, bits_left, last_bits;
  482. uint32_t state;
  483. H261Context *h = avctx->priv_data;
  484. if(h){
  485. bits_left = h->bits_left;
  486. last_bits = h->last_bits;
  487. }
  488. else{
  489. bits_left = 0;
  490. last_bits = 0;
  491. }
  492. vop_found= pc->frame_start_found;
  493. state= pc->state;
  494. if(bits_left!=0 && !vop_found)
  495. state = state << (8-bits_left) | last_bits;
  496. i=0;
  497. if(!vop_found){
  498. for(i=0; i<buf_size; i++){
  499. state= (state<<8) | buf[i];
  500. for(j=0; j<8; j++){
  501. if(( ( (state<<j) | (buf[i]>>(8-j)) )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
  502. i++;
  503. vop_found=1;
  504. break;
  505. }
  506. }
  507. if(vop_found)
  508. break;
  509. }
  510. }
  511. if(vop_found){
  512. for(; i<buf_size; i++){
  513. if(avctx->flags & CODEC_FLAG_TRUNCATED)//XXX ffplay workaround, someone a better solution?
  514. state= (state<<8) | buf[i];
  515. for(j=0; j<8; j++){
  516. if(( ( (state<<j) | (buf[i]>>(8-j)) )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
  517. pc->frame_start_found=0;
  518. pc->state=-1;
  519. return i-3;
  520. }
  521. }
  522. }
  523. }
  524. pc->frame_start_found= vop_found;
  525. pc->state= state;
  526. return END_NOT_FOUND;
  527. }
  528. static int h261_parse(AVCodecParserContext *s,
  529. AVCodecContext *avctx,
  530. uint8_t **poutbuf, int *poutbuf_size,
  531. const uint8_t *buf, int buf_size)
  532. {
  533. ParseContext *pc = s->priv_data;
  534. int next;
  535. next= h261_find_frame_end(pc,avctx, buf, buf_size);
  536. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  537. *poutbuf = NULL;
  538. *poutbuf_size = 0;
  539. return buf_size;
  540. }
  541. *poutbuf = (uint8_t *)buf;
  542. *poutbuf_size = buf_size;
  543. return next;
  544. }
  545. /**
  546. * returns the number of bytes consumed for building the current frame
  547. */
  548. static int get_consumed_bytes(MpegEncContext *s, int buf_size){
  549. int pos= (get_bits_count(&s->gb)+7)>>3;
  550. if(s->flags&CODEC_FLAG_TRUNCATED){
  551. pos -= s->parse_context.last_index;
  552. if(pos<0) pos=0;// padding is not really read so this might be -1
  553. return pos;
  554. }else{
  555. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  556. if(pos+10>buf_size) pos=buf_size; // oops ;)
  557. return pos;
  558. }
  559. }
  560. static int h261_decode_frame(AVCodecContext *avctx,
  561. void *data, int *data_size,
  562. uint8_t *buf, int buf_size)
  563. {
  564. H261Context *h= avctx->priv_data;
  565. MpegEncContext *s = &h->s;
  566. int ret;
  567. AVFrame *pict = data;
  568. #ifdef DEBUG
  569. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  570. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  571. #endif
  572. s->flags= avctx->flags;
  573. s->flags2= avctx->flags2;
  574. /* no supplementary picture */
  575. if (buf_size == 0) {
  576. return 0;
  577. }
  578. if(s->flags&CODEC_FLAG_TRUNCATED){
  579. int next;
  580. next= h261_find_frame_end(&s->parse_context,avctx, buf, buf_size);
  581. if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
  582. return buf_size;
  583. }
  584. retry:
  585. init_get_bits(&s->gb, buf, buf_size*8);
  586. if(!s->context_initialized){
  587. if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
  588. return -1;
  589. }
  590. //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
  591. if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
  592. int i= ff_find_unused_picture(s, 0);
  593. s->current_picture_ptr= &s->picture[i];
  594. }
  595. ret = h261_decode_picture_header(h);
  596. /* skip if the header was thrashed */
  597. if (ret < 0){
  598. av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
  599. return -1;
  600. }
  601. if (s->width != avctx->width || s->height != avctx->height){
  602. ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
  603. s->parse_context.buffer=0;
  604. MPV_common_end(s);
  605. s->parse_context= pc;
  606. }
  607. if (!s->context_initialized) {
  608. avctx->width = s->width;
  609. avctx->height = s->height;
  610. goto retry;
  611. }
  612. // for hurry_up==5
  613. s->current_picture.pict_type= s->pict_type;
  614. s->current_picture.key_frame= s->pict_type == I_TYPE;
  615. /* skip everything if we are in a hurry>=5 */
  616. if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
  617. if(MPV_frame_start(s, avctx) < 0)
  618. return -1;
  619. ff_er_frame_start(s);
  620. /* decode each macroblock */
  621. s->mb_x=0;
  622. s->mb_y=0;
  623. while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
  624. if(ff_h261_resync(h)<0)
  625. break;
  626. h261_decode_gob(h);
  627. }
  628. MPV_frame_end(s);
  629. // h261 doesn't have byte aligned codes
  630. // store the bits of the next frame that are left in the last byte
  631. // in the H261Context and remember the number of stored bits
  632. {
  633. int bitsleft;
  634. int current_pos= get_bits_count(&s->gb)>>3;
  635. bitsleft = (current_pos<<3) - get_bits_count(&s->gb);
  636. h->bits_left = - bitsleft;
  637. if(bitsleft > 0)
  638. h->last_bits= get_bits(&s->gb, 8 - h->bits_left);
  639. else
  640. h->last_bits = 0;
  641. }
  642. assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
  643. assert(s->current_picture.pict_type == s->pict_type);
  644. *pict= *(AVFrame*)&s->current_picture;
  645. ff_print_debug_info(s, pict);
  646. /* Return the Picture timestamp as the frame number */
  647. /* we substract 1 because it is added on utils.c */
  648. avctx->frame_number = s->picture_number - 1;
  649. *data_size = sizeof(AVFrame);
  650. return get_consumed_bytes(s, buf_size);
  651. }
  652. static int h261_decode_end(AVCodecContext *avctx)
  653. {
  654. H261Context *h= avctx->priv_data;
  655. MpegEncContext *s = &h->s;
  656. MPV_common_end(s);
  657. return 0;
  658. }
  659. AVCodec h261_decoder = {
  660. "h261",
  661. CODEC_TYPE_VIDEO,
  662. CODEC_ID_H261,
  663. sizeof(H261Context),
  664. h261_decode_init,
  665. NULL,
  666. h261_decode_end,
  667. h261_decode_frame,
  668. CODEC_CAP_TRUNCATED,
  669. };
  670. AVCodecParser h261_parser = {
  671. { CODEC_ID_H261 },
  672. sizeof(ParseContext),
  673. NULL,
  674. h261_parse,
  675. ff_parse_close,
  676. };