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.

1048 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 gob_start_code_skipped; // 1 if gob start code is already read before gob header is read
  50. }H261Context;
  51. void ff_h261_loop_filter(MpegEncContext *s){
  52. H261Context * h= (H261Context*)s;
  53. const int linesize = s->linesize;
  54. const int uvlinesize= s->uvlinesize;
  55. uint8_t *dest_y = s->dest[0];
  56. uint8_t *dest_cb= s->dest[1];
  57. uint8_t *dest_cr= s->dest[2];
  58. if(!(IS_FIL (h->mtype)))
  59. return;
  60. s->dsp.h261_loop_filter(dest_y , linesize);
  61. s->dsp.h261_loop_filter(dest_y + 8, linesize);
  62. s->dsp.h261_loop_filter(dest_y + 8 * linesize , linesize);
  63. s->dsp.h261_loop_filter(dest_y + 8 * linesize + 8, linesize);
  64. s->dsp.h261_loop_filter(dest_cb, uvlinesize);
  65. s->dsp.h261_loop_filter(dest_cr, uvlinesize);
  66. }
  67. static int ff_h261_get_picture_format(int width, int height){
  68. // QCIF
  69. if (width == 176 && height == 144)
  70. return 0;
  71. // CIF
  72. else if (width == 352 && height == 288)
  73. return 1;
  74. // ERROR
  75. else
  76. return -1;
  77. }
  78. static void h261_encode_block(H261Context * h, DCTELEM * block,
  79. int n);
  80. static int h261_decode_block(H261Context *h, DCTELEM *block,
  81. int n, int coded);
  82. void ff_h261_encode_picture_header(MpegEncContext * s, int picture_number){
  83. H261Context * h = (H261Context *) s;
  84. int format, temp_ref;
  85. align_put_bits(&s->pb);
  86. /* Update the pointer to last GOB */
  87. s->ptr_lastgob = pbBufPtr(&s->pb);
  88. put_bits(&s->pb, 20, 0x10); /* PSC */
  89. temp_ref= s->picture_number * (int64_t)30000 * s->avctx->time_base.num /
  90. (1001 * (int64_t)s->avctx->time_base.den); //FIXME maybe this should use a timestamp
  91. put_bits(&s->pb, 5, temp_ref & 0x1f); /* TemporalReference */
  92. put_bits(&s->pb, 1, 0); /* split screen off */
  93. put_bits(&s->pb, 1, 0); /* camera off */
  94. put_bits(&s->pb, 1, 0); /* freeze picture release off */
  95. format = ff_h261_get_picture_format(s->width, s->height);
  96. put_bits(&s->pb, 1, format); /* 0 == QCIF, 1 == CIF */
  97. put_bits(&s->pb, 1, 0); /* still image mode */
  98. put_bits(&s->pb, 1, 0); /* reserved */
  99. put_bits(&s->pb, 1, 0); /* no PEI */
  100. if(format == 0)
  101. h->gob_number = -1;
  102. else
  103. h->gob_number = 0;
  104. h->current_mba = 0;
  105. }
  106. /**
  107. * Encodes a group of blocks header.
  108. */
  109. static void h261_encode_gob_header(MpegEncContext * s, int mb_line){
  110. H261Context * h = (H261Context *)s;
  111. if(ff_h261_get_picture_format(s->width, s->height) == 0){
  112. h->gob_number+=2; // QCIF
  113. }
  114. else{
  115. h->gob_number++; // CIF
  116. }
  117. put_bits(&s->pb, 16, 1); /* GBSC */
  118. put_bits(&s->pb, 4, h->gob_number); /* GN */
  119. put_bits(&s->pb, 5, s->qscale); /* GQUANT */
  120. put_bits(&s->pb, 1, 0); /* no GEI */
  121. h->current_mba = 0;
  122. h->previous_mba = 0;
  123. h->current_mv_x=0;
  124. h->current_mv_y=0;
  125. }
  126. void ff_h261_reorder_mb_index(MpegEncContext* s){
  127. int index= s->mb_x + s->mb_y*s->mb_width;
  128. if(index % 33 == 0)
  129. h261_encode_gob_header(s,0);
  130. /* for CIF the GOB's are fragmented in the middle of a scanline
  131. that's why we need to adjust the x and y index of the macroblocks */
  132. if(ff_h261_get_picture_format(s->width,s->height) == 1){ // CIF
  133. s->mb_x = index % 11 ; index /= 11;
  134. s->mb_y = index % 3 ; index /= 3;
  135. s->mb_x+= 11*(index % 2); index /= 2;
  136. s->mb_y+= 3*index;
  137. ff_init_block_index(s);
  138. ff_update_block_index(s);
  139. }
  140. }
  141. static void h261_encode_motion(H261Context * h, int val){
  142. MpegEncContext * const s = &h->s;
  143. int sign, code;
  144. if(val==0){
  145. code = 0;
  146. put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]);
  147. }
  148. else{
  149. if(val > 15)
  150. val -=32;
  151. if(val < -16)
  152. val+=32;
  153. sign = val < 0;
  154. code = sign ? -val : val;
  155. put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]);
  156. put_bits(&s->pb,1,sign);
  157. }
  158. }
  159. static inline int get_cbp(MpegEncContext * s,
  160. DCTELEM block[6][64])
  161. {
  162. int i, cbp;
  163. cbp= 0;
  164. for (i = 0; i < 6; i++) {
  165. if (s->block_last_index[i] >= 0)
  166. cbp |= 1 << (5 - i);
  167. }
  168. return cbp;
  169. }
  170. void ff_h261_encode_mb(MpegEncContext * s,
  171. DCTELEM block[6][64],
  172. int motion_x, int motion_y)
  173. {
  174. H261Context * h = (H261Context *)s;
  175. int mvd, mv_diff_x, mv_diff_y, i, cbp;
  176. cbp = 63; // avoid warning
  177. mvd = 0;
  178. h->current_mba++;
  179. h->mtype = 0;
  180. if (!s->mb_intra){
  181. /* compute cbp */
  182. cbp= get_cbp(s, block);
  183. /* mvd indicates if this block is motion compensated */
  184. mvd = motion_x | motion_y;
  185. if((cbp | mvd | s->dquant ) == 0) {
  186. /* skip macroblock */
  187. s->skip_count++;
  188. h->current_mv_x=0;
  189. h->current_mv_y=0;
  190. return;
  191. }
  192. }
  193. /* MB is not skipped, encode MBA */
  194. put_bits(&s->pb, h261_mba_bits[(h->current_mba-h->previous_mba)-1], h261_mba_code[(h->current_mba-h->previous_mba)-1]);
  195. /* calculate MTYPE */
  196. if(!s->mb_intra){
  197. h->mtype++;
  198. if(mvd || s->loop_filter)
  199. h->mtype+=3;
  200. if(s->loop_filter)
  201. h->mtype+=3;
  202. if(cbp || s->dquant)
  203. h->mtype++;
  204. assert(h->mtype > 1);
  205. }
  206. if(s->dquant)
  207. h->mtype++;
  208. put_bits(&s->pb, h261_mtype_bits[h->mtype], h261_mtype_code[h->mtype]);
  209. h->mtype = h261_mtype_map[h->mtype];
  210. if(IS_QUANT(h->mtype)){
  211. ff_set_qscale(s,s->qscale+s->dquant);
  212. put_bits(&s->pb, 5, s->qscale);
  213. }
  214. if(IS_16X16(h->mtype)){
  215. mv_diff_x = (motion_x >> 1) - h->current_mv_x;
  216. mv_diff_y = (motion_y >> 1) - h->current_mv_y;
  217. h->current_mv_x = (motion_x >> 1);
  218. h->current_mv_y = (motion_y >> 1);
  219. h261_encode_motion(h,mv_diff_x);
  220. h261_encode_motion(h,mv_diff_y);
  221. }
  222. h->previous_mba = h->current_mba;
  223. if(HAS_CBP(h->mtype)){
  224. put_bits(&s->pb,h261_cbp_tab[cbp-1][1],h261_cbp_tab[cbp-1][0]);
  225. }
  226. for(i=0; i<6; i++) {
  227. /* encode each block */
  228. h261_encode_block(h, block[i], i);
  229. }
  230. if ( ( h->current_mba == 11 ) || ( h->current_mba == 22 ) || ( h->current_mba == 33 ) || ( !IS_16X16 ( h->mtype ) )){
  231. h->current_mv_x=0;
  232. h->current_mv_y=0;
  233. }
  234. }
  235. void ff_h261_encode_init(MpegEncContext *s){
  236. static int done = 0;
  237. if (!done) {
  238. done = 1;
  239. init_rl(&h261_rl_tcoeff, 1);
  240. }
  241. s->min_qcoeff= -127;
  242. s->max_qcoeff= 127;
  243. s->y_dc_scale_table=
  244. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  245. }
  246. /**
  247. * encodes a 8x8 block.
  248. * @param block the 8x8 block
  249. * @param n block index (0-3 are luma, 4-5 are chroma)
  250. */
  251. static void h261_encode_block(H261Context * h, DCTELEM * block, int n){
  252. MpegEncContext * const s = &h->s;
  253. int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
  254. RLTable *rl;
  255. rl = &h261_rl_tcoeff;
  256. if (s->mb_intra) {
  257. /* DC coef */
  258. level = block[0];
  259. /* 255 cannot be represented, so we clamp */
  260. if (level > 254) {
  261. level = 254;
  262. block[0] = 254;
  263. }
  264. /* 0 cannot be represented also */
  265. else if (level < 1) {
  266. level = 1;
  267. block[0] = 1;
  268. }
  269. if (level == 128)
  270. put_bits(&s->pb, 8, 0xff);
  271. else
  272. put_bits(&s->pb, 8, level);
  273. i = 1;
  274. } else if((block[0]==1 || block[0] == -1) && (s->block_last_index[n] > -1)){
  275. //special case
  276. put_bits(&s->pb,2,block[0]>0 ? 2 : 3 );
  277. i = 1;
  278. } else {
  279. i = 0;
  280. }
  281. /* AC coefs */
  282. last_index = s->block_last_index[n];
  283. last_non_zero = i - 1;
  284. for (; i <= last_index; i++) {
  285. j = s->intra_scantable.permutated[i];
  286. level = block[j];
  287. if (level) {
  288. run = i - last_non_zero - 1;
  289. last = (i == last_index);
  290. sign = 0;
  291. slevel = level;
  292. if (level < 0) {
  293. sign = 1;
  294. level = -level;
  295. }
  296. code = get_rl_index(rl, 0 /*no last in H.261, EOB is used*/, run, level);
  297. if(run==0 && level < 16)
  298. code+=1;
  299. put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
  300. if (code == rl->n) {
  301. put_bits(&s->pb, 6, run);
  302. assert(slevel != 0);
  303. assert(level <= 127);
  304. put_bits(&s->pb, 8, slevel & 0xff);
  305. } else {
  306. put_bits(&s->pb, 1, sign);
  307. }
  308. last_non_zero = i;
  309. }
  310. }
  311. if(last_index > -1){
  312. put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]);// END OF BLOCK
  313. }
  314. }
  315. /***********************************************/
  316. /* decoding */
  317. static VLC h261_mba_vlc;
  318. static VLC h261_mtype_vlc;
  319. static VLC h261_mv_vlc;
  320. static VLC h261_cbp_vlc;
  321. void init_vlc_rl(RLTable *rl, int use_static);
  322. static void h261_decode_init_vlc(H261Context *h){
  323. static int done = 0;
  324. if(!done){
  325. done = 1;
  326. init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
  327. h261_mba_bits, 1, 1,
  328. h261_mba_code, 1, 1, 1);
  329. init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
  330. h261_mtype_bits, 1, 1,
  331. h261_mtype_code, 1, 1, 1);
  332. init_vlc(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
  333. &h261_mv_tab[0][1], 2, 1,
  334. &h261_mv_tab[0][0], 2, 1, 1);
  335. init_vlc(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
  336. &h261_cbp_tab[0][1], 2, 1,
  337. &h261_cbp_tab[0][0], 2, 1, 1);
  338. init_rl(&h261_rl_tcoeff, 1);
  339. init_vlc_rl(&h261_rl_tcoeff, 1);
  340. }
  341. }
  342. static int h261_decode_init(AVCodecContext *avctx){
  343. H261Context *h= avctx->priv_data;
  344. MpegEncContext * const s = &h->s;
  345. // set defaults
  346. MPV_decode_defaults(s);
  347. s->avctx = avctx;
  348. s->width = s->avctx->coded_width;
  349. s->height = s->avctx->coded_height;
  350. s->codec_id = s->avctx->codec->id;
  351. s->out_format = FMT_H261;
  352. s->low_delay= 1;
  353. avctx->pix_fmt= PIX_FMT_YUV420P;
  354. s->codec_id= avctx->codec->id;
  355. h261_decode_init_vlc(h);
  356. h->gob_start_code_skipped = 0;
  357. return 0;
  358. }
  359. /**
  360. * decodes the group of blocks header or slice header.
  361. * @return <0 if an error occured
  362. */
  363. static int h261_decode_gob_header(H261Context *h){
  364. unsigned int val;
  365. MpegEncContext * const s = &h->s;
  366. if ( !h->gob_start_code_skipped ){
  367. /* Check for GOB Start Code */
  368. val = show_bits(&s->gb, 15);
  369. if(val)
  370. return -1;
  371. /* We have a GBSC */
  372. skip_bits(&s->gb, 16);
  373. }
  374. h->gob_start_code_skipped = 0;
  375. h->gob_number = get_bits(&s->gb, 4); /* GN */
  376. s->qscale = get_bits(&s->gb, 5); /* GQUANT */
  377. /* Check if gob_number is valid */
  378. if (s->mb_height==18){ //cif
  379. if ((h->gob_number<=0) || (h->gob_number>12))
  380. return -1;
  381. }
  382. else{ //qcif
  383. if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
  384. return -1;
  385. }
  386. /* GEI */
  387. while (get_bits1(&s->gb) != 0) {
  388. skip_bits(&s->gb, 8);
  389. }
  390. if(s->qscale==0)
  391. return -1;
  392. // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
  393. // subsequent macroblocks, MBA is the difference between the absolute addresses of
  394. // the macroblock and the last transmitted macroblock.
  395. h->current_mba = 0;
  396. h->mba_diff = 0;
  397. return 0;
  398. }
  399. /**
  400. * decodes the group of blocks / video packet header.
  401. * @return <0 if no resync found
  402. */
  403. static int ff_h261_resync(H261Context *h){
  404. MpegEncContext * const s = &h->s;
  405. int left, ret;
  406. if ( h->gob_start_code_skipped ){
  407. ret= h261_decode_gob_header(h);
  408. if(ret>=0)
  409. return 0;
  410. }
  411. else{
  412. if(show_bits(&s->gb, 15)==0){
  413. ret= h261_decode_gob_header(h);
  414. if(ret>=0)
  415. return 0;
  416. }
  417. //ok, its not where its supposed to be ...
  418. s->gb= s->last_resync_gb;
  419. align_get_bits(&s->gb);
  420. left= s->gb.size_in_bits - get_bits_count(&s->gb);
  421. for(;left>15+1+4+5; left-=8){
  422. if(show_bits(&s->gb, 15)==0){
  423. GetBitContext bak= s->gb;
  424. ret= h261_decode_gob_header(h);
  425. if(ret>=0)
  426. return 0;
  427. s->gb= bak;
  428. }
  429. skip_bits(&s->gb, 8);
  430. }
  431. }
  432. return -1;
  433. }
  434. /**
  435. * decodes skipped macroblocks
  436. * @return 0
  437. */
  438. static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
  439. {
  440. MpegEncContext * const s = &h->s;
  441. int i;
  442. s->mb_intra = 0;
  443. for(i=mba1; i<mba2; i++){
  444. int j, xy;
  445. s->mb_x= ((h->gob_number-1) % 2) * 11 + i % 11;
  446. s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11;
  447. xy = s->mb_x + s->mb_y * s->mb_stride;
  448. ff_init_block_index(s);
  449. ff_update_block_index(s);
  450. for(j=0;j<6;j++)
  451. s->block_last_index[j] = -1;
  452. s->mv_dir = MV_DIR_FORWARD;
  453. s->mv_type = MV_TYPE_16X16;
  454. s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
  455. s->mv[0][0][0] = 0;
  456. s->mv[0][0][1] = 0;
  457. s->mb_skipped = 1;
  458. h->mtype &= ~MB_TYPE_H261_FIL;
  459. MPV_decode_mb(s, s->block);
  460. }
  461. return 0;
  462. }
  463. static int decode_mv_component(GetBitContext *gb, int v){
  464. int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
  465. /* check if mv_diff is valid */
  466. if ( mv_diff < 0 )
  467. return v;
  468. mv_diff = mvmap[mv_diff];
  469. if(mv_diff && !get_bits1(gb))
  470. mv_diff= -mv_diff;
  471. v += mv_diff;
  472. if (v <=-16) v+= 32;
  473. else if(v >= 16) v-= 32;
  474. return v;
  475. }
  476. static int h261_decode_mb(H261Context *h){
  477. MpegEncContext * const s = &h->s;
  478. int i, cbp, xy;
  479. cbp = 63;
  480. // Read mba
  481. do{
  482. h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
  483. /* Check for slice end */
  484. /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
  485. if (h->mba_diff == MBA_STARTCODE){ // start code
  486. h->gob_start_code_skipped = 1;
  487. return SLICE_END;
  488. }
  489. }
  490. while( h->mba_diff == MBA_STUFFING ); // stuffing
  491. if ( h->mba_diff < 0 ){
  492. if ( get_bits_count(&s->gb) + 7 >= s->gb.size_in_bits )
  493. return SLICE_END;
  494. av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
  495. return SLICE_ERROR;
  496. }
  497. h->mba_diff += 1;
  498. h->current_mba += h->mba_diff;
  499. if ( h->current_mba > MBA_STUFFING )
  500. return SLICE_ERROR;
  501. s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
  502. s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
  503. xy = s->mb_x + s->mb_y * s->mb_stride;
  504. ff_init_block_index(s);
  505. ff_update_block_index(s);
  506. // Read mtype
  507. h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
  508. h->mtype = h261_mtype_map[h->mtype];
  509. // Read mquant
  510. if ( IS_QUANT ( h->mtype ) ){
  511. ff_set_qscale(s, get_bits(&s->gb, 5));
  512. }
  513. s->mb_intra = IS_INTRA4x4(h->mtype);
  514. // Read mv
  515. if ( IS_16X16 ( h->mtype ) ){
  516. // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
  517. // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
  518. // following three situations:
  519. // 1) evaluating MVD for macroblocks 1, 12 and 23;
  520. // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
  521. // 3) MTYPE of the previous macroblock was not MC.
  522. if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
  523. ( h->mba_diff != 1))
  524. {
  525. h->current_mv_x = 0;
  526. h->current_mv_y = 0;
  527. }
  528. h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
  529. h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
  530. }else{
  531. h->current_mv_x = 0;
  532. h->current_mv_y = 0;
  533. }
  534. // Read cbp
  535. if ( HAS_CBP( h->mtype ) ){
  536. cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
  537. }
  538. if(s->mb_intra){
  539. s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
  540. goto intra;
  541. }
  542. //set motion vectors
  543. s->mv_dir = MV_DIR_FORWARD;
  544. s->mv_type = MV_TYPE_16X16;
  545. s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
  546. s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
  547. s->mv[0][0][1] = h->current_mv_y * 2;
  548. intra:
  549. /* decode each block */
  550. if(s->mb_intra || HAS_CBP(h->mtype)){
  551. s->dsp.clear_blocks(s->block[0]);
  552. for (i = 0; i < 6; i++) {
  553. if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
  554. return SLICE_ERROR;
  555. }
  556. cbp+=cbp;
  557. }
  558. }else{
  559. for (i = 0; i < 6; i++)
  560. s->block_last_index[i]= -1;
  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;
  717. uint32_t state;
  718. vop_found= pc->frame_start_found;
  719. state= pc->state;
  720. for(i=0; i<buf_size && !vop_found; i++){
  721. state= (state<<8) | buf[i];
  722. for(j=0; j<8; j++){
  723. if(((state>>j)&0xFFFFF) == 0x00010){
  724. i++;
  725. vop_found=1;
  726. break;
  727. }
  728. }
  729. }
  730. if(vop_found){
  731. for(; i<buf_size; i++){
  732. state= (state<<8) | buf[i];
  733. for(j=0; j<8; j++){
  734. if(((state>>j)&0xFFFFF) == 0x00010){
  735. pc->frame_start_found=0;
  736. pc->state= state>>(2*8);
  737. return i-1;
  738. }
  739. }
  740. }
  741. }
  742. pc->frame_start_found= vop_found;
  743. pc->state= state;
  744. return END_NOT_FOUND;
  745. }
  746. static int h261_parse(AVCodecParserContext *s,
  747. AVCodecContext *avctx,
  748. uint8_t **poutbuf, int *poutbuf_size,
  749. const uint8_t *buf, int buf_size)
  750. {
  751. ParseContext *pc = s->priv_data;
  752. int next;
  753. next= h261_find_frame_end(pc,avctx, buf, buf_size);
  754. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  755. *poutbuf = NULL;
  756. *poutbuf_size = 0;
  757. return buf_size;
  758. }
  759. *poutbuf = (uint8_t *)buf;
  760. *poutbuf_size = buf_size;
  761. return next;
  762. }
  763. /**
  764. * returns the number of bytes consumed for building the current frame
  765. */
  766. static int get_consumed_bytes(MpegEncContext *s, int buf_size){
  767. int pos= get_bits_count(&s->gb)>>3;
  768. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  769. if(pos+10>buf_size) pos=buf_size; // oops ;)
  770. return pos;
  771. }
  772. static int h261_decode_frame(AVCodecContext *avctx,
  773. void *data, int *data_size,
  774. uint8_t *buf, int buf_size)
  775. {
  776. H261Context *h= avctx->priv_data;
  777. MpegEncContext *s = &h->s;
  778. int ret;
  779. AVFrame *pict = data;
  780. #ifdef DEBUG
  781. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  782. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  783. #endif
  784. s->flags= avctx->flags;
  785. s->flags2= avctx->flags2;
  786. h->gob_start_code_skipped=0;
  787. retry:
  788. init_get_bits(&s->gb, buf, buf_size*8);
  789. if(!s->context_initialized){
  790. if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
  791. return -1;
  792. }
  793. //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
  794. if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
  795. int i= ff_find_unused_picture(s, 0);
  796. s->current_picture_ptr= &s->picture[i];
  797. }
  798. ret = h261_decode_picture_header(h);
  799. /* skip if the header was thrashed */
  800. if (ret < 0){
  801. av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
  802. return -1;
  803. }
  804. if (s->width != avctx->coded_width || s->height != avctx->coded_height){
  805. ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
  806. s->parse_context.buffer=0;
  807. MPV_common_end(s);
  808. s->parse_context= pc;
  809. }
  810. if (!s->context_initialized) {
  811. avcodec_set_dimensions(avctx, s->width, s->height);
  812. goto retry;
  813. }
  814. // for hurry_up==5
  815. s->current_picture.pict_type= s->pict_type;
  816. s->current_picture.key_frame= s->pict_type == I_TYPE;
  817. /* skip everything if we are in a hurry>=5 */
  818. if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
  819. if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
  820. ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
  821. || avctx->skip_frame >= AVDISCARD_ALL)
  822. return get_consumed_bytes(s, buf_size);
  823. if(MPV_frame_start(s, avctx) < 0)
  824. return -1;
  825. ff_er_frame_start(s);
  826. /* decode each macroblock */
  827. s->mb_x=0;
  828. s->mb_y=0;
  829. while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
  830. if(ff_h261_resync(h)<0)
  831. break;
  832. h261_decode_gob(h);
  833. }
  834. MPV_frame_end(s);
  835. assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
  836. assert(s->current_picture.pict_type == s->pict_type);
  837. *pict= *(AVFrame*)s->current_picture_ptr;
  838. ff_print_debug_info(s, pict);
  839. /* Return the Picture timestamp as the frame number */
  840. /* we substract 1 because it is added on utils.c */
  841. avctx->frame_number = s->picture_number - 1;
  842. *data_size = sizeof(AVFrame);
  843. return get_consumed_bytes(s, buf_size);
  844. }
  845. static int h261_decode_end(AVCodecContext *avctx)
  846. {
  847. H261Context *h= avctx->priv_data;
  848. MpegEncContext *s = &h->s;
  849. MPV_common_end(s);
  850. return 0;
  851. }
  852. #ifdef CONFIG_ENCODERS
  853. AVCodec h261_encoder = {
  854. "h261",
  855. CODEC_TYPE_VIDEO,
  856. CODEC_ID_H261,
  857. sizeof(H261Context),
  858. MPV_encode_init,
  859. MPV_encode_picture,
  860. MPV_encode_end,
  861. };
  862. #endif
  863. AVCodec h261_decoder = {
  864. "h261",
  865. CODEC_TYPE_VIDEO,
  866. CODEC_ID_H261,
  867. sizeof(H261Context),
  868. h261_decode_init,
  869. NULL,
  870. h261_decode_end,
  871. h261_decode_frame,
  872. CODEC_CAP_DR1,
  873. };
  874. AVCodecParser h261_parser = {
  875. { CODEC_ID_H261 },
  876. sizeof(ParseContext),
  877. NULL,
  878. h261_parse,
  879. ff_parse_close,
  880. };