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.

1113 lines
31KB

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