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.

1086 lines
30KB

  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,1,1);
  279. put_bits(&s->pb,1,block[0]>0 ? 0 : 1 );
  280. i = 1;
  281. } else {
  282. i = 0;
  283. }
  284. /* AC coefs */
  285. last_index = s->block_last_index[n];
  286. last_non_zero = i - 1;
  287. for (; i <= last_index; i++) {
  288. j = s->intra_scantable.permutated[i];
  289. level = block[j];
  290. if (level) {
  291. run = i - last_non_zero - 1;
  292. last = (i == last_index);
  293. sign = 0;
  294. slevel = level;
  295. if (level < 0) {
  296. sign = 1;
  297. level = -level;
  298. }
  299. code = get_rl_index(rl, 0 /*no last in H.261, EOB is used*/, run, level);
  300. if(run==0 && level < 16)
  301. code+=1;
  302. put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
  303. if (code == rl->n) {
  304. put_bits(&s->pb, 6, run);
  305. assert(slevel != 0);
  306. if(slevel < -127){
  307. slevel = -127;
  308. }
  309. else if(slevel > 127){
  310. slevel = 127;
  311. }
  312. put_bits(&s->pb, 8, slevel & 0xff);
  313. } else {
  314. put_bits(&s->pb, 1, sign);
  315. }
  316. last_non_zero = i;
  317. }
  318. }
  319. if(last_index > -1){
  320. put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]);// END OF BLOCK
  321. }
  322. }
  323. /***********************************************/
  324. /* decoding */
  325. static VLC h261_mba_vlc;
  326. static VLC h261_mtype_vlc;
  327. static VLC h261_mv_vlc;
  328. static VLC h261_cbp_vlc;
  329. void init_vlc_rl(RLTable *rl);
  330. static void h261_decode_init_vlc(H261Context *h){
  331. static int done = 0;
  332. if(!done){
  333. done = 1;
  334. init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
  335. h261_mba_bits, 1, 1,
  336. h261_mba_code, 1, 1);
  337. init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
  338. h261_mtype_bits, 1, 1,
  339. h261_mtype_code, 1, 1);
  340. init_vlc(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
  341. &h261_mv_tab[0][1], 2, 1,
  342. &h261_mv_tab[0][0], 2, 1);
  343. init_vlc(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
  344. &h261_cbp_tab[0][1], 2, 1,
  345. &h261_cbp_tab[0][0], 2, 1);
  346. init_rl(&h261_rl_tcoeff);
  347. init_vlc_rl(&h261_rl_tcoeff);
  348. }
  349. }
  350. static int h261_decode_init(AVCodecContext *avctx){
  351. H261Context *h= avctx->priv_data;
  352. MpegEncContext * const s = &h->s;
  353. // set defaults
  354. MPV_decode_defaults(s);
  355. s->avctx = avctx;
  356. s->width = s->avctx->coded_width;
  357. s->height = s->avctx->coded_height;
  358. s->codec_id = s->avctx->codec->id;
  359. s->out_format = FMT_H261;
  360. s->low_delay= 1;
  361. avctx->pix_fmt= PIX_FMT_YUV420P;
  362. s->codec_id= avctx->codec->id;
  363. h261_decode_init_vlc(h);
  364. h->gob_start_code_skipped = 0;
  365. return 0;
  366. }
  367. /**
  368. * decodes the group of blocks header or slice header.
  369. * @return <0 if an error occured
  370. */
  371. static int h261_decode_gob_header(H261Context *h){
  372. unsigned int val;
  373. MpegEncContext * const s = &h->s;
  374. if ( !h->gob_start_code_skipped ){
  375. /* Check for GOB Start Code */
  376. val = show_bits(&s->gb, 15);
  377. if(val)
  378. return -1;
  379. /* We have a GBSC */
  380. skip_bits(&s->gb, 16);
  381. }
  382. h->gob_start_code_skipped = 0;
  383. h->gob_number = get_bits(&s->gb, 4); /* GN */
  384. s->qscale = get_bits(&s->gb, 5); /* GQUANT */
  385. /* Check if gob_number is valid */
  386. if (s->mb_height==18){ //cif
  387. if ((h->gob_number<=0) || (h->gob_number>12))
  388. return -1;
  389. }
  390. else{ //qcif
  391. if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
  392. return -1;
  393. }
  394. /* GEI */
  395. while (get_bits1(&s->gb) != 0) {
  396. skip_bits(&s->gb, 8);
  397. }
  398. if(s->qscale==0)
  399. return -1;
  400. // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
  401. // subsequent macroblocks, MBA is the difference between the absolute addresses of
  402. // the macroblock and the last transmitted macroblock.
  403. h->current_mba = 0;
  404. h->mba_diff = 0;
  405. return 0;
  406. }
  407. /**
  408. * decodes the group of blocks / video packet header.
  409. * @return <0 if no resync found
  410. */
  411. static int ff_h261_resync(H261Context *h){
  412. MpegEncContext * const s = &h->s;
  413. int left, ret;
  414. if ( h->gob_start_code_skipped ){
  415. ret= h261_decode_gob_header(h);
  416. if(ret>=0)
  417. return 0;
  418. }
  419. else{
  420. if(show_bits(&s->gb, 15)==0){
  421. ret= h261_decode_gob_header(h);
  422. if(ret>=0)
  423. return 0;
  424. }
  425. //ok, its not where its supposed to be ...
  426. s->gb= s->last_resync_gb;
  427. align_get_bits(&s->gb);
  428. left= s->gb.size_in_bits - get_bits_count(&s->gb);
  429. for(;left>15+1+4+5; left-=8){
  430. if(show_bits(&s->gb, 15)==0){
  431. GetBitContext bak= s->gb;
  432. ret= h261_decode_gob_header(h);
  433. if(ret>=0)
  434. return 0;
  435. s->gb= bak;
  436. }
  437. skip_bits(&s->gb, 8);
  438. }
  439. }
  440. return -1;
  441. }
  442. /**
  443. * decodes skipped macroblocks
  444. * @return 0
  445. */
  446. static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
  447. {
  448. MpegEncContext * const s = &h->s;
  449. int i;
  450. s->mb_intra = 0;
  451. for(i=mba1; i<mba2; i++){
  452. int j, xy;
  453. s->mb_x= ((h->gob_number-1) % 2) * 11 + i % 11;
  454. s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11;
  455. xy = s->mb_x + s->mb_y * s->mb_stride;
  456. ff_init_block_index(s);
  457. ff_update_block_index(s);
  458. s->dsp.clear_blocks(s->block[0]);
  459. for(j=0;j<6;j++)
  460. s->block_last_index[j] = -1;
  461. s->mv_dir = MV_DIR_FORWARD;
  462. s->mv_type = MV_TYPE_16X16;
  463. s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
  464. s->mv[0][0][0] = 0;
  465. s->mv[0][0][1] = 0;
  466. s->mb_skiped = 1;
  467. h->mtype &= ~MB_TYPE_H261_FIL;
  468. MPV_decode_mb(s, s->block);
  469. }
  470. return 0;
  471. }
  472. static int decode_mv_component(GetBitContext *gb, int v){
  473. int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
  474. /* check if mv_diff is valid */
  475. if ( mv_diff < 0 )
  476. return v;
  477. mv_diff = mvmap[mv_diff];
  478. if(mv_diff && !get_bits1(gb))
  479. mv_diff= -mv_diff;
  480. v += mv_diff;
  481. if (v <=-16) v+= 32;
  482. else if(v >= 16) v-= 32;
  483. return v;
  484. }
  485. static int h261_decode_mb(H261Context *h){
  486. MpegEncContext * const s = &h->s;
  487. int i, cbp, xy;
  488. cbp = 63;
  489. // Read mba
  490. do{
  491. h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
  492. /* Check for slice end */
  493. /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
  494. if (h->mba_diff == MBA_STARTCODE){ // start code
  495. h->gob_start_code_skipped = 1;
  496. return SLICE_END;
  497. }
  498. }
  499. while( h->mba_diff == MBA_STUFFING ); // stuffing
  500. if ( h->mba_diff < 0 ){
  501. if ( get_bits_count(&s->gb) + 7 >= s->gb.size_in_bits )
  502. return SLICE_END;
  503. av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
  504. return SLICE_ERROR;
  505. }
  506. h->mba_diff += 1;
  507. h->current_mba += h->mba_diff;
  508. if ( h->current_mba > MBA_STUFFING )
  509. return SLICE_ERROR;
  510. s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
  511. s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
  512. xy = s->mb_x + s->mb_y * s->mb_stride;
  513. ff_init_block_index(s);
  514. ff_update_block_index(s);
  515. s->dsp.clear_blocks(s->block[0]);
  516. // Read mtype
  517. h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
  518. h->mtype = h261_mtype_map[h->mtype];
  519. // Read mquant
  520. if ( IS_QUANT ( h->mtype ) ){
  521. ff_set_qscale(s, get_bits(&s->gb, 5));
  522. }
  523. s->mb_intra = IS_INTRA4x4(h->mtype);
  524. // Read mv
  525. if ( IS_16X16 ( h->mtype ) ){
  526. // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
  527. // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
  528. // following three situations:
  529. // 1) evaluating MVD for macroblocks 1, 12 and 23;
  530. // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
  531. // 3) MTYPE of the previous macroblock was not MC.
  532. if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
  533. ( h->mba_diff != 1))
  534. {
  535. h->current_mv_x = 0;
  536. h->current_mv_y = 0;
  537. }
  538. h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
  539. h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
  540. }else{
  541. h->current_mv_x = 0;
  542. h->current_mv_y = 0;
  543. }
  544. // Read cbp
  545. if ( HAS_CBP( h->mtype ) ){
  546. cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
  547. }
  548. if(s->mb_intra){
  549. s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
  550. goto intra;
  551. }
  552. //set motion vectors
  553. s->mv_dir = MV_DIR_FORWARD;
  554. s->mv_type = MV_TYPE_16X16;
  555. s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
  556. s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
  557. s->mv[0][0][1] = h->current_mv_y * 2;
  558. intra:
  559. /* decode each block */
  560. if(s->mb_intra || HAS_CBP(h->mtype)){
  561. for (i = 0; i < 6; i++) {
  562. if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
  563. return SLICE_ERROR;
  564. }
  565. cbp+=cbp;
  566. }
  567. }
  568. MPV_decode_mb(s, s->block);
  569. return SLICE_OK;
  570. }
  571. /**
  572. * decodes a macroblock
  573. * @return <0 if an error occured
  574. */
  575. static int h261_decode_block(H261Context * h, DCTELEM * block,
  576. int n, int coded)
  577. {
  578. MpegEncContext * const s = &h->s;
  579. int code, level, i, j, run;
  580. RLTable *rl = &h261_rl_tcoeff;
  581. const uint8_t *scan_table;
  582. // For the variable length encoding there are two code tables, one being used for
  583. // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
  584. // for all other LEVELs except the first one in INTRA blocks which is fixed length
  585. // coded with 8 bits.
  586. // NOTE: the two code tables only differ in one VLC so we handle that manually.
  587. scan_table = s->intra_scantable.permutated;
  588. if (s->mb_intra){
  589. /* DC coef */
  590. level = get_bits(&s->gb, 8);
  591. // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
  592. if((level&0x7F) == 0){
  593. av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
  594. return -1;
  595. }
  596. // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
  597. if (level == 255)
  598. level = 128;
  599. block[0] = level;
  600. i = 1;
  601. }else if(coded){
  602. // Run Level Code
  603. // EOB Not possible for first level when cbp is available (that's why the table is different)
  604. // 0 1 1s
  605. // * * 0*
  606. int check = show_bits(&s->gb, 2);
  607. i = 0;
  608. if ( check & 0x2 ){
  609. skip_bits(&s->gb, 2);
  610. block[0] = ( check & 0x1 ) ? -1 : 1;
  611. i = 1;
  612. }
  613. }else{
  614. i = 0;
  615. }
  616. if(!coded){
  617. s->block_last_index[n] = i - 1;
  618. return 0;
  619. }
  620. for(;;){
  621. code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
  622. if (code < 0){
  623. av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
  624. return -1;
  625. }
  626. if (code == rl->n) {
  627. /* escape */
  628. // 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.
  629. run = get_bits(&s->gb, 6);
  630. level = (int8_t)get_bits(&s->gb, 8);
  631. }else if(code == 0){
  632. break;
  633. }else{
  634. run = rl->table_run[code];
  635. level = rl->table_level[code];
  636. if (get_bits1(&s->gb))
  637. level = -level;
  638. }
  639. i += run;
  640. if (i >= 64){
  641. av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
  642. return -1;
  643. }
  644. j = scan_table[i];
  645. block[j] = level;
  646. i++;
  647. }
  648. s->block_last_index[n] = i-1;
  649. return 0;
  650. }
  651. /**
  652. * decodes the H261 picture header.
  653. * @return <0 if no startcode found
  654. */
  655. int h261_decode_picture_header(H261Context *h){
  656. MpegEncContext * const s = &h->s;
  657. int format, i;
  658. uint32_t startcode= 0;
  659. for(i= s->gb.size_in_bits - get_bits_count(&s->gb); i>24; i-=1){
  660. startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
  661. if(startcode == 0x10)
  662. break;
  663. }
  664. if (startcode != 0x10){
  665. av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
  666. return -1;
  667. }
  668. /* temporal reference */
  669. s->picture_number = get_bits(&s->gb, 5); /* picture timestamp */
  670. /* PTYPE starts here */
  671. skip_bits1(&s->gb); /* split screen off */
  672. skip_bits1(&s->gb); /* camera off */
  673. skip_bits1(&s->gb); /* freeze picture release off */
  674. format = get_bits1(&s->gb);
  675. //only 2 formats possible
  676. if (format == 0){//QCIF
  677. s->width = 176;
  678. s->height = 144;
  679. s->mb_width = 11;
  680. s->mb_height = 9;
  681. }else{//CIF
  682. s->width = 352;
  683. s->height = 288;
  684. s->mb_width = 22;
  685. s->mb_height = 18;
  686. }
  687. s->mb_num = s->mb_width * s->mb_height;
  688. skip_bits1(&s->gb); /* still image mode off */
  689. skip_bits1(&s->gb); /* Reserved */
  690. /* PEI */
  691. while (get_bits1(&s->gb) != 0){
  692. skip_bits(&s->gb, 8);
  693. }
  694. // h261 has no I-FRAMES, but if we pass I_TYPE for the first frame, the codec crashes if it does
  695. // not contain all I-blocks (e.g. when a packet is lost)
  696. s->pict_type = P_TYPE;
  697. h->gob_number = 0;
  698. return 0;
  699. }
  700. static int h261_decode_gob(H261Context *h){
  701. MpegEncContext * const s = &h->s;
  702. ff_set_qscale(s, s->qscale);
  703. /* decode mb's */
  704. while(h->current_mba <= MBA_STUFFING)
  705. {
  706. int ret;
  707. /* DCT & quantize */
  708. ret= h261_decode_mb(h);
  709. if(ret<0){
  710. if(ret==SLICE_END){
  711. h261_decode_mb_skipped(h, h->current_mba, 33);
  712. return 0;
  713. }
  714. av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
  715. return -1;
  716. }
  717. h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
  718. }
  719. return -1;
  720. }
  721. static int h261_find_frame_end(ParseContext *pc, AVCodecContext* avctx, const uint8_t *buf, int buf_size){
  722. int vop_found, i, j, bits_left, last_bits;
  723. uint32_t state;
  724. H261Context *h = avctx->priv_data;
  725. if(h){
  726. bits_left = h->bits_left;
  727. last_bits = h->last_bits;
  728. }
  729. else{
  730. bits_left = 0;
  731. last_bits = 0;
  732. }
  733. vop_found= pc->frame_start_found;
  734. state= pc->state;
  735. if(bits_left!=0 && !vop_found)
  736. state = state << (8-bits_left) | last_bits;
  737. i=0;
  738. if(!vop_found){
  739. for(i=0; i<buf_size; i++){
  740. state= (state<<8) | buf[i];
  741. for(j=0; j<8; j++){
  742. if(( ( (state<<j) | (buf[i]>>(8-j)) )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
  743. i++;
  744. vop_found=1;
  745. break;
  746. }
  747. }
  748. if(vop_found)
  749. break;
  750. }
  751. }
  752. if(vop_found){
  753. for(; i<buf_size; i++){
  754. if(avctx->flags & CODEC_FLAG_TRUNCATED)//XXX ffplay workaround, someone a better solution?
  755. state= (state<<8) | buf[i];
  756. for(j=0; j<8; j++){
  757. if(( ( (state<<j) | (buf[i]>>(8-j)) )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
  758. pc->frame_start_found=0;
  759. pc->state=-1;
  760. return i-3;
  761. }
  762. }
  763. }
  764. }
  765. pc->frame_start_found= vop_found;
  766. pc->state= state;
  767. return END_NOT_FOUND;
  768. }
  769. static int h261_parse(AVCodecParserContext *s,
  770. AVCodecContext *avctx,
  771. uint8_t **poutbuf, int *poutbuf_size,
  772. const uint8_t *buf, int buf_size)
  773. {
  774. ParseContext *pc = s->priv_data;
  775. int next;
  776. next= h261_find_frame_end(pc,avctx, buf, buf_size);
  777. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  778. *poutbuf = NULL;
  779. *poutbuf_size = 0;
  780. return buf_size;
  781. }
  782. *poutbuf = (uint8_t *)buf;
  783. *poutbuf_size = buf_size;
  784. return next;
  785. }
  786. /**
  787. * returns the number of bytes consumed for building the current frame
  788. */
  789. static int get_consumed_bytes(MpegEncContext *s, int buf_size){
  790. if(s->flags&CODEC_FLAG_TRUNCATED){
  791. int pos= (get_bits_count(&s->gb)+7)>>3;
  792. pos -= s->parse_context.last_index;
  793. if(pos<0) pos=0;// padding is not really read so this might be -1
  794. return pos;
  795. }else{
  796. int pos= get_bits_count(&s->gb)>>3;
  797. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  798. if(pos+10>buf_size) pos=buf_size; // oops ;)
  799. return pos;
  800. }
  801. }
  802. static int h261_decode_frame(AVCodecContext *avctx,
  803. void *data, int *data_size,
  804. uint8_t *buf, int buf_size)
  805. {
  806. H261Context *h= avctx->priv_data;
  807. MpegEncContext *s = &h->s;
  808. int ret;
  809. AVFrame *pict = data;
  810. #ifdef DEBUG
  811. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  812. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  813. #endif
  814. s->flags= avctx->flags;
  815. s->flags2= avctx->flags2;
  816. /* no supplementary picture */
  817. if (buf_size == 0) {
  818. return 0;
  819. }
  820. if(s->flags&CODEC_FLAG_TRUNCATED){
  821. int next;
  822. next= h261_find_frame_end(&s->parse_context,avctx, buf, buf_size);
  823. if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
  824. return buf_size;
  825. }
  826. retry:
  827. init_get_bits(&s->gb, buf, buf_size*8);
  828. if(!s->context_initialized){
  829. if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
  830. return -1;
  831. }
  832. //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
  833. if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
  834. int i= ff_find_unused_picture(s, 0);
  835. s->current_picture_ptr= &s->picture[i];
  836. }
  837. ret = h261_decode_picture_header(h);
  838. /* skip if the header was thrashed */
  839. if (ret < 0){
  840. av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
  841. return -1;
  842. }
  843. if (s->width != avctx->coded_width || s->height != avctx->coded_height){
  844. ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
  845. s->parse_context.buffer=0;
  846. MPV_common_end(s);
  847. s->parse_context= pc;
  848. }
  849. if (!s->context_initialized) {
  850. avcodec_set_dimensions(avctx, s->width, s->height);
  851. goto retry;
  852. }
  853. // for hurry_up==5
  854. s->current_picture.pict_type= s->pict_type;
  855. s->current_picture.key_frame= s->pict_type == I_TYPE;
  856. /* skip everything if we are in a hurry>=5 */
  857. if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
  858. if(MPV_frame_start(s, avctx) < 0)
  859. return -1;
  860. ff_er_frame_start(s);
  861. /* decode each macroblock */
  862. s->mb_x=0;
  863. s->mb_y=0;
  864. while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
  865. if(ff_h261_resync(h)<0)
  866. break;
  867. h261_decode_gob(h);
  868. }
  869. MPV_frame_end(s);
  870. assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
  871. assert(s->current_picture.pict_type == s->pict_type);
  872. *pict= *(AVFrame*)&s->current_picture;
  873. ff_print_debug_info(s, pict);
  874. /* Return the Picture timestamp as the frame number */
  875. /* we substract 1 because it is added on utils.c */
  876. avctx->frame_number = s->picture_number - 1;
  877. *data_size = sizeof(AVFrame);
  878. return get_consumed_bytes(s, buf_size);
  879. }
  880. static int h261_decode_end(AVCodecContext *avctx)
  881. {
  882. H261Context *h= avctx->priv_data;
  883. MpegEncContext *s = &h->s;
  884. MPV_common_end(s);
  885. return 0;
  886. }
  887. AVCodec h261_encoder = {
  888. "h261",
  889. CODEC_TYPE_VIDEO,
  890. CODEC_ID_H261,
  891. sizeof(H261Context),
  892. MPV_encode_init,
  893. MPV_encode_picture,
  894. MPV_encode_end,
  895. };
  896. AVCodec h261_decoder = {
  897. "h261",
  898. CODEC_TYPE_VIDEO,
  899. CODEC_ID_H261,
  900. sizeof(H261Context),
  901. h261_decode_init,
  902. NULL,
  903. h261_decode_end,
  904. h261_decode_frame,
  905. CODEC_CAP_TRUNCATED,
  906. };
  907. AVCodecParser h261_parser = {
  908. { CODEC_ID_H261 },
  909. sizeof(ParseContext),
  910. NULL,
  911. h261_parse,
  912. ff_parse_close,
  913. };