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.

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