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.

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