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.

661 lines
19KB

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