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.

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