h261.c 29 KB

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