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.

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