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.

657 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
  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. 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. 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. * decodes 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->error_recognition >= FF_ER_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. * decodes 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. * decodes 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.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. 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_count(&s->gb) + 7 >= s->gb.size_in_bits )
  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. h->mtype = h261_mtype_map[h->mtype];
  235. // Read mquant
  236. if ( IS_QUANT ( h->mtype ) ){
  237. ff_set_qscale(s, get_bits(&s->gb, 5));
  238. }
  239. s->mb_intra = IS_INTRA4x4(h->mtype);
  240. // Read mv
  241. if ( IS_16X16 ( h->mtype ) ){
  242. // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
  243. // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
  244. // following three situations:
  245. // 1) evaluating MVD for macroblocks 1, 12 and 23;
  246. // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
  247. // 3) MTYPE of the previous macroblock was not MC.
  248. if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
  249. ( h->mba_diff != 1))
  250. {
  251. h->current_mv_x = 0;
  252. h->current_mv_y = 0;
  253. }
  254. h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
  255. h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
  256. }else{
  257. h->current_mv_x = 0;
  258. h->current_mv_y = 0;
  259. }
  260. // Read cbp
  261. if ( HAS_CBP( h->mtype ) ){
  262. cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
  263. }
  264. if(s->mb_intra){
  265. s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
  266. goto intra;
  267. }
  268. //set motion vectors
  269. s->mv_dir = MV_DIR_FORWARD;
  270. s->mv_type = MV_TYPE_16X16;
  271. s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
  272. s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
  273. s->mv[0][0][1] = h->current_mv_y * 2;
  274. intra:
  275. /* decode each block */
  276. if(s->mb_intra || HAS_CBP(h->mtype)){
  277. s->dsp.clear_blocks(s->block[0]);
  278. for (i = 0; i < 6; i++) {
  279. if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
  280. return SLICE_ERROR;
  281. }
  282. cbp+=cbp;
  283. }
  284. }else{
  285. for (i = 0; i < 6; i++)
  286. s->block_last_index[i]= -1;
  287. }
  288. MPV_decode_mb(s, s->block);
  289. return SLICE_OK;
  290. }
  291. /**
  292. * decodes a macroblock
  293. * @return <0 if an error occurred
  294. */
  295. static int h261_decode_block(H261Context * h, DCTELEM * block,
  296. int n, int coded)
  297. {
  298. MpegEncContext * const s = &h->s;
  299. int code, level, i, j, run;
  300. RLTable *rl = &h261_rl_tcoeff;
  301. const uint8_t *scan_table;
  302. // For the variable length encoding there are two code tables, one being used for
  303. // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
  304. // for all other LEVELs except the first one in INTRA blocks which is fixed length
  305. // coded with 8 bits.
  306. // NOTE: the two code tables only differ in one VLC so we handle that manually.
  307. scan_table = s->intra_scantable.permutated;
  308. if (s->mb_intra){
  309. /* DC coef */
  310. level = get_bits(&s->gb, 8);
  311. // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
  312. if((level&0x7F) == 0){
  313. av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
  314. return -1;
  315. }
  316. // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
  317. if (level == 255)
  318. level = 128;
  319. block[0] = level;
  320. i = 1;
  321. }else if(coded){
  322. // Run Level Code
  323. // EOB Not possible for first level when cbp is available (that's why the table is different)
  324. // 0 1 1s
  325. // * * 0*
  326. int check = show_bits(&s->gb, 2);
  327. i = 0;
  328. if ( check & 0x2 ){
  329. skip_bits(&s->gb, 2);
  330. block[0] = ( check & 0x1 ) ? -1 : 1;
  331. i = 1;
  332. }
  333. }else{
  334. i = 0;
  335. }
  336. if(!coded){
  337. s->block_last_index[n] = i - 1;
  338. return 0;
  339. }
  340. for(;;){
  341. code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
  342. if (code < 0){
  343. av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
  344. return -1;
  345. }
  346. if (code == rl->n) {
  347. /* escape */
  348. // 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.
  349. run = get_bits(&s->gb, 6);
  350. level = get_sbits(&s->gb, 8);
  351. }else if(code == 0){
  352. break;
  353. }else{
  354. run = rl->table_run[code];
  355. level = rl->table_level[code];
  356. if (get_bits1(&s->gb))
  357. level = -level;
  358. }
  359. i += run;
  360. if (i >= 64){
  361. av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
  362. return -1;
  363. }
  364. j = scan_table[i];
  365. block[j] = level;
  366. i++;
  367. }
  368. s->block_last_index[n] = i-1;
  369. return 0;
  370. }
  371. /**
  372. * decodes the H261 picture header.
  373. * @return <0 if no startcode found
  374. */
  375. static int h261_decode_picture_header(H261Context *h){
  376. MpegEncContext * const s = &h->s;
  377. int format, i;
  378. uint32_t startcode= 0;
  379. for(i= get_bits_left(&s->gb); i>24; i-=1){
  380. startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
  381. if(startcode == 0x10)
  382. break;
  383. }
  384. if (startcode != 0x10){
  385. av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
  386. return -1;
  387. }
  388. /* temporal reference */
  389. i= get_bits(&s->gb, 5); /* picture timestamp */
  390. if(i < (s->picture_number&31))
  391. i += 32;
  392. s->picture_number = (s->picture_number&~31) + i;
  393. s->avctx->time_base= (AVRational){1001, 30000};
  394. s->current_picture.pts= s->picture_number;
  395. /* PTYPE starts here */
  396. skip_bits1(&s->gb); /* split screen off */
  397. skip_bits1(&s->gb); /* camera off */
  398. skip_bits1(&s->gb); /* freeze picture release off */
  399. format = get_bits1(&s->gb);
  400. //only 2 formats possible
  401. if (format == 0){//QCIF
  402. s->width = 176;
  403. s->height = 144;
  404. s->mb_width = 11;
  405. s->mb_height = 9;
  406. }else{//CIF
  407. s->width = 352;
  408. s->height = 288;
  409. s->mb_width = 22;
  410. s->mb_height = 18;
  411. }
  412. s->mb_num = s->mb_width * s->mb_height;
  413. skip_bits1(&s->gb); /* still image mode off */
  414. skip_bits1(&s->gb); /* Reserved */
  415. /* PEI */
  416. while (get_bits1(&s->gb) != 0){
  417. skip_bits(&s->gb, 8);
  418. }
  419. // h261 has no I-FRAMES, but if we pass FF_I_TYPE for the first frame, the codec crashes if it does
  420. // not contain all I-blocks (e.g. when a packet is lost)
  421. s->pict_type = FF_P_TYPE;
  422. h->gob_number = 0;
  423. return 0;
  424. }
  425. static int h261_decode_gob(H261Context *h){
  426. MpegEncContext * const s = &h->s;
  427. ff_set_qscale(s, s->qscale);
  428. /* decode mb's */
  429. while(h->current_mba <= MBA_STUFFING)
  430. {
  431. int ret;
  432. /* DCT & quantize */
  433. ret= h261_decode_mb(h);
  434. if(ret<0){
  435. if(ret==SLICE_END){
  436. h261_decode_mb_skipped(h, h->current_mba, 33);
  437. return 0;
  438. }
  439. av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
  440. return -1;
  441. }
  442. h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
  443. }
  444. return -1;
  445. }
  446. /**
  447. * returns the number of bytes consumed for building the current frame
  448. */
  449. static int get_consumed_bytes(MpegEncContext *s, int buf_size){
  450. int pos= get_bits_count(&s->gb)>>3;
  451. if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
  452. if(pos+10>buf_size) pos=buf_size; // oops ;)
  453. return pos;
  454. }
  455. static int h261_decode_frame(AVCodecContext *avctx,
  456. void *data, int *data_size,
  457. AVPacket *avpkt)
  458. {
  459. const uint8_t *buf = avpkt->data;
  460. int buf_size = avpkt->size;
  461. H261Context *h= avctx->priv_data;
  462. MpegEncContext *s = &h->s;
  463. int ret;
  464. AVFrame *pict = data;
  465. dprintf(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
  466. dprintf(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  467. s->flags= avctx->flags;
  468. s->flags2= avctx->flags2;
  469. h->gob_start_code_skipped=0;
  470. retry:
  471. init_get_bits(&s->gb, buf, buf_size*8);
  472. if(!s->context_initialized){
  473. if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
  474. return -1;
  475. }
  476. //we need to set current_picture_ptr before reading the header, otherwise we cannot store anyting im there
  477. if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
  478. int i= ff_find_unused_picture(s, 0);
  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. 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 hurry_up==5
  498. s->current_picture.pict_type= s->pict_type;
  499. s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
  500. /* skip everything if we are in a hurry>=5 */
  501. if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
  502. if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==FF_B_TYPE)
  503. ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=FF_I_TYPE)
  504. || avctx->skip_frame >= AVDISCARD_ALL)
  505. return get_consumed_bytes(s, buf_size);
  506. if(MPV_frame_start(s, avctx) < 0)
  507. return -1;
  508. ff_er_frame_start(s);
  509. /* decode each macroblock */
  510. s->mb_x=0;
  511. s->mb_y=0;
  512. while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
  513. if(ff_h261_resync(h)<0)
  514. break;
  515. h261_decode_gob(h);
  516. }
  517. MPV_frame_end(s);
  518. assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
  519. assert(s->current_picture.pict_type == s->pict_type);
  520. *pict= *(AVFrame*)s->current_picture_ptr;
  521. ff_print_debug_info(s, pict);
  522. *data_size = sizeof(AVFrame);
  523. return get_consumed_bytes(s, buf_size);
  524. }
  525. static av_cold int h261_decode_end(AVCodecContext *avctx)
  526. {
  527. H261Context *h= avctx->priv_data;
  528. MpegEncContext *s = &h->s;
  529. MPV_common_end(s);
  530. return 0;
  531. }
  532. AVCodec ff_h261_decoder = {
  533. "h261",
  534. AVMEDIA_TYPE_VIDEO,
  535. CODEC_ID_H261,
  536. sizeof(H261Context),
  537. h261_decode_init,
  538. NULL,
  539. h261_decode_end,
  540. h261_decode_frame,
  541. CODEC_CAP_DR1,
  542. .max_lowres = 3,
  543. .long_name = NULL_IF_CONFIG_SMALL("H.261"),
  544. };