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.

1080 lines
29KB

  1. /*
  2. * H261 decoder
  3. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  4. * Copyright (c) 2004 Maarten Daniels
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /**
  21. * @file h261.c
  22. * h261codec.
  23. */
  24. #include "common.h"
  25. #include "dsputil.h"
  26. #include "avcodec.h"
  27. #include "mpegvideo.h"
  28. #include "h261data.h"
  29. #define H261_MBA_VLC_BITS 9
  30. #define H261_MTYPE_VLC_BITS 6
  31. #define H261_MV_VLC_BITS 7
  32. #define H261_CBP_VLC_BITS 9
  33. #define TCOEFF_VLC_BITS 9
  34. #define MBA_STUFFING 33
  35. #define MBA_STARTCODE 34
  36. #define IS_FIL(a) ((a)&MB_TYPE_H261_FIL)
  37. /**
  38. * H261Context
  39. */
  40. typedef struct H261Context{
  41. MpegEncContext s;
  42. int current_mba;
  43. int previous_mba;
  44. int mba_diff;
  45. int mtype;
  46. int current_mv_x;
  47. int current_mv_y;
  48. int gob_number;
  49. int bits_left; //8 - nr of bits left of the following frame in the last byte in this frame
  50. int last_bits; //bits left of the following frame in the last byte in this frame
  51. int gob_start_code_skipped; // 1 if gob start code is already read before gob header is read
  52. }H261Context;
  53. void ff_h261_loop_filter(MpegEncContext *s){
  54. H261Context * h= (H261Context*)s;
  55. const int linesize = s->linesize;
  56. const int uvlinesize= s->uvlinesize;
  57. uint8_t *dest_y = s->dest[0];
  58. uint8_t *dest_cb= s->dest[1];
  59. uint8_t *dest_cr= s->dest[2];
  60. if(!(IS_FIL (h->mtype)))
  61. return;
  62. s->dsp.h261_loop_filter(dest_y , linesize);
  63. s->dsp.h261_loop_filter(dest_y + 8, linesize);
  64. s->dsp.h261_loop_filter(dest_y + 8 * linesize , linesize);
  65. s->dsp.h261_loop_filter(dest_y + 8 * linesize + 8, linesize);
  66. s->dsp.h261_loop_filter(dest_cb, uvlinesize);
  67. s->dsp.h261_loop_filter(dest_cr, uvlinesize);
  68. }
  69. static int ff_h261_get_picture_format(int width, int height){
  70. // QCIF
  71. if (width == 176 && height == 144)
  72. return 0;
  73. // CIF
  74. else if (width == 352 && height == 288)
  75. return 1;
  76. // ERROR
  77. else
  78. return -1;
  79. }
  80. static void h261_encode_block(H261Context * h, DCTELEM * block,
  81. int n);
  82. static int h261_decode_block(H261Context *h, DCTELEM *block,
  83. int n, int coded);
  84. void ff_h261_encode_picture_header(MpegEncContext * s, int picture_number){
  85. H261Context * h = (H261Context *) s;
  86. int format, temp_ref;
  87. align_put_bits(&s->pb);
  88. /* Update the pointer to last GOB */
  89. s->ptr_lastgob = pbBufPtr(&s->pb);
  90. put_bits(&s->pb, 20, 0x10); /* PSC */
  91. temp_ref= s->picture_number * (int64_t)30000 * s->avctx->frame_rate_base /
  92. (1001 * (int64_t)s->avctx->frame_rate);
  93. put_bits(&s->pb, 5, temp_ref & 0x1f); /* TemporalReference */
  94. put_bits(&s->pb, 1, 0); /* split screen off */
  95. put_bits(&s->pb, 1, 0); /* camera off */
  96. put_bits(&s->pb, 1, 0); /* freeze picture release off */
  97. format = ff_h261_get_picture_format(s->width, s->height);
  98. put_bits(&s->pb, 1, format); /* 0 == QCIF, 1 == CIF */
  99. put_bits(&s->pb, 1, 0); /* still image mode */
  100. put_bits(&s->pb, 1, 0); /* reserved */
  101. put_bits(&s->pb, 1, 0); /* no PEI */
  102. if(format == 0)
  103. h->gob_number = -1;
  104. else
  105. h->gob_number = 0;
  106. h->current_mba = 0;
  107. }
  108. /**
  109. * Encodes a group of blocks header.
  110. */
  111. static void h261_encode_gob_header(MpegEncContext * s, int mb_line){
  112. H261Context * h = (H261Context *)s;
  113. if(ff_h261_get_picture_format(s->width, s->height) == 0){
  114. h->gob_number+=2; // QCIF
  115. }
  116. else{
  117. h->gob_number++; // CIF
  118. }
  119. put_bits(&s->pb, 16, 1); /* GBSC */
  120. put_bits(&s->pb, 4, h->gob_number); /* GN */
  121. put_bits(&s->pb, 5, s->qscale); /* GQUANT */
  122. put_bits(&s->pb, 1, 0); /* no GEI */
  123. h->current_mba = 0;
  124. h->previous_mba = 0;
  125. h->current_mv_x=0;
  126. h->current_mv_y=0;
  127. }
  128. void ff_h261_reorder_mb_index(MpegEncContext* s){
  129. int index= s->mb_x + s->mb_y*s->mb_width;
  130. if(index % 33 == 0)
  131. h261_encode_gob_header(s,0);
  132. /* for CIF the GOB's are fragmented in the middle of a scanline
  133. that's why we need to adjust the x and y index of the macroblocks */
  134. if(ff_h261_get_picture_format(s->width,s->height) == 1){ // CIF
  135. s->mb_x = index % 11 ; index /= 11;
  136. s->mb_y = index % 3 ; index /= 3;
  137. s->mb_x+= 11*(index % 2); index /= 2;
  138. s->mb_y+= 3*index;
  139. ff_init_block_index(s);
  140. ff_update_block_index(s);
  141. }
  142. }
  143. static void h261_encode_motion(H261Context * h, int val){
  144. MpegEncContext * const s = &h->s;
  145. int sign, code;
  146. if(val==0){
  147. code = 0;
  148. put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]);
  149. }
  150. else{
  151. if(val > 16)
  152. val -=32;
  153. if(val < -16)
  154. val+=32;
  155. sign = val < 0;
  156. code = sign ? -val : val;
  157. put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]);
  158. put_bits(&s->pb,1,sign);
  159. }
  160. }
  161. static inline int get_cbp(MpegEncContext * s,
  162. DCTELEM block[6][64])
  163. {
  164. int i, cbp;
  165. cbp= 0;
  166. for (i = 0; i < 6; i++) {
  167. if (s->block_last_index[i] >= 0)
  168. cbp |= 1 << (5 - i);
  169. }
  170. return cbp;
  171. }
  172. void ff_h261_encode_mb(MpegEncContext * s,
  173. DCTELEM block[6][64],
  174. int motion_x, int motion_y)
  175. {
  176. H261Context * h = (H261Context *)s;
  177. int mvd, mv_diff_x, mv_diff_y, i, cbp;
  178. cbp = 63; // avoid warning
  179. mvd = 0;
  180. h->current_mba++;
  181. h->mtype = 0;
  182. if (!s->mb_intra){
  183. /* compute cbp */
  184. cbp= get_cbp(s, block);
  185. /* mvd indicates if this block is motion compensated */
  186. mvd = motion_x | motion_y;
  187. if((cbp | mvd | s->dquant ) == 0) {
  188. /* skip macroblock */
  189. s->skip_count++;
  190. h->current_mv_x=0;
  191. h->current_mv_y=0;
  192. return;
  193. }
  194. }
  195. /* MB is not skipped, encode MBA */
  196. put_bits(&s->pb, h261_mba_bits[(h->current_mba-h->previous_mba)-1], h261_mba_code[(h->current_mba-h->previous_mba)-1]);
  197. /* calculate MTYPE */
  198. if(!s->mb_intra){
  199. h->mtype++;
  200. if(mvd || s->loop_filter)
  201. h->mtype+=3;
  202. if(s->loop_filter)
  203. h->mtype+=3;
  204. if(cbp || s->dquant)
  205. h->mtype++;
  206. assert(h->mtype > 1);
  207. }
  208. if(s->dquant)
  209. h->mtype++;
  210. put_bits(&s->pb, h261_mtype_bits[h->mtype], h261_mtype_code[h->mtype]);
  211. h->mtype = h261_mtype_map[h->mtype];
  212. if(IS_QUANT(h->mtype)){
  213. ff_set_qscale(s,s->qscale+s->dquant);
  214. put_bits(&s->pb, 5, s->qscale);
  215. }
  216. if(IS_16X16(h->mtype)){
  217. mv_diff_x = (motion_x >> 1) - h->current_mv_x;
  218. mv_diff_y = (motion_y >> 1) - h->current_mv_y;
  219. h->current_mv_x = (motion_x >> 1);
  220. h->current_mv_y = (motion_y >> 1);
  221. h261_encode_motion(h,mv_diff_x);
  222. h261_encode_motion(h,mv_diff_y);
  223. }
  224. h->previous_mba = h->current_mba;
  225. if(HAS_CBP(h->mtype)){
  226. put_bits(&s->pb,h261_cbp_tab[cbp-1][1],h261_cbp_tab[cbp-1][0]);
  227. }
  228. for(i=0; i<6; i++) {
  229. /* encode each block */
  230. h261_encode_block(h, block[i], i);
  231. }
  232. if ( ( h->current_mba == 11 ) || ( h->current_mba == 22 ) || ( h->current_mba == 33 ) || ( !IS_16X16 ( h->mtype ) )){
  233. h->current_mv_x=0;
  234. h->current_mv_y=0;
  235. }
  236. }
  237. void ff_h261_encode_init(MpegEncContext *s){
  238. static int done = 0;
  239. if (!done) {
  240. done = 1;
  241. init_rl(&h261_rl_tcoeff);
  242. }
  243. s->min_qcoeff= -127;
  244. s->max_qcoeff= 127;
  245. s->y_dc_scale_table=
  246. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  247. }
  248. /**
  249. * encodes a 8x8 block.
  250. * @param block the 8x8 block
  251. * @param n block index (0-3 are luma, 4-5 are chroma)
  252. */
  253. static void h261_encode_block(H261Context * h, DCTELEM * block, int n){
  254. MpegEncContext * const s = &h->s;
  255. int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
  256. RLTable *rl;
  257. rl = &h261_rl_tcoeff;
  258. if (s->mb_intra) {
  259. /* DC coef */
  260. level = block[0];
  261. /* 255 cannot be represented, so we clamp */
  262. if (level > 254) {
  263. level = 254;
  264. block[0] = 254;
  265. }
  266. /* 0 cannot be represented also */
  267. else if (level < 1) {
  268. level = 1;
  269. block[0] = 1;
  270. }
  271. if (level == 128)
  272. put_bits(&s->pb, 8, 0xff);
  273. else
  274. put_bits(&s->pb, 8, level);
  275. i = 1;
  276. } else if((block[0]==1 || block[0] == -1) && (s->block_last_index[n] > -1)){
  277. //special case
  278. put_bits(&s->pb,2,block[0]>0 ? 2 : 3 );
  279. i = 1;
  280. } else {
  281. i = 0;
  282. }
  283. /* AC coefs */
  284. last_index = s->block_last_index[n];
  285. last_non_zero = i - 1;
  286. for (; i <= last_index; i++) {
  287. j = s->intra_scantable.permutated[i];
  288. level = block[j];
  289. if (level) {
  290. run = i - last_non_zero - 1;
  291. last = (i == last_index);
  292. sign = 0;
  293. slevel = level;
  294. if (level < 0) {
  295. sign = 1;
  296. level = -level;
  297. }
  298. code = get_rl_index(rl, 0 /*no last in H.261, EOB is used*/, run, level);
  299. if(run==0 && level < 16)
  300. code+=1;
  301. put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
  302. if (code == rl->n) {
  303. put_bits(&s->pb, 6, run);
  304. assert(slevel != 0);
  305. assert(level <= 127);
  306. put_bits(&s->pb, 8, slevel & 0xff);
  307. } else {
  308. put_bits(&s->pb, 1, sign);
  309. }
  310. last_non_zero = i;
  311. }
  312. }
  313. if(last_index > -1){
  314. put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]);// END OF BLOCK
  315. }
  316. }
  317. /***********************************************/
  318. /* decoding */
  319. static VLC h261_mba_vlc;
  320. static VLC h261_mtype_vlc;
  321. static VLC h261_mv_vlc;
  322. static VLC h261_cbp_vlc;
  323. void init_vlc_rl(RLTable *rl);
  324. static void h261_decode_init_vlc(H261Context *h){
  325. static int done = 0;
  326. if(!done){
  327. done = 1;
  328. init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
  329. h261_mba_bits, 1, 1,
  330. h261_mba_code, 1, 1);
  331. init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
  332. h261_mtype_bits, 1, 1,
  333. h261_mtype_code, 1, 1);
  334. init_vlc(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
  335. &h261_mv_tab[0][1], 2, 1,
  336. &h261_mv_tab[0][0], 2, 1);
  337. init_vlc(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
  338. &h261_cbp_tab[0][1], 2, 1,
  339. &h261_cbp_tab[0][0], 2, 1);
  340. init_rl(&h261_rl_tcoeff);
  341. init_vlc_rl(&h261_rl_tcoeff);
  342. }
  343. }
  344. static int h261_decode_init(AVCodecContext *avctx){
  345. H261Context *h= avctx->priv_data;
  346. MpegEncContext * const s = &h->s;
  347. // set defaults
  348. MPV_decode_defaults(s);
  349. s->avctx = avctx;
  350. s->width = s->avctx->coded_width;
  351. s->height = s->avctx->coded_height;
  352. s->codec_id = s->avctx->codec->id;
  353. s->out_format = FMT_H261;
  354. s->low_delay= 1;
  355. avctx->pix_fmt= PIX_FMT_YUV420P;
  356. s->codec_id= avctx->codec->id;
  357. h261_decode_init_vlc(h);
  358. h->gob_start_code_skipped = 0;
  359. return 0;
  360. }
  361. /**
  362. * decodes the group of blocks header or slice header.
  363. * @return <0 if an error occured
  364. */
  365. static int h261_decode_gob_header(H261Context *h){
  366. unsigned int val;
  367. MpegEncContext * const s = &h->s;
  368. if ( !h->gob_start_code_skipped ){
  369. /* Check for GOB Start Code */
  370. val = show_bits(&s->gb, 15);
  371. if(val)
  372. return -1;
  373. /* We have a GBSC */
  374. skip_bits(&s->gb, 16);
  375. }
  376. h->gob_start_code_skipped = 0;
  377. h->gob_number = get_bits(&s->gb, 4); /* GN */
  378. s->qscale = get_bits(&s->gb, 5); /* GQUANT */
  379. /* Check if gob_number is valid */
  380. if (s->mb_height==18){ //cif
  381. if ((h->gob_number<=0) || (h->gob_number>12))
  382. return -1;
  383. }
  384. else{ //qcif
  385. if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
  386. return -1;
  387. }
  388. /* GEI */
  389. while (get_bits1(&s->gb) != 0) {
  390. skip_bits(&s->gb, 8);
  391. }
  392. if(s->qscale==0)
  393. return -1;
  394. // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
  395. // subsequent macroblocks, MBA is the difference between the absolute addresses of
  396. // the macroblock and the last transmitted macroblock.
  397. h->current_mba = 0;
  398. h->mba_diff = 0;
  399. return 0;
  400. }
  401. /**
  402. * decodes the group of blocks / video packet header.
  403. * @return <0 if no resync found
  404. */
  405. static int ff_h261_resync(H261Context *h){
  406. MpegEncContext * const s = &h->s;
  407. int left, ret;
  408. if ( h->gob_start_code_skipped ){
  409. ret= h261_decode_gob_header(h);
  410. if(ret>=0)
  411. return 0;
  412. }
  413. else{
  414. if(show_bits(&s->gb, 15)==0){
  415. ret= h261_decode_gob_header(h);
  416. if(ret>=0)
  417. return 0;
  418. }
  419. //ok, its not where its supposed to be ...
  420. s->gb= s->last_resync_gb;
  421. align_get_bits(&s->gb);
  422. left= s->gb.size_in_bits - get_bits_count(&s->gb);
  423. for(;left>15+1+4+5; left-=8){
  424. if(show_bits(&s->gb, 15)==0){
  425. GetBitContext bak= s->gb;
  426. ret= h261_decode_gob_header(h);
  427. if(ret>=0)
  428. return 0;
  429. s->gb= bak;
  430. }
  431. skip_bits(&s->gb, 8);
  432. }
  433. }
  434. return -1;
  435. }
  436. /**
  437. * decodes skipped macroblocks
  438. * @return 0
  439. */
  440. static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
  441. {
  442. MpegEncContext * const s = &h->s;
  443. int i;
  444. s->mb_intra = 0;
  445. for(i=mba1; i<mba2; i++){
  446. int j, xy;
  447. s->mb_x= ((h->gob_number-1) % 2) * 11 + i % 11;
  448. s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11;
  449. xy = s->mb_x + s->mb_y * s->mb_stride;
  450. ff_init_block_index(s);
  451. ff_update_block_index(s);
  452. s->dsp.clear_blocks(s->block[0]);
  453. for(j=0;j<6;j++)
  454. s->block_last_index[j] = -1;
  455. s->mv_dir = MV_DIR_FORWARD;
  456. s->mv_type = MV_TYPE_16X16;
  457. s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
  458. s->mv[0][0][0] = 0;
  459. s->mv[0][0][1] = 0;
  460. s->mb_skiped = 1;
  461. h->mtype &= ~MB_TYPE_H261_FIL;
  462. MPV_decode_mb(s, s->block);
  463. }
  464. return 0;
  465. }
  466. static int decode_mv_component(GetBitContext *gb, int v){
  467. int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
  468. /* check if mv_diff is valid */
  469. if ( mv_diff < 0 )
  470. return v;
  471. mv_diff = mvmap[mv_diff];
  472. if(mv_diff && !get_bits1(gb))
  473. mv_diff= -mv_diff;
  474. v += mv_diff;
  475. if (v <=-16) v+= 32;
  476. else if(v >= 16) v-= 32;
  477. return v;
  478. }
  479. static int h261_decode_mb(H261Context *h){
  480. MpegEncContext * const s = &h->s;
  481. int i, cbp, xy;
  482. cbp = 63;
  483. // Read mba
  484. do{
  485. h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
  486. /* Check for slice end */
  487. /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
  488. if (h->mba_diff == MBA_STARTCODE){ // start code
  489. h->gob_start_code_skipped = 1;
  490. return SLICE_END;
  491. }
  492. }
  493. while( h->mba_diff == MBA_STUFFING ); // stuffing
  494. if ( h->mba_diff < 0 ){
  495. if ( get_bits_count(&s->gb) + 7 >= s->gb.size_in_bits )
  496. return SLICE_END;
  497. av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
  498. return SLICE_ERROR;
  499. }
  500. h->mba_diff += 1;
  501. h->current_mba += h->mba_diff;
  502. if ( h->current_mba > MBA_STUFFING )
  503. return SLICE_ERROR;
  504. s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
  505. s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
  506. xy = s->mb_x + s->mb_y * s->mb_stride;
  507. ff_init_block_index(s);
  508. ff_update_block_index(s);
  509. s->dsp.clear_blocks(s->block[0]);
  510. // Read mtype
  511. h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
  512. h->mtype = h261_mtype_map[h->mtype];
  513. // Read mquant
  514. if ( IS_QUANT ( h->mtype ) ){
  515. ff_set_qscale(s, get_bits(&s->gb, 5));
  516. }
  517. s->mb_intra = IS_INTRA4x4(h->mtype);
  518. // Read mv
  519. if ( IS_16X16 ( h->mtype ) ){
  520. // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
  521. // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
  522. // following three situations:
  523. // 1) evaluating MVD for macroblocks 1, 12 and 23;
  524. // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
  525. // 3) MTYPE of the previous macroblock was not MC.
  526. if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
  527. ( h->mba_diff != 1))
  528. {
  529. h->current_mv_x = 0;
  530. h->current_mv_y = 0;
  531. }
  532. h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
  533. h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
  534. }else{
  535. h->current_mv_x = 0;
  536. h->current_mv_y = 0;
  537. }
  538. // Read cbp
  539. if ( HAS_CBP( h->mtype ) ){
  540. cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
  541. }
  542. if(s->mb_intra){
  543. s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
  544. goto intra;
  545. }
  546. //set motion vectors
  547. s->mv_dir = MV_DIR_FORWARD;
  548. s->mv_type = MV_TYPE_16X16;
  549. s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
  550. s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
  551. s->mv[0][0][1] = h->current_mv_y * 2;
  552. intra:
  553. /* decode each block */
  554. if(s->mb_intra || HAS_CBP(h->mtype)){
  555. for (i = 0; i < 6; i++) {
  556. if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
  557. return SLICE_ERROR;
  558. }
  559. cbp+=cbp;
  560. }
  561. }
  562. MPV_decode_mb(s, s->block);
  563. return SLICE_OK;
  564. }
  565. /**
  566. * decodes a macroblock
  567. * @return <0 if an error occured
  568. */
  569. static int h261_decode_block(H261Context * h, DCTELEM * block,
  570. int n, int coded)
  571. {
  572. MpegEncContext * const s = &h->s;
  573. int code, level, i, j, run;
  574. RLTable *rl = &h261_rl_tcoeff;
  575. const uint8_t *scan_table;
  576. // For the variable length encoding there are two code tables, one being used for
  577. // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
  578. // for all other LEVELs except the first one in INTRA blocks which is fixed length
  579. // coded with 8 bits.
  580. // NOTE: the two code tables only differ in one VLC so we handle that manually.
  581. scan_table = s->intra_scantable.permutated;
  582. if (s->mb_intra){
  583. /* DC coef */
  584. level = get_bits(&s->gb, 8);
  585. // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
  586. if((level&0x7F) == 0){
  587. av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
  588. return -1;
  589. }
  590. // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
  591. if (level == 255)
  592. level = 128;
  593. block[0] = level;
  594. i = 1;
  595. }else if(coded){
  596. // Run Level Code
  597. // EOB Not possible for first level when cbp is available (that's why the table is different)
  598. // 0 1 1s
  599. // * * 0*
  600. int check = show_bits(&s->gb, 2);
  601. i = 0;
  602. if ( check & 0x2 ){
  603. skip_bits(&s->gb, 2);
  604. block[0] = ( check & 0x1 ) ? -1 : 1;
  605. i = 1;
  606. }
  607. }else{
  608. i = 0;
  609. }
  610. if(!coded){
  611. s->block_last_index[n] = i - 1;
  612. return 0;
  613. }
  614. for(;;){
  615. code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
  616. if (code < 0){
  617. av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
  618. return -1;
  619. }
  620. if (code == rl->n) {
  621. /* escape */
  622. // 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.
  623. run = get_bits(&s->gb, 6);
  624. level = get_sbits(&s->gb, 8);
  625. }else if(code == 0){
  626. break;
  627. }else{
  628. run = rl->table_run[code];
  629. level = rl->table_level[code];
  630. if (get_bits1(&s->gb))
  631. level = -level;
  632. }
  633. i += run;
  634. if (i >= 64){
  635. av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
  636. return -1;
  637. }
  638. j = scan_table[i];
  639. block[j] = level;
  640. i++;
  641. }
  642. s->block_last_index[n] = i-1;
  643. return 0;
  644. }
  645. /**
  646. * decodes the H261 picture header.
  647. * @return <0 if no startcode found
  648. */
  649. int h261_decode_picture_header(H261Context *h){
  650. MpegEncContext * const s = &h->s;
  651. int format, i;
  652. uint32_t startcode= 0;
  653. for(i= s->gb.size_in_bits - get_bits_count(&s->gb); i>24; i-=1){
  654. startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
  655. if(startcode == 0x10)
  656. break;
  657. }
  658. if (startcode != 0x10){
  659. av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
  660. return -1;
  661. }
  662. /* temporal reference */
  663. s->picture_number = get_bits(&s->gb, 5); /* picture timestamp */
  664. /* PTYPE starts here */
  665. skip_bits1(&s->gb); /* split screen off */
  666. skip_bits1(&s->gb); /* camera off */
  667. skip_bits1(&s->gb); /* freeze picture release off */
  668. format = get_bits1(&s->gb);
  669. //only 2 formats possible
  670. if (format == 0){//QCIF
  671. s->width = 176;
  672. s->height = 144;
  673. s->mb_width = 11;
  674. s->mb_height = 9;
  675. }else{//CIF
  676. s->width = 352;
  677. s->height = 288;
  678. s->mb_width = 22;
  679. s->mb_height = 18;
  680. }
  681. s->mb_num = s->mb_width * s->mb_height;
  682. skip_bits1(&s->gb); /* still image mode off */
  683. skip_bits1(&s->gb); /* Reserved */
  684. /* PEI */
  685. while (get_bits1(&s->gb) != 0){
  686. skip_bits(&s->gb, 8);
  687. }
  688. // h261 has no I-FRAMES, but if we pass I_TYPE for the first frame, the codec crashes if it does
  689. // not contain all I-blocks (e.g. when a packet is lost)
  690. s->pict_type = P_TYPE;
  691. h->gob_number = 0;
  692. return 0;
  693. }
  694. static int h261_decode_gob(H261Context *h){
  695. MpegEncContext * const s = &h->s;
  696. ff_set_qscale(s, s->qscale);
  697. /* decode mb's */
  698. while(h->current_mba <= MBA_STUFFING)
  699. {
  700. int ret;
  701. /* DCT & quantize */
  702. ret= h261_decode_mb(h);
  703. if(ret<0){
  704. if(ret==SLICE_END){
  705. h261_decode_mb_skipped(h, h->current_mba, 33);
  706. return 0;
  707. }
  708. av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
  709. return -1;
  710. }
  711. h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
  712. }
  713. return -1;
  714. }
  715. static int h261_find_frame_end(ParseContext *pc, AVCodecContext* avctx, const uint8_t *buf, int buf_size){
  716. int vop_found, i, j, bits_left, last_bits;
  717. uint32_t state;
  718. H261Context *h = avctx->priv_data;
  719. if(h){
  720. bits_left = h->bits_left;
  721. last_bits = h->last_bits;
  722. }
  723. else{
  724. bits_left = 0;
  725. last_bits = 0;
  726. }
  727. vop_found= pc->frame_start_found;
  728. state= pc->state;
  729. if(bits_left!=0 && !vop_found)
  730. state = state << (8-bits_left) | last_bits;
  731. i=0;
  732. if(!vop_found){
  733. for(i=0; i<buf_size; i++){
  734. state= (state<<8) | buf[i];
  735. for(j=0; j<8; j++){
  736. if(( ( (state<<j) | (buf[i]>>(8-j)) )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
  737. i++;
  738. vop_found=1;
  739. break;
  740. }
  741. }
  742. if(vop_found)
  743. break;
  744. }
  745. }
  746. if(vop_found){
  747. for(; i<buf_size; i++){
  748. if(avctx->flags & CODEC_FLAG_TRUNCATED)//XXX ffplay workaround, someone a better solution?
  749. state= (state<<8) | buf[i];
  750. for(j=0; j<8; j++){
  751. if(( ( (state<<j) | (buf[i]>>(8-j)) )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
  752. pc->frame_start_found=0;
  753. pc->state=-1;
  754. return i-3;
  755. }
  756. }
  757. }
  758. }
  759. pc->frame_start_found= vop_found;
  760. pc->state= state;
  761. return END_NOT_FOUND;
  762. }
  763. static int h261_parse(AVCodecParserContext *s,
  764. AVCodecContext *avctx,
  765. uint8_t **poutbuf, int *poutbuf_size,
  766. const uint8_t *buf, int buf_size)
  767. {
  768. ParseContext *pc = s->priv_data;
  769. int next;
  770. next= h261_find_frame_end(pc,avctx, buf, buf_size);
  771. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  772. *poutbuf = NULL;
  773. *poutbuf_size = 0;
  774. return buf_size;
  775. }
  776. *poutbuf = (uint8_t *)buf;
  777. *poutbuf_size = buf_size;
  778. return next;
  779. }
  780. /**
  781. * returns the number of bytes consumed for building the current frame
  782. */
  783. static int get_consumed_bytes(MpegEncContext *s, int buf_size){
  784. if(s->flags&CODEC_FLAG_TRUNCATED){
  785. int pos= (get_bits_count(&s->gb)+7)>>3;
  786. pos -= s->parse_context.last_index;
  787. if(pos<0) pos=0;// padding is not really read so this might be -1
  788. return pos;
  789. }else{
  790. int pos= get_bits_count(&s->gb)>>3;
  791. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  792. if(pos+10>buf_size) pos=buf_size; // oops ;)
  793. return pos;
  794. }
  795. }
  796. static int h261_decode_frame(AVCodecContext *avctx,
  797. void *data, int *data_size,
  798. uint8_t *buf, int buf_size)
  799. {
  800. H261Context *h= avctx->priv_data;
  801. MpegEncContext *s = &h->s;
  802. int ret;
  803. AVFrame *pict = data;
  804. #ifdef DEBUG
  805. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  806. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  807. #endif
  808. s->flags= avctx->flags;
  809. s->flags2= avctx->flags2;
  810. /* no supplementary picture */
  811. if (buf_size == 0) {
  812. return 0;
  813. }
  814. if(s->flags&CODEC_FLAG_TRUNCATED){
  815. int next;
  816. next= h261_find_frame_end(&s->parse_context,avctx, buf, buf_size);
  817. if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
  818. return buf_size;
  819. }
  820. retry:
  821. init_get_bits(&s->gb, buf, buf_size*8);
  822. if(!s->context_initialized){
  823. if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
  824. return -1;
  825. }
  826. //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
  827. if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
  828. int i= ff_find_unused_picture(s, 0);
  829. s->current_picture_ptr= &s->picture[i];
  830. }
  831. ret = h261_decode_picture_header(h);
  832. /* skip if the header was thrashed */
  833. if (ret < 0){
  834. av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
  835. return -1;
  836. }
  837. if (s->width != avctx->coded_width || s->height != avctx->coded_height){
  838. ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
  839. s->parse_context.buffer=0;
  840. MPV_common_end(s);
  841. s->parse_context= pc;
  842. }
  843. if (!s->context_initialized) {
  844. avcodec_set_dimensions(avctx, s->width, s->height);
  845. goto retry;
  846. }
  847. // for hurry_up==5
  848. s->current_picture.pict_type= s->pict_type;
  849. s->current_picture.key_frame= s->pict_type == I_TYPE;
  850. /* skip everything if we are in a hurry>=5 */
  851. if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
  852. if(MPV_frame_start(s, avctx) < 0)
  853. return -1;
  854. ff_er_frame_start(s);
  855. /* decode each macroblock */
  856. s->mb_x=0;
  857. s->mb_y=0;
  858. while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
  859. if(ff_h261_resync(h)<0)
  860. break;
  861. h261_decode_gob(h);
  862. }
  863. MPV_frame_end(s);
  864. assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
  865. assert(s->current_picture.pict_type == s->pict_type);
  866. *pict= *(AVFrame*)&s->current_picture;
  867. ff_print_debug_info(s, pict);
  868. /* Return the Picture timestamp as the frame number */
  869. /* we substract 1 because it is added on utils.c */
  870. avctx->frame_number = s->picture_number - 1;
  871. *data_size = sizeof(AVFrame);
  872. return get_consumed_bytes(s, buf_size);
  873. }
  874. static int h261_decode_end(AVCodecContext *avctx)
  875. {
  876. H261Context *h= avctx->priv_data;
  877. MpegEncContext *s = &h->s;
  878. MPV_common_end(s);
  879. return 0;
  880. }
  881. AVCodec h261_encoder = {
  882. "h261",
  883. CODEC_TYPE_VIDEO,
  884. CODEC_ID_H261,
  885. sizeof(H261Context),
  886. MPV_encode_init,
  887. MPV_encode_picture,
  888. MPV_encode_end,
  889. };
  890. AVCodec h261_decoder = {
  891. "h261",
  892. CODEC_TYPE_VIDEO,
  893. CODEC_ID_H261,
  894. sizeof(H261Context),
  895. h261_decode_init,
  896. NULL,
  897. h261_decode_end,
  898. h261_decode_frame,
  899. CODEC_CAP_TRUNCATED,
  900. };
  901. AVCodecParser h261_parser = {
  902. { CODEC_ID_H261 },
  903. sizeof(ParseContext),
  904. NULL,
  905. h261_parse,
  906. ff_parse_close,
  907. };