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.

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