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.

651 lines
18KB

  1. /*
  2. * H261 decoder
  3. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  4. * Copyright (c) 2004 Maarten Daniels
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file h261dec.c
  24. * H.261 decoder.
  25. */
  26. #include "dsputil.h"
  27. #include "avcodec.h"
  28. #include "mpegvideo.h"
  29. #include "h261.h"
  30. #include "h261data.h"
  31. #define H261_MBA_VLC_BITS 9
  32. #define H261_MTYPE_VLC_BITS 6
  33. #define H261_MV_VLC_BITS 7
  34. #define H261_CBP_VLC_BITS 9
  35. #define TCOEFF_VLC_BITS 9
  36. #define MBA_STUFFING 33
  37. #define MBA_STARTCODE 34
  38. extern uint8_t ff_h261_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
  39. static VLC h261_mba_vlc;
  40. static VLC h261_mtype_vlc;
  41. static VLC h261_mv_vlc;
  42. static VLC h261_cbp_vlc;
  43. static int h261_decode_block(H261Context * h, DCTELEM * block, int n, int coded);
  44. static void h261_decode_init_vlc(H261Context *h){
  45. static int done = 0;
  46. if(!done){
  47. done = 1;
  48. init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
  49. h261_mba_bits, 1, 1,
  50. h261_mba_code, 1, 1, 1);
  51. init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
  52. h261_mtype_bits, 1, 1,
  53. h261_mtype_code, 1, 1, 1);
  54. init_vlc(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
  55. &h261_mv_tab[0][1], 2, 1,
  56. &h261_mv_tab[0][0], 2, 1, 1);
  57. init_vlc(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
  58. &h261_cbp_tab[0][1], 2, 1,
  59. &h261_cbp_tab[0][0], 2, 1, 1);
  60. init_rl(&h261_rl_tcoeff, ff_h261_rl_table_store);
  61. init_vlc_rl(&h261_rl_tcoeff, 1);
  62. }
  63. }
  64. static int h261_decode_init(AVCodecContext *avctx){
  65. H261Context *h= avctx->priv_data;
  66. MpegEncContext * const s = &h->s;
  67. // set defaults
  68. MPV_decode_defaults(s);
  69. s->avctx = avctx;
  70. s->width = s->avctx->coded_width;
  71. s->height = s->avctx->coded_height;
  72. s->codec_id = s->avctx->codec->id;
  73. s->out_format = FMT_H261;
  74. s->low_delay= 1;
  75. avctx->pix_fmt= PIX_FMT_YUV420P;
  76. s->codec_id= avctx->codec->id;
  77. h261_decode_init_vlc(h);
  78. h->gob_start_code_skipped = 0;
  79. return 0;
  80. }
  81. /**
  82. * decodes the group of blocks header or slice header.
  83. * @return <0 if an error occured
  84. */
  85. static int h261_decode_gob_header(H261Context *h){
  86. unsigned int val;
  87. MpegEncContext * const s = &h->s;
  88. if ( !h->gob_start_code_skipped ){
  89. /* Check for GOB Start Code */
  90. val = show_bits(&s->gb, 15);
  91. if(val)
  92. return -1;
  93. /* We have a GBSC */
  94. skip_bits(&s->gb, 16);
  95. }
  96. h->gob_start_code_skipped = 0;
  97. h->gob_number = get_bits(&s->gb, 4); /* GN */
  98. s->qscale = get_bits(&s->gb, 5); /* GQUANT */
  99. /* Check if gob_number is valid */
  100. if (s->mb_height==18){ //cif
  101. if ((h->gob_number<=0) || (h->gob_number>12))
  102. return -1;
  103. }
  104. else{ //qcif
  105. if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
  106. return -1;
  107. }
  108. /* GEI */
  109. while (get_bits1(&s->gb) != 0) {
  110. skip_bits(&s->gb, 8);
  111. }
  112. if(s->qscale==0)
  113. return -1;
  114. // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
  115. // subsequent macroblocks, MBA is the difference between the absolute addresses of
  116. // the macroblock and the last transmitted macroblock.
  117. h->current_mba = 0;
  118. h->mba_diff = 0;
  119. return 0;
  120. }
  121. /**
  122. * decodes the group of blocks / video packet header.
  123. * @return <0 if no resync found
  124. */
  125. static int ff_h261_resync(H261Context *h){
  126. MpegEncContext * const s = &h->s;
  127. int left, ret;
  128. if ( h->gob_start_code_skipped ){
  129. ret= h261_decode_gob_header(h);
  130. if(ret>=0)
  131. return 0;
  132. }
  133. else{
  134. if(show_bits(&s->gb, 15)==0){
  135. ret= h261_decode_gob_header(h);
  136. if(ret>=0)
  137. return 0;
  138. }
  139. //OK, it is not where it is supposed to be ...
  140. s->gb= s->last_resync_gb;
  141. align_get_bits(&s->gb);
  142. left= s->gb.size_in_bits - get_bits_count(&s->gb);
  143. for(;left>15+1+4+5; left-=8){
  144. if(show_bits(&s->gb, 15)==0){
  145. GetBitContext bak= s->gb;
  146. ret= h261_decode_gob_header(h);
  147. if(ret>=0)
  148. return 0;
  149. s->gb= bak;
  150. }
  151. skip_bits(&s->gb, 8);
  152. }
  153. }
  154. return -1;
  155. }
  156. /**
  157. * decodes skipped macroblocks
  158. * @return 0
  159. */
  160. static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
  161. {
  162. MpegEncContext * const s = &h->s;
  163. int i;
  164. s->mb_intra = 0;
  165. for(i=mba1; i<mba2; i++){
  166. int j, xy;
  167. s->mb_x= ((h->gob_number-1) % 2) * 11 + i % 11;
  168. s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11;
  169. xy = s->mb_x + s->mb_y * s->mb_stride;
  170. ff_init_block_index(s);
  171. ff_update_block_index(s);
  172. for(j=0;j<6;j++)
  173. s->block_last_index[j] = -1;
  174. s->mv_dir = MV_DIR_FORWARD;
  175. s->mv_type = MV_TYPE_16X16;
  176. s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
  177. s->mv[0][0][0] = 0;
  178. s->mv[0][0][1] = 0;
  179. s->mb_skipped = 1;
  180. h->mtype &= ~MB_TYPE_H261_FIL;
  181. MPV_decode_mb(s, s->block);
  182. }
  183. return 0;
  184. }
  185. static int decode_mv_component(GetBitContext *gb, int v){
  186. int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
  187. /* check if mv_diff is valid */
  188. if ( mv_diff < 0 )
  189. return v;
  190. mv_diff = mvmap[mv_diff];
  191. if(mv_diff && !get_bits1(gb))
  192. mv_diff= -mv_diff;
  193. v += mv_diff;
  194. if (v <=-16) v+= 32;
  195. else if(v >= 16) v-= 32;
  196. return v;
  197. }
  198. static int h261_decode_mb(H261Context *h){
  199. MpegEncContext * const s = &h->s;
  200. int i, cbp, xy;
  201. cbp = 63;
  202. // Read mba
  203. do{
  204. h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
  205. /* Check for slice end */
  206. /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
  207. if (h->mba_diff == MBA_STARTCODE){ // start code
  208. h->gob_start_code_skipped = 1;
  209. return SLICE_END;
  210. }
  211. }
  212. while( h->mba_diff == MBA_STUFFING ); // stuffing
  213. if ( h->mba_diff < 0 ){
  214. if ( get_bits_count(&s->gb) + 7 >= s->gb.size_in_bits )
  215. return SLICE_END;
  216. av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
  217. return SLICE_ERROR;
  218. }
  219. h->mba_diff += 1;
  220. h->current_mba += h->mba_diff;
  221. if ( h->current_mba > MBA_STUFFING )
  222. return SLICE_ERROR;
  223. s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
  224. s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
  225. xy = s->mb_x + s->mb_y * s->mb_stride;
  226. ff_init_block_index(s);
  227. ff_update_block_index(s);
  228. // Read mtype
  229. h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
  230. h->mtype = h261_mtype_map[h->mtype];
  231. // Read mquant
  232. if ( IS_QUANT ( h->mtype ) ){
  233. ff_set_qscale(s, get_bits(&s->gb, 5));
  234. }
  235. s->mb_intra = IS_INTRA4x4(h->mtype);
  236. // Read mv
  237. if ( IS_16X16 ( h->mtype ) ){
  238. // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
  239. // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
  240. // following three situations:
  241. // 1) evaluating MVD for macroblocks 1, 12 and 23;
  242. // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
  243. // 3) MTYPE of the previous macroblock was not MC.
  244. if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
  245. ( h->mba_diff != 1))
  246. {
  247. h->current_mv_x = 0;
  248. h->current_mv_y = 0;
  249. }
  250. h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
  251. h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
  252. }else{
  253. h->current_mv_x = 0;
  254. h->current_mv_y = 0;
  255. }
  256. // Read cbp
  257. if ( HAS_CBP( h->mtype ) ){
  258. cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
  259. }
  260. if(s->mb_intra){
  261. s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
  262. goto intra;
  263. }
  264. //set motion vectors
  265. s->mv_dir = MV_DIR_FORWARD;
  266. s->mv_type = MV_TYPE_16X16;
  267. s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
  268. s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
  269. s->mv[0][0][1] = h->current_mv_y * 2;
  270. intra:
  271. /* decode each block */
  272. if(s->mb_intra || HAS_CBP(h->mtype)){
  273. s->dsp.clear_blocks(s->block[0]);
  274. for (i = 0; i < 6; i++) {
  275. if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
  276. return SLICE_ERROR;
  277. }
  278. cbp+=cbp;
  279. }
  280. }else{
  281. for (i = 0; i < 6; i++)
  282. s->block_last_index[i]= -1;
  283. }
  284. MPV_decode_mb(s, s->block);
  285. return SLICE_OK;
  286. }
  287. /**
  288. * decodes a macroblock
  289. * @return <0 if an error occured
  290. */
  291. static int h261_decode_block(H261Context * h, DCTELEM * block,
  292. int n, int coded)
  293. {
  294. MpegEncContext * const s = &h->s;
  295. int code, level, i, j, run;
  296. RLTable *rl = &h261_rl_tcoeff;
  297. const uint8_t *scan_table;
  298. // For the variable length encoding there are two code tables, one being used for
  299. // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
  300. // for all other LEVELs except the first one in INTRA blocks which is fixed length
  301. // coded with 8 bits.
  302. // NOTE: the two code tables only differ in one VLC so we handle that manually.
  303. scan_table = s->intra_scantable.permutated;
  304. if (s->mb_intra){
  305. /* DC coef */
  306. level = get_bits(&s->gb, 8);
  307. // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
  308. if((level&0x7F) == 0){
  309. av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
  310. return -1;
  311. }
  312. // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
  313. if (level == 255)
  314. level = 128;
  315. block[0] = level;
  316. i = 1;
  317. }else if(coded){
  318. // Run Level Code
  319. // EOB Not possible for first level when cbp is available (that's why the table is different)
  320. // 0 1 1s
  321. // * * 0*
  322. int check = show_bits(&s->gb, 2);
  323. i = 0;
  324. if ( check & 0x2 ){
  325. skip_bits(&s->gb, 2);
  326. block[0] = ( check & 0x1 ) ? -1 : 1;
  327. i = 1;
  328. }
  329. }else{
  330. i = 0;
  331. }
  332. if(!coded){
  333. s->block_last_index[n] = i - 1;
  334. return 0;
  335. }
  336. for(;;){
  337. code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
  338. if (code < 0){
  339. av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
  340. return -1;
  341. }
  342. if (code == rl->n) {
  343. /* escape */
  344. // 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.
  345. run = get_bits(&s->gb, 6);
  346. level = get_sbits(&s->gb, 8);
  347. }else if(code == 0){
  348. break;
  349. }else{
  350. run = rl->table_run[code];
  351. level = rl->table_level[code];
  352. if (get_bits1(&s->gb))
  353. level = -level;
  354. }
  355. i += run;
  356. if (i >= 64){
  357. av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
  358. return -1;
  359. }
  360. j = scan_table[i];
  361. block[j] = level;
  362. i++;
  363. }
  364. s->block_last_index[n] = i-1;
  365. return 0;
  366. }
  367. /**
  368. * decodes the H261 picture header.
  369. * @return <0 if no startcode found
  370. */
  371. static int h261_decode_picture_header(H261Context *h){
  372. MpegEncContext * const s = &h->s;
  373. int format, i;
  374. uint32_t startcode= 0;
  375. for(i= s->gb.size_in_bits - get_bits_count(&s->gb); i>24; i-=1){
  376. startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
  377. if(startcode == 0x10)
  378. break;
  379. }
  380. if (startcode != 0x10){
  381. av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
  382. return -1;
  383. }
  384. /* temporal reference */
  385. i= get_bits(&s->gb, 5); /* picture timestamp */
  386. if(i < (s->picture_number&31))
  387. i += 32;
  388. s->picture_number = (s->picture_number&~31) + i;
  389. s->avctx->time_base= (AVRational){1001, 30000};
  390. s->current_picture.pts= s->picture_number;
  391. /* PTYPE starts here */
  392. skip_bits1(&s->gb); /* split screen off */
  393. skip_bits1(&s->gb); /* camera off */
  394. skip_bits1(&s->gb); /* freeze picture release off */
  395. format = get_bits1(&s->gb);
  396. //only 2 formats possible
  397. if (format == 0){//QCIF
  398. s->width = 176;
  399. s->height = 144;
  400. s->mb_width = 11;
  401. s->mb_height = 9;
  402. }else{//CIF
  403. s->width = 352;
  404. s->height = 288;
  405. s->mb_width = 22;
  406. s->mb_height = 18;
  407. }
  408. s->mb_num = s->mb_width * s->mb_height;
  409. skip_bits1(&s->gb); /* still image mode off */
  410. skip_bits1(&s->gb); /* Reserved */
  411. /* PEI */
  412. while (get_bits1(&s->gb) != 0){
  413. skip_bits(&s->gb, 8);
  414. }
  415. // h261 has no I-FRAMES, but if we pass I_TYPE for the first frame, the codec crashes if it does
  416. // not contain all I-blocks (e.g. when a packet is lost)
  417. s->pict_type = P_TYPE;
  418. h->gob_number = 0;
  419. return 0;
  420. }
  421. static int h261_decode_gob(H261Context *h){
  422. MpegEncContext * const s = &h->s;
  423. ff_set_qscale(s, s->qscale);
  424. /* decode mb's */
  425. while(h->current_mba <= MBA_STUFFING)
  426. {
  427. int ret;
  428. /* DCT & quantize */
  429. ret= h261_decode_mb(h);
  430. if(ret<0){
  431. if(ret==SLICE_END){
  432. h261_decode_mb_skipped(h, h->current_mba, 33);
  433. return 0;
  434. }
  435. av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
  436. return -1;
  437. }
  438. h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
  439. }
  440. return -1;
  441. }
  442. /**
  443. * returns the number of bytes consumed for building the current frame
  444. */
  445. static int get_consumed_bytes(MpegEncContext *s, int buf_size){
  446. int pos= get_bits_count(&s->gb)>>3;
  447. if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
  448. if(pos+10>buf_size) pos=buf_size; // oops ;)
  449. return pos;
  450. }
  451. static int h261_decode_frame(AVCodecContext *avctx,
  452. void *data, int *data_size,
  453. uint8_t *buf, int buf_size)
  454. {
  455. H261Context *h= avctx->priv_data;
  456. MpegEncContext *s = &h->s;
  457. int ret;
  458. AVFrame *pict = data;
  459. #ifdef DEBUG
  460. av_log(avctx, AV_LOG_DEBUG, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
  461. av_log(avctx, AV_LOG_DEBUG, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  462. #endif
  463. s->flags= avctx->flags;
  464. s->flags2= avctx->flags2;
  465. h->gob_start_code_skipped=0;
  466. retry:
  467. init_get_bits(&s->gb, buf, buf_size*8);
  468. if(!s->context_initialized){
  469. if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
  470. return -1;
  471. }
  472. //we need to set current_picture_ptr before reading the header, otherwise we cannot store anyting im there
  473. if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
  474. int i= ff_find_unused_picture(s, 0);
  475. s->current_picture_ptr= &s->picture[i];
  476. }
  477. ret = h261_decode_picture_header(h);
  478. /* skip if the header was thrashed */
  479. if (ret < 0){
  480. av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
  481. return -1;
  482. }
  483. if (s->width != avctx->coded_width || s->height != avctx->coded_height){
  484. ParseContext pc= s->parse_context; //FIXME move this demuxing hack to libavformat
  485. s->parse_context.buffer=0;
  486. MPV_common_end(s);
  487. s->parse_context= pc;
  488. }
  489. if (!s->context_initialized) {
  490. avcodec_set_dimensions(avctx, s->width, s->height);
  491. goto retry;
  492. }
  493. // for hurry_up==5
  494. s->current_picture.pict_type= s->pict_type;
  495. s->current_picture.key_frame= s->pict_type == I_TYPE;
  496. /* skip everything if we are in a hurry>=5 */
  497. if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
  498. if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
  499. ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
  500. || avctx->skip_frame >= AVDISCARD_ALL)
  501. return get_consumed_bytes(s, buf_size);
  502. if(MPV_frame_start(s, avctx) < 0)
  503. return -1;
  504. ff_er_frame_start(s);
  505. /* decode each macroblock */
  506. s->mb_x=0;
  507. s->mb_y=0;
  508. while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
  509. if(ff_h261_resync(h)<0)
  510. break;
  511. h261_decode_gob(h);
  512. }
  513. MPV_frame_end(s);
  514. assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
  515. assert(s->current_picture.pict_type == s->pict_type);
  516. *pict= *(AVFrame*)s->current_picture_ptr;
  517. ff_print_debug_info(s, pict);
  518. *data_size = sizeof(AVFrame);
  519. return get_consumed_bytes(s, buf_size);
  520. }
  521. static int h261_decode_end(AVCodecContext *avctx)
  522. {
  523. H261Context *h= avctx->priv_data;
  524. MpegEncContext *s = &h->s;
  525. MPV_common_end(s);
  526. return 0;
  527. }
  528. AVCodec h261_decoder = {
  529. "h261",
  530. CODEC_TYPE_VIDEO,
  531. CODEC_ID_H261,
  532. sizeof(H261Context),
  533. h261_decode_init,
  534. NULL,
  535. h261_decode_end,
  536. h261_decode_frame,
  537. CODEC_CAP_DR1,
  538. };