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.

1042 lines
28KB

  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->frame_rate_base /
  90. (1001 * (int64_t)s->avctx->frame_rate);
  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. s->dsp.clear_blocks(s->block[0]);
  451. for(j=0;j<6;j++)
  452. s->block_last_index[j] = -1;
  453. s->mv_dir = MV_DIR_FORWARD;
  454. s->mv_type = MV_TYPE_16X16;
  455. s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
  456. s->mv[0][0][0] = 0;
  457. s->mv[0][0][1] = 0;
  458. s->mb_skiped = 1;
  459. h->mtype &= ~MB_TYPE_H261_FIL;
  460. MPV_decode_mb(s, s->block);
  461. }
  462. return 0;
  463. }
  464. static int decode_mv_component(GetBitContext *gb, int v){
  465. int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
  466. /* check if mv_diff is valid */
  467. if ( mv_diff < 0 )
  468. return v;
  469. mv_diff = mvmap[mv_diff];
  470. if(mv_diff && !get_bits1(gb))
  471. mv_diff= -mv_diff;
  472. v += mv_diff;
  473. if (v <=-16) v+= 32;
  474. else if(v >= 16) v-= 32;
  475. return v;
  476. }
  477. static int h261_decode_mb(H261Context *h){
  478. MpegEncContext * const s = &h->s;
  479. int i, cbp, xy;
  480. cbp = 63;
  481. // Read mba
  482. do{
  483. h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
  484. /* Check for slice end */
  485. /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
  486. if (h->mba_diff == MBA_STARTCODE){ // start code
  487. h->gob_start_code_skipped = 1;
  488. return SLICE_END;
  489. }
  490. }
  491. while( h->mba_diff == MBA_STUFFING ); // stuffing
  492. if ( h->mba_diff < 0 ){
  493. if ( get_bits_count(&s->gb) + 7 >= s->gb.size_in_bits )
  494. return SLICE_END;
  495. av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
  496. return SLICE_ERROR;
  497. }
  498. h->mba_diff += 1;
  499. h->current_mba += h->mba_diff;
  500. if ( h->current_mba > MBA_STUFFING )
  501. return SLICE_ERROR;
  502. s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
  503. s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
  504. xy = s->mb_x + s->mb_y * s->mb_stride;
  505. ff_init_block_index(s);
  506. ff_update_block_index(s);
  507. s->dsp.clear_blocks(s->block[0]);
  508. // Read mtype
  509. h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
  510. h->mtype = h261_mtype_map[h->mtype];
  511. // Read mquant
  512. if ( IS_QUANT ( h->mtype ) ){
  513. ff_set_qscale(s, get_bits(&s->gb, 5));
  514. }
  515. s->mb_intra = IS_INTRA4x4(h->mtype);
  516. // Read mv
  517. if ( IS_16X16 ( h->mtype ) ){
  518. // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
  519. // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
  520. // following three situations:
  521. // 1) evaluating MVD for macroblocks 1, 12 and 23;
  522. // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
  523. // 3) MTYPE of the previous macroblock was not MC.
  524. if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
  525. ( h->mba_diff != 1))
  526. {
  527. h->current_mv_x = 0;
  528. h->current_mv_y = 0;
  529. }
  530. h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
  531. h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
  532. }else{
  533. h->current_mv_x = 0;
  534. h->current_mv_y = 0;
  535. }
  536. // Read cbp
  537. if ( HAS_CBP( h->mtype ) ){
  538. cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
  539. }
  540. if(s->mb_intra){
  541. s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
  542. goto intra;
  543. }
  544. //set motion vectors
  545. s->mv_dir = MV_DIR_FORWARD;
  546. s->mv_type = MV_TYPE_16X16;
  547. s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
  548. s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
  549. s->mv[0][0][1] = h->current_mv_y * 2;
  550. intra:
  551. /* decode each block */
  552. if(s->mb_intra || HAS_CBP(h->mtype)){
  553. for (i = 0; i < 6; i++) {
  554. if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
  555. return SLICE_ERROR;
  556. }
  557. cbp+=cbp;
  558. }
  559. }
  560. MPV_decode_mb(s, s->block);
  561. return SLICE_OK;
  562. }
  563. /**
  564. * decodes a macroblock
  565. * @return <0 if an error occured
  566. */
  567. static int h261_decode_block(H261Context * h, DCTELEM * block,
  568. int n, int coded)
  569. {
  570. MpegEncContext * const s = &h->s;
  571. int code, level, i, j, run;
  572. RLTable *rl = &h261_rl_tcoeff;
  573. const uint8_t *scan_table;
  574. // For the variable length encoding there are two code tables, one being used for
  575. // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
  576. // for all other LEVELs except the first one in INTRA blocks which is fixed length
  577. // coded with 8 bits.
  578. // NOTE: the two code tables only differ in one VLC so we handle that manually.
  579. scan_table = s->intra_scantable.permutated;
  580. if (s->mb_intra){
  581. /* DC coef */
  582. level = get_bits(&s->gb, 8);
  583. // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
  584. if((level&0x7F) == 0){
  585. av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
  586. return -1;
  587. }
  588. // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
  589. if (level == 255)
  590. level = 128;
  591. block[0] = level;
  592. i = 1;
  593. }else if(coded){
  594. // Run Level Code
  595. // EOB Not possible for first level when cbp is available (that's why the table is different)
  596. // 0 1 1s
  597. // * * 0*
  598. int check = show_bits(&s->gb, 2);
  599. i = 0;
  600. if ( check & 0x2 ){
  601. skip_bits(&s->gb, 2);
  602. block[0] = ( check & 0x1 ) ? -1 : 1;
  603. i = 1;
  604. }
  605. }else{
  606. i = 0;
  607. }
  608. if(!coded){
  609. s->block_last_index[n] = i - 1;
  610. return 0;
  611. }
  612. for(;;){
  613. code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
  614. if (code < 0){
  615. av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
  616. return -1;
  617. }
  618. if (code == rl->n) {
  619. /* escape */
  620. // 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.
  621. run = get_bits(&s->gb, 6);
  622. level = get_sbits(&s->gb, 8);
  623. }else if(code == 0){
  624. break;
  625. }else{
  626. run = rl->table_run[code];
  627. level = rl->table_level[code];
  628. if (get_bits1(&s->gb))
  629. level = -level;
  630. }
  631. i += run;
  632. if (i >= 64){
  633. av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
  634. return -1;
  635. }
  636. j = scan_table[i];
  637. block[j] = level;
  638. i++;
  639. }
  640. s->block_last_index[n] = i-1;
  641. return 0;
  642. }
  643. /**
  644. * decodes the H261 picture header.
  645. * @return <0 if no startcode found
  646. */
  647. int h261_decode_picture_header(H261Context *h){
  648. MpegEncContext * const s = &h->s;
  649. int format, i;
  650. uint32_t startcode= 0;
  651. for(i= s->gb.size_in_bits - get_bits_count(&s->gb); i>24; i-=1){
  652. startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
  653. if(startcode == 0x10)
  654. break;
  655. }
  656. if (startcode != 0x10){
  657. av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
  658. return -1;
  659. }
  660. /* temporal reference */
  661. s->picture_number = get_bits(&s->gb, 5); /* picture timestamp */
  662. /* PTYPE starts here */
  663. skip_bits1(&s->gb); /* split screen off */
  664. skip_bits1(&s->gb); /* camera off */
  665. skip_bits1(&s->gb); /* freeze picture release off */
  666. format = get_bits1(&s->gb);
  667. //only 2 formats possible
  668. if (format == 0){//QCIF
  669. s->width = 176;
  670. s->height = 144;
  671. s->mb_width = 11;
  672. s->mb_height = 9;
  673. }else{//CIF
  674. s->width = 352;
  675. s->height = 288;
  676. s->mb_width = 22;
  677. s->mb_height = 18;
  678. }
  679. s->mb_num = s->mb_width * s->mb_height;
  680. skip_bits1(&s->gb); /* still image mode off */
  681. skip_bits1(&s->gb); /* Reserved */
  682. /* PEI */
  683. while (get_bits1(&s->gb) != 0){
  684. skip_bits(&s->gb, 8);
  685. }
  686. // h261 has no I-FRAMES, but if we pass I_TYPE for the first frame, the codec crashes if it does
  687. // not contain all I-blocks (e.g. when a packet is lost)
  688. s->pict_type = P_TYPE;
  689. h->gob_number = 0;
  690. return 0;
  691. }
  692. static int h261_decode_gob(H261Context *h){
  693. MpegEncContext * const s = &h->s;
  694. ff_set_qscale(s, s->qscale);
  695. /* decode mb's */
  696. while(h->current_mba <= MBA_STUFFING)
  697. {
  698. int ret;
  699. /* DCT & quantize */
  700. ret= h261_decode_mb(h);
  701. if(ret<0){
  702. if(ret==SLICE_END){
  703. h261_decode_mb_skipped(h, h->current_mba, 33);
  704. return 0;
  705. }
  706. av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
  707. return -1;
  708. }
  709. h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
  710. }
  711. return -1;
  712. }
  713. static int h261_find_frame_end(ParseContext *pc, AVCodecContext* avctx, const uint8_t *buf, int buf_size){
  714. int vop_found, i, j;
  715. uint32_t state;
  716. vop_found= pc->frame_start_found;
  717. state= pc->state;
  718. for(i=0; i<buf_size && !vop_found; i++){
  719. state= (state<<8) | buf[i];
  720. for(j=0; j<8; j++){
  721. if(((state>>j)&0xFFFFF) == 0x00010){
  722. i++;
  723. vop_found=1;
  724. break;
  725. }
  726. }
  727. }
  728. if(vop_found){
  729. for(; i<buf_size; i++){
  730. state= (state<<8) | buf[i];
  731. for(j=0; j<8; j++){
  732. if(((state>>j)&0xFFFFF) == 0x00010){
  733. pc->frame_start_found=0;
  734. pc->state= state>>(2*8);
  735. return i-1;
  736. }
  737. }
  738. }
  739. }
  740. pc->frame_start_found= vop_found;
  741. pc->state= state;
  742. return END_NOT_FOUND;
  743. }
  744. static int h261_parse(AVCodecParserContext *s,
  745. AVCodecContext *avctx,
  746. uint8_t **poutbuf, int *poutbuf_size,
  747. const uint8_t *buf, int buf_size)
  748. {
  749. ParseContext *pc = s->priv_data;
  750. int next;
  751. next= h261_find_frame_end(pc,avctx, buf, buf_size);
  752. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  753. *poutbuf = NULL;
  754. *poutbuf_size = 0;
  755. return buf_size;
  756. }
  757. *poutbuf = (uint8_t *)buf;
  758. *poutbuf_size = buf_size;
  759. return next;
  760. }
  761. /**
  762. * returns the number of bytes consumed for building the current frame
  763. */
  764. static int get_consumed_bytes(MpegEncContext *s, int buf_size){
  765. int pos= get_bits_count(&s->gb)>>3;
  766. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  767. if(pos+10>buf_size) pos=buf_size; // oops ;)
  768. return pos;
  769. }
  770. static int h261_decode_frame(AVCodecContext *avctx,
  771. void *data, int *data_size,
  772. uint8_t *buf, int buf_size)
  773. {
  774. H261Context *h= avctx->priv_data;
  775. MpegEncContext *s = &h->s;
  776. int ret;
  777. AVFrame *pict = data;
  778. #ifdef DEBUG
  779. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  780. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  781. #endif
  782. s->flags= avctx->flags;
  783. s->flags2= avctx->flags2;
  784. h->gob_start_code_skipped=0;
  785. retry:
  786. init_get_bits(&s->gb, buf, buf_size*8);
  787. if(!s->context_initialized){
  788. if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
  789. return -1;
  790. }
  791. //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
  792. if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
  793. int i= ff_find_unused_picture(s, 0);
  794. s->current_picture_ptr= &s->picture[i];
  795. }
  796. ret = h261_decode_picture_header(h);
  797. /* skip if the header was thrashed */
  798. if (ret < 0){
  799. av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
  800. return -1;
  801. }
  802. if (s->width != avctx->coded_width || s->height != avctx->coded_height){
  803. ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
  804. s->parse_context.buffer=0;
  805. MPV_common_end(s);
  806. s->parse_context= pc;
  807. }
  808. if (!s->context_initialized) {
  809. avcodec_set_dimensions(avctx, s->width, s->height);
  810. goto retry;
  811. }
  812. // for hurry_up==5
  813. s->current_picture.pict_type= s->pict_type;
  814. s->current_picture.key_frame= s->pict_type == I_TYPE;
  815. /* skip everything if we are in a hurry>=5 */
  816. if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
  817. if(MPV_frame_start(s, avctx) < 0)
  818. return -1;
  819. ff_er_frame_start(s);
  820. /* decode each macroblock */
  821. s->mb_x=0;
  822. s->mb_y=0;
  823. while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
  824. if(ff_h261_resync(h)<0)
  825. break;
  826. h261_decode_gob(h);
  827. }
  828. MPV_frame_end(s);
  829. assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
  830. assert(s->current_picture.pict_type == s->pict_type);
  831. *pict= *(AVFrame*)s->current_picture_ptr;
  832. ff_print_debug_info(s, pict);
  833. /* Return the Picture timestamp as the frame number */
  834. /* we substract 1 because it is added on utils.c */
  835. avctx->frame_number = s->picture_number - 1;
  836. *data_size = sizeof(AVFrame);
  837. return get_consumed_bytes(s, buf_size);
  838. }
  839. static int h261_decode_end(AVCodecContext *avctx)
  840. {
  841. H261Context *h= avctx->priv_data;
  842. MpegEncContext *s = &h->s;
  843. MPV_common_end(s);
  844. return 0;
  845. }
  846. #ifdef CONFIG_ENCODERS
  847. AVCodec h261_encoder = {
  848. "h261",
  849. CODEC_TYPE_VIDEO,
  850. CODEC_ID_H261,
  851. sizeof(H261Context),
  852. MPV_encode_init,
  853. MPV_encode_picture,
  854. MPV_encode_end,
  855. };
  856. #endif
  857. AVCodec h261_decoder = {
  858. "h261",
  859. CODEC_TYPE_VIDEO,
  860. CODEC_ID_H261,
  861. sizeof(H261Context),
  862. h261_decode_init,
  863. NULL,
  864. h261_decode_end,
  865. h261_decode_frame,
  866. CODEC_CAP_DR1,
  867. };
  868. AVCodecParser h261_parser = {
  869. { CODEC_ID_H261 },
  870. sizeof(ParseContext),
  871. NULL,
  872. h261_parse,
  873. ff_parse_close,
  874. };