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.

2057 lines
66KB

  1. /*
  2. * MPEG1 encoder / MPEG2 decoder
  3. * Copyright (c) 2000,2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. //#define DEBUG
  20. #include "avcodec.h"
  21. #include "dsputil.h"
  22. #include "mpegvideo.h"
  23. #include "mpeg12data.h"
  24. #if 1
  25. #define PRINT_QP(a, b) {}
  26. #else
  27. #define PRINT_QP(a, b) printf(a, b)
  28. #endif
  29. /* Start codes. */
  30. #define SEQ_END_CODE 0x000001b7
  31. #define SEQ_START_CODE 0x000001b3
  32. #define GOP_START_CODE 0x000001b8
  33. #define PICTURE_START_CODE 0x00000100
  34. #define SLICE_MIN_START_CODE 0x00000101
  35. #define SLICE_MAX_START_CODE 0x000001af
  36. #define EXT_START_CODE 0x000001b5
  37. #define USER_START_CODE 0x000001b2
  38. #define DC_VLC_BITS 9
  39. #define MV_VLC_BITS 9
  40. #define MBINCR_VLC_BITS 9
  41. #define MB_PAT_VLC_BITS 9
  42. #define MB_PTYPE_VLC_BITS 6
  43. #define MB_BTYPE_VLC_BITS 6
  44. #define TEX_VLC_BITS 9
  45. static void mpeg1_encode_block(MpegEncContext *s,
  46. DCTELEM *block,
  47. int component);
  48. static void mpeg1_encode_motion(MpegEncContext *s, int val);
  49. static void mpeg1_skip_picture(MpegEncContext *s, int pict_num);
  50. static inline int mpeg1_decode_block_inter(MpegEncContext *s,
  51. DCTELEM *block,
  52. int n);
  53. static inline int mpeg1_decode_block_intra(MpegEncContext *s,
  54. DCTELEM *block,
  55. int n);
  56. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
  57. DCTELEM *block,
  58. int n);
  59. static inline int mpeg2_decode_block_intra(MpegEncContext *s,
  60. DCTELEM *block,
  61. int n);
  62. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
  63. #ifdef CONFIG_ENCODERS
  64. static UINT16 mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
  65. static UINT8 fcode_tab[MAX_MV*2+1];
  66. static uint32_t uni_mpeg1_ac_vlc_bits[64*64*2];
  67. static uint8_t uni_mpeg1_ac_vlc_len [64*64*2];
  68. #endif
  69. static inline int get_bits_diff(MpegEncContext *s){
  70. int bits,ret;
  71. bits= get_bit_count(&s->pb);
  72. ret= bits - s->last_bits;
  73. s->last_bits=bits;
  74. return ret;
  75. }
  76. static void init_2d_vlc_rl(RLTable *rl)
  77. {
  78. int i;
  79. init_vlc(&rl->vlc, TEX_VLC_BITS, rl->n + 2,
  80. &rl->table_vlc[0][1], 4, 2,
  81. &rl->table_vlc[0][0], 4, 2);
  82. rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
  83. for(i=0; i<rl->vlc.table_size; i++){
  84. int code= rl->vlc.table[i][0];
  85. int len = rl->vlc.table[i][1];
  86. int level, run;
  87. if(len==0){ // illegal code
  88. run= 65;
  89. level= MAX_LEVEL;
  90. }else if(len<0){ //more bits needed
  91. run= 0;
  92. level= code;
  93. }else{
  94. if(code==rl->n){ //esc
  95. run= 65;
  96. level= 0;
  97. }else if(code==rl->n+1){ //eob
  98. run= 0;
  99. level= 127;
  100. }else{
  101. run= rl->table_run [code] + 1;
  102. level= rl->table_level[code];
  103. }
  104. }
  105. rl->rl_vlc[0][i].len= len;
  106. rl->rl_vlc[0][i].level= level;
  107. rl->rl_vlc[0][i].run= run;
  108. }
  109. }
  110. static void init_uni_ac_vlc(RLTable *rl, uint32_t *uni_ac_vlc_bits, uint8_t *uni_ac_vlc_len){
  111. int i;
  112. for(i=0; i<128; i++){
  113. int level= i-64;
  114. int run;
  115. for(run=0; run<64; run++){
  116. int len, bits, code;
  117. int alevel= ABS(level);
  118. int sign= (level>>31)&1;
  119. if (alevel > rl->max_level[0][run])
  120. code= 111; /*rl->n*/
  121. else
  122. code= rl->index_run[0][run] + alevel - 1;
  123. if (code < 111 /* rl->n */) {
  124. /* store the vlc & sign at once */
  125. len= mpeg1_vlc[code][1]+1;
  126. bits= (mpeg1_vlc[code][0]<<1) + sign;
  127. } else {
  128. len= mpeg1_vlc[111/*rl->n*/][1]+6;
  129. bits= mpeg1_vlc[111/*rl->n*/][0]<<6;
  130. bits|= run;
  131. if (alevel < 128) {
  132. bits<<=8; len+=8;
  133. bits|= level & 0xff;
  134. } else {
  135. bits<<=16; len+=16;
  136. bits|= level & 0xff;
  137. if (level < 0) {
  138. bits|= 0x8001 + level + 255;
  139. } else {
  140. bits|= level & 0xffff;
  141. }
  142. }
  143. }
  144. uni_ac_vlc_bits[UNI_AC_ENC_INDEX(run, i)]= bits;
  145. uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len;
  146. }
  147. }
  148. }
  149. static void put_header(MpegEncContext *s, int header)
  150. {
  151. align_put_bits(&s->pb);
  152. put_bits(&s->pb, 16, header>>16);
  153. put_bits(&s->pb, 16, header&0xFFFF);
  154. }
  155. /* put sequence header if needed */
  156. static void mpeg1_encode_sequence_header(MpegEncContext *s)
  157. {
  158. unsigned int vbv_buffer_size;
  159. unsigned int fps, v;
  160. int n, i;
  161. UINT64 time_code;
  162. float best_aspect_error= 1E10;
  163. float aspect_ratio= s->avctx->aspect_ratio;
  164. if(aspect_ratio==0.0) aspect_ratio= s->width / (float)s->height; //pixel aspect 1:1 (VGA)
  165. if (s->current_picture.key_frame) {
  166. /* mpeg1 header repeated every gop */
  167. put_header(s, SEQ_START_CODE);
  168. /* search closest frame rate */
  169. {
  170. int i, dmin, d;
  171. s->frame_rate_index = 0;
  172. dmin = 0x7fffffff;
  173. for(i=1;i<9;i++) {
  174. d = abs(s->frame_rate - frame_rate_tab[i]);
  175. if (d < dmin) {
  176. dmin = d;
  177. s->frame_rate_index = i;
  178. }
  179. }
  180. }
  181. put_bits(&s->pb, 12, s->width);
  182. put_bits(&s->pb, 12, s->height);
  183. for(i=1; i<15; i++){
  184. float error= mpeg1_aspect[i] - s->width/(s->height*aspect_ratio);
  185. error= ABS(error);
  186. if(error < best_aspect_error){
  187. best_aspect_error= error;
  188. s->aspect_ratio_info= i;
  189. }
  190. }
  191. put_bits(&s->pb, 4, s->aspect_ratio_info);
  192. put_bits(&s->pb, 4, s->frame_rate_index);
  193. v = s->bit_rate / 400;
  194. if (v > 0x3ffff)
  195. v = 0x3ffff;
  196. put_bits(&s->pb, 18, v);
  197. put_bits(&s->pb, 1, 1); /* marker */
  198. if(s->avctx->rc_buffer_size)
  199. vbv_buffer_size = s->avctx->rc_buffer_size;
  200. else
  201. /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
  202. vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
  203. put_bits(&s->pb, 10, (vbv_buffer_size + 16383) / 16384);
  204. put_bits(&s->pb, 1, 1); /* constrained parameter flag */
  205. put_bits(&s->pb, 1, 0); /* no custom intra matrix */
  206. put_bits(&s->pb, 1, 0); /* no custom non intra matrix */
  207. put_header(s, GOP_START_CODE);
  208. put_bits(&s->pb, 1, 0); /* do drop frame */
  209. /* time code : we must convert from the real frame rate to a
  210. fake mpeg frame rate in case of low frame rate */
  211. fps = frame_rate_tab[s->frame_rate_index];
  212. time_code = (INT64)s->fake_picture_number * FRAME_RATE_BASE;
  213. s->gop_picture_number = s->fake_picture_number;
  214. put_bits(&s->pb, 5, (UINT32)((time_code / (fps * 3600)) % 24));
  215. put_bits(&s->pb, 6, (UINT32)((time_code / (fps * 60)) % 60));
  216. put_bits(&s->pb, 1, 1);
  217. put_bits(&s->pb, 6, (UINT32)((time_code / fps) % 60));
  218. put_bits(&s->pb, 6, (UINT32)((time_code % fps) / FRAME_RATE_BASE));
  219. put_bits(&s->pb, 1, 1); /* closed gop */
  220. put_bits(&s->pb, 1, 0); /* broken link */
  221. }
  222. if (s->frame_rate < (24 * FRAME_RATE_BASE) && s->picture_number > 0) {
  223. /* insert empty P pictures to slow down to the desired
  224. frame rate. Each fake pictures takes about 20 bytes */
  225. fps = frame_rate_tab[s->frame_rate_index];
  226. n = (((INT64)s->picture_number * fps) / s->frame_rate) - 1;
  227. while (s->fake_picture_number < n) {
  228. mpeg1_skip_picture(s, s->fake_picture_number -
  229. s->gop_picture_number);
  230. s->fake_picture_number++;
  231. }
  232. }
  233. }
  234. /* insert a fake P picture */
  235. static void mpeg1_skip_picture(MpegEncContext *s, int pict_num)
  236. {
  237. unsigned int mb_incr;
  238. /* mpeg1 picture header */
  239. put_header(s, PICTURE_START_CODE);
  240. /* temporal reference */
  241. put_bits(&s->pb, 10, pict_num & 0x3ff);
  242. put_bits(&s->pb, 3, P_TYPE);
  243. put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */
  244. put_bits(&s->pb, 1, 1); /* integer coordinates */
  245. put_bits(&s->pb, 3, 1); /* forward_f_code */
  246. put_bits(&s->pb, 1, 0); /* extra bit picture */
  247. /* only one slice */
  248. put_header(s, SLICE_MIN_START_CODE);
  249. put_bits(&s->pb, 5, 1); /* quantizer scale */
  250. put_bits(&s->pb, 1, 0); /* slice extra information */
  251. mb_incr = 1;
  252. put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1],
  253. mbAddrIncrTable[mb_incr - 1][0]);
  254. /* empty macroblock */
  255. put_bits(&s->pb, 3, 1); /* motion only */
  256. /* zero motion x & y */
  257. put_bits(&s->pb, 1, 1);
  258. put_bits(&s->pb, 1, 1);
  259. /* output a number of empty slice */
  260. mb_incr = s->mb_width * s->mb_height - 1;
  261. while (mb_incr > 33) {
  262. put_bits(&s->pb, 11, 0x008);
  263. mb_incr -= 33;
  264. }
  265. put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1],
  266. mbAddrIncrTable[mb_incr - 1][0]);
  267. /* empty macroblock */
  268. put_bits(&s->pb, 3, 1); /* motion only */
  269. /* zero motion x & y */
  270. put_bits(&s->pb, 1, 1);
  271. put_bits(&s->pb, 1, 1);
  272. }
  273. static void common_init(MpegEncContext *s)
  274. {
  275. s->y_dc_scale_table=
  276. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  277. }
  278. void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
  279. {
  280. mpeg1_encode_sequence_header(s);
  281. /* mpeg1 picture header */
  282. put_header(s, PICTURE_START_CODE);
  283. /* temporal reference */
  284. put_bits(&s->pb, 10, (s->fake_picture_number -
  285. s->gop_picture_number) & 0x3ff);
  286. s->fake_picture_number++;
  287. put_bits(&s->pb, 3, s->pict_type);
  288. put_bits(&s->pb, 16, 0xffff); /* non constant bit rate */
  289. if (s->pict_type == P_TYPE) {
  290. put_bits(&s->pb, 1, 0); /* half pel coordinates */
  291. put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
  292. }
  293. put_bits(&s->pb, 1, 0); /* extra bit picture */
  294. /* only one slice */
  295. put_header(s, SLICE_MIN_START_CODE);
  296. put_bits(&s->pb, 5, s->qscale); /* quantizer scale */
  297. put_bits(&s->pb, 1, 0); /* slice extra information */
  298. }
  299. void mpeg1_encode_mb(MpegEncContext *s,
  300. DCTELEM block[6][64],
  301. int motion_x, int motion_y)
  302. {
  303. int mb_incr, i, cbp, mb_x, mb_y;
  304. mb_x = s->mb_x;
  305. mb_y = s->mb_y;
  306. /* compute cbp */
  307. cbp = 0;
  308. for(i=0;i<6;i++) {
  309. if (s->block_last_index[i] >= 0)
  310. cbp |= 1 << (5 - i);
  311. }
  312. /* skip macroblock, except if first or last macroblock of a slice */
  313. if ((cbp | motion_x | motion_y) == 0 &&
  314. (!((mb_x | mb_y) == 0 ||
  315. (mb_x == s->mb_width - 1 && mb_y == s->mb_height - 1)))) {
  316. s->mb_incr++;
  317. s->qscale -= s->dquant;
  318. s->skip_count++;
  319. s->misc_bits++;
  320. s->last_bits++;
  321. } else {
  322. /* output mb incr */
  323. mb_incr = s->mb_incr;
  324. while (mb_incr > 33) {
  325. put_bits(&s->pb, 11, 0x008);
  326. mb_incr -= 33;
  327. }
  328. put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1],
  329. mbAddrIncrTable[mb_incr - 1][0]);
  330. if (s->pict_type == I_TYPE) {
  331. if(s->dquant && cbp){
  332. put_bits(&s->pb, 2, 1); /* macroblock_type : macroblock_quant = 1 */
  333. put_bits(&s->pb, 5, s->qscale);
  334. }else{
  335. put_bits(&s->pb, 1, 1); /* macroblock_type : macroblock_quant = 0 */
  336. s->qscale -= s->dquant;
  337. }
  338. s->misc_bits+= get_bits_diff(s);
  339. s->i_count++;
  340. } else {
  341. if (s->mb_intra) {
  342. if(s->dquant && cbp){
  343. put_bits(&s->pb, 6, 0x01);
  344. put_bits(&s->pb, 5, s->qscale);
  345. }else{
  346. put_bits(&s->pb, 5, 0x03);
  347. s->qscale -= s->dquant;
  348. }
  349. s->misc_bits+= get_bits_diff(s);
  350. s->i_count++;
  351. } else {
  352. if (cbp != 0) {
  353. if (motion_x == 0 && motion_y == 0) {
  354. if(s->dquant){
  355. put_bits(&s->pb, 5, 1); /* macroblock_pattern & quant */
  356. put_bits(&s->pb, 5, s->qscale);
  357. }else{
  358. put_bits(&s->pb, 2, 1); /* macroblock_pattern only */
  359. }
  360. s->misc_bits+= get_bits_diff(s);
  361. put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
  362. } else {
  363. if(s->dquant){
  364. put_bits(&s->pb, 5, 2); /* motion + cbp */
  365. put_bits(&s->pb, 5, s->qscale);
  366. }else{
  367. put_bits(&s->pb, 1, 1); /* motion + cbp */
  368. }
  369. s->misc_bits+= get_bits_diff(s);
  370. mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]);
  371. mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]);
  372. s->mv_bits+= get_bits_diff(s);
  373. put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
  374. }
  375. } else {
  376. put_bits(&s->pb, 3, 1); /* motion only */
  377. mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]);
  378. mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]);
  379. s->qscale -= s->dquant;
  380. s->mv_bits+= get_bits_diff(s);
  381. }
  382. s->f_count++;
  383. }
  384. }
  385. for(i=0;i<6;i++) {
  386. if (cbp & (1 << (5 - i))) {
  387. mpeg1_encode_block(s, block[i], i);
  388. }
  389. }
  390. s->mb_incr = 1;
  391. if(s->mb_intra)
  392. s->i_tex_bits+= get_bits_diff(s);
  393. else
  394. s->p_tex_bits+= get_bits_diff(s);
  395. }
  396. s->last_mv[0][0][0] = motion_x;
  397. s->last_mv[0][0][1] = motion_y;
  398. }
  399. static void mpeg1_encode_motion(MpegEncContext *s, int val)
  400. {
  401. int code, bit_size, l, m, bits, range, sign;
  402. if (val == 0) {
  403. /* zero vector */
  404. code = 0;
  405. put_bits(&s->pb,
  406. mbMotionVectorTable[0][1],
  407. mbMotionVectorTable[0][0]);
  408. } else {
  409. bit_size = s->f_code - 1;
  410. range = 1 << bit_size;
  411. /* modulo encoding */
  412. l = 16 * range;
  413. m = 2 * l;
  414. if (val < -l) {
  415. val += m;
  416. } else if (val >= l) {
  417. val -= m;
  418. }
  419. if (val >= 0) {
  420. val--;
  421. code = (val >> bit_size) + 1;
  422. bits = val & (range - 1);
  423. sign = 0;
  424. } else {
  425. val = -val;
  426. val--;
  427. code = (val >> bit_size) + 1;
  428. bits = val & (range - 1);
  429. sign = 1;
  430. }
  431. put_bits(&s->pb,
  432. mbMotionVectorTable[code][1],
  433. mbMotionVectorTable[code][0]);
  434. put_bits(&s->pb, 1, sign);
  435. if (bit_size > 0) {
  436. put_bits(&s->pb, bit_size, bits);
  437. }
  438. }
  439. }
  440. void ff_mpeg1_encode_init(MpegEncContext *s)
  441. {
  442. static int done=0;
  443. common_init(s);
  444. if(!done){
  445. int f_code;
  446. int mv;
  447. int i;
  448. done=1;
  449. init_rl(&rl_mpeg1);
  450. for(i=0; i<64; i++)
  451. {
  452. mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
  453. mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
  454. }
  455. init_uni_ac_vlc(&rl_mpeg1, uni_mpeg1_ac_vlc_bits, uni_mpeg1_ac_vlc_len);
  456. /* build unified dc encoding tables */
  457. for(i=-255; i<256; i++)
  458. {
  459. int adiff, index;
  460. int bits, code;
  461. int diff=i;
  462. adiff = ABS(diff);
  463. if(diff<0) diff--;
  464. index = vlc_dc_table[adiff];
  465. bits= vlc_dc_lum_bits[index] + index;
  466. code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
  467. mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
  468. bits= vlc_dc_chroma_bits[index] + index;
  469. code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
  470. mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
  471. }
  472. for(f_code=1; f_code<=MAX_FCODE; f_code++){
  473. for(mv=-MAX_MV; mv<=MAX_MV; mv++){
  474. int len;
  475. if(mv==0) len= mbMotionVectorTable[0][1];
  476. else{
  477. int val, bit_size, range, code;
  478. bit_size = s->f_code - 1;
  479. range = 1 << bit_size;
  480. val=mv;
  481. if (val < 0)
  482. val = -val;
  483. val--;
  484. code = (val >> bit_size) + 1;
  485. if(code<17){
  486. len= mbMotionVectorTable[code][1] + 1 + bit_size;
  487. }else{
  488. len= mbMotionVectorTable[16][1] + 2 + bit_size;
  489. }
  490. }
  491. mv_penalty[f_code][mv+MAX_MV]= len;
  492. }
  493. }
  494. for(f_code=MAX_FCODE; f_code>0; f_code--){
  495. for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
  496. fcode_tab[mv+MAX_MV]= f_code;
  497. }
  498. }
  499. }
  500. s->me.mv_penalty= mv_penalty;
  501. s->fcode_tab= fcode_tab;
  502. s->min_qcoeff=-255;
  503. s->max_qcoeff= 255;
  504. s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
  505. s->inter_quant_bias= 0;
  506. s->intra_ac_vlc_length=
  507. s->inter_ac_vlc_length= uni_mpeg1_ac_vlc_len;
  508. }
  509. static inline void encode_dc(MpegEncContext *s, int diff, int component)
  510. {
  511. if (component == 0) {
  512. put_bits(
  513. &s->pb,
  514. mpeg1_lum_dc_uni[diff+255]&0xFF,
  515. mpeg1_lum_dc_uni[diff+255]>>8);
  516. } else {
  517. put_bits(
  518. &s->pb,
  519. mpeg1_chr_dc_uni[diff+255]&0xFF,
  520. mpeg1_chr_dc_uni[diff+255]>>8);
  521. }
  522. }
  523. static void mpeg1_encode_block(MpegEncContext *s,
  524. DCTELEM *block,
  525. int n)
  526. {
  527. int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
  528. int code, component;
  529. // RLTable *rl = &rl_mpeg1;
  530. last_index = s->block_last_index[n];
  531. /* DC coef */
  532. if (s->mb_intra) {
  533. component = (n <= 3 ? 0 : n - 4 + 1);
  534. dc = block[0]; /* overflow is impossible */
  535. diff = dc - s->last_dc[component];
  536. encode_dc(s, diff, component);
  537. s->last_dc[component] = dc;
  538. i = 1;
  539. } else {
  540. /* encode the first coefficient : needs to be done here because
  541. it is handled slightly differently */
  542. level = block[0];
  543. if (abs(level) == 1) {
  544. code = ((UINT32)level >> 31); /* the sign bit */
  545. put_bits(&s->pb, 2, code | 0x02);
  546. i = 1;
  547. } else {
  548. i = 0;
  549. last_non_zero = -1;
  550. goto next_coef;
  551. }
  552. }
  553. /* now quantify & encode AC coefs */
  554. last_non_zero = i - 1;
  555. for(;i<=last_index;i++) {
  556. j = s->intra_scantable.permutated[i];
  557. level = block[j];
  558. next_coef:
  559. #if 0
  560. if (level != 0)
  561. dprintf("level[%d]=%d\n", i, level);
  562. #endif
  563. /* encode using VLC */
  564. if (level != 0) {
  565. run = i - last_non_zero - 1;
  566. alevel= level;
  567. MASK_ABS(sign, alevel)
  568. sign&=1;
  569. // code = get_rl_index(rl, 0, run, alevel);
  570. if (alevel <= mpeg1_max_level[0][run]){
  571. code= mpeg1_index_run[0][run] + alevel - 1;
  572. /* store the vlc & sign at once */
  573. put_bits(&s->pb, mpeg1_vlc[code][1]+1, (mpeg1_vlc[code][0]<<1) + sign);
  574. } else {
  575. /* escape seems to be pretty rare <5% so i dont optimize it */
  576. put_bits(&s->pb, mpeg1_vlc[111/*rl->n*/][1], mpeg1_vlc[111/*rl->n*/][0]);
  577. /* escape: only clip in this case */
  578. put_bits(&s->pb, 6, run);
  579. if (alevel < 128) {
  580. put_bits(&s->pb, 8, level & 0xff);
  581. } else {
  582. if (level < 0) {
  583. put_bits(&s->pb, 16, 0x8001 + level + 255);
  584. } else {
  585. put_bits(&s->pb, 16, level & 0xffff);
  586. }
  587. }
  588. }
  589. last_non_zero = i;
  590. }
  591. }
  592. /* end of block */
  593. put_bits(&s->pb, 2, 0x2);
  594. }
  595. /******************************************/
  596. /* decoding */
  597. static VLC dc_lum_vlc;
  598. static VLC dc_chroma_vlc;
  599. static VLC mv_vlc;
  600. static VLC mbincr_vlc;
  601. static VLC mb_ptype_vlc;
  602. static VLC mb_btype_vlc;
  603. static VLC mb_pat_vlc;
  604. static void init_vlcs(MpegEncContext *s)
  605. {
  606. static int done = 0;
  607. if (!done) {
  608. done = 1;
  609. init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12,
  610. vlc_dc_lum_bits, 1, 1,
  611. vlc_dc_lum_code, 2, 2);
  612. init_vlc(&dc_chroma_vlc, DC_VLC_BITS, 12,
  613. vlc_dc_chroma_bits, 1, 1,
  614. vlc_dc_chroma_code, 2, 2);
  615. init_vlc(&mv_vlc, MV_VLC_BITS, 17,
  616. &mbMotionVectorTable[0][1], 2, 1,
  617. &mbMotionVectorTable[0][0], 2, 1);
  618. init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 35,
  619. &mbAddrIncrTable[0][1], 2, 1,
  620. &mbAddrIncrTable[0][0], 2, 1);
  621. init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 63,
  622. &mbPatTable[0][1], 2, 1,
  623. &mbPatTable[0][0], 2, 1);
  624. init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 32,
  625. &table_mb_ptype[0][1], 2, 1,
  626. &table_mb_ptype[0][0], 2, 1);
  627. init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 32,
  628. &table_mb_btype[0][1], 2, 1,
  629. &table_mb_btype[0][0], 2, 1);
  630. init_rl(&rl_mpeg1);
  631. init_rl(&rl_mpeg2);
  632. init_2d_vlc_rl(&rl_mpeg1);
  633. init_2d_vlc_rl(&rl_mpeg2);
  634. }
  635. }
  636. static inline int get_dmv(MpegEncContext *s)
  637. {
  638. if(get_bits1(&s->gb))
  639. return 1 - (get_bits1(&s->gb) << 1);
  640. else
  641. return 0;
  642. }
  643. static inline int get_qscale(MpegEncContext *s)
  644. {
  645. int qscale;
  646. if (s->mpeg2) {
  647. if (s->q_scale_type) {
  648. qscale = non_linear_qscale[get_bits(&s->gb, 5)];
  649. } else {
  650. qscale = get_bits(&s->gb, 5) << 1;
  651. }
  652. } else {
  653. /* for mpeg1, we use the generic unquant code */
  654. qscale = get_bits(&s->gb, 5);
  655. }
  656. return qscale;
  657. }
  658. /* motion type (for mpeg2) */
  659. #define MT_FIELD 1
  660. #define MT_FRAME 2
  661. #define MT_16X8 2
  662. #define MT_DMV 3
  663. static int mpeg_decode_mb(MpegEncContext *s,
  664. DCTELEM block[6][64])
  665. {
  666. int i, j, k, cbp, val, mb_type, motion_type;
  667. dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
  668. if (--s->mb_incr != 0) {
  669. /* skip mb */
  670. s->mb_intra = 0;
  671. for(i=0;i<6;i++)
  672. s->block_last_index[i] = -1;
  673. s->mv_type = MV_TYPE_16X16;
  674. if (s->pict_type == P_TYPE) {
  675. /* if P type, zero motion vector is implied */
  676. s->mv_dir = MV_DIR_FORWARD;
  677. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  678. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  679. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  680. } else {
  681. /* if B type, reuse previous vectors and directions */
  682. s->mv[0][0][0] = s->last_mv[0][0][0];
  683. s->mv[0][0][1] = s->last_mv[0][0][1];
  684. s->mv[1][0][0] = s->last_mv[1][0][0];
  685. s->mv[1][0][1] = s->last_mv[1][0][1];
  686. }
  687. s->mb_skiped = 1;
  688. return 0;
  689. }
  690. switch(s->pict_type) {
  691. default:
  692. case I_TYPE:
  693. if (get_bits1(&s->gb) == 0) {
  694. if (get_bits1(&s->gb) == 0)
  695. return -1;
  696. mb_type = MB_QUANT | MB_INTRA;
  697. } else {
  698. mb_type = MB_INTRA;
  699. }
  700. break;
  701. case P_TYPE:
  702. mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
  703. if (mb_type < 0){
  704. fprintf(stderr, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
  705. return -1;
  706. }
  707. break;
  708. case B_TYPE:
  709. mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
  710. if (mb_type < 0){
  711. fprintf(stderr, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
  712. return -1;
  713. }
  714. break;
  715. }
  716. dprintf("mb_type=%x\n", mb_type);
  717. motion_type = 0; /* avoid warning */
  718. if (mb_type & (MB_FOR|MB_BACK)) {
  719. /* get additionnal motion vector type */
  720. if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct)
  721. motion_type = MT_FRAME;
  722. else
  723. motion_type = get_bits(&s->gb, 2);
  724. }
  725. /* compute dct type */
  726. if (s->picture_structure == PICT_FRAME &&
  727. !s->frame_pred_frame_dct &&
  728. (mb_type & (MB_PAT | MB_INTRA))) {
  729. s->interlaced_dct = get_bits1(&s->gb);
  730. #ifdef DEBUG
  731. if (s->interlaced_dct)
  732. printf("interlaced_dct\n");
  733. #endif
  734. } else {
  735. s->interlaced_dct = 0; /* frame based */
  736. }
  737. if (mb_type & MB_QUANT) {
  738. s->qscale = get_qscale(s);
  739. }
  740. if (mb_type & MB_INTRA) {
  741. if (s->concealment_motion_vectors) {
  742. /* just parse them */
  743. if (s->picture_structure != PICT_FRAME)
  744. skip_bits1(&s->gb); /* field select */
  745. mpeg_decode_motion(s, s->mpeg_f_code[0][0], 0);
  746. mpeg_decode_motion(s, s->mpeg_f_code[0][1], 0);
  747. }
  748. s->mb_intra = 1;
  749. cbp = 0x3f;
  750. memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
  751. } else {
  752. s->mb_intra = 0;
  753. cbp = 0;
  754. }
  755. /* special case of implicit zero motion vector */
  756. if (s->pict_type == P_TYPE && !(mb_type & MB_FOR)) {
  757. s->mv_dir = MV_DIR_FORWARD;
  758. s->mv_type = MV_TYPE_16X16;
  759. s->last_mv[0][0][0] = 0;
  760. s->last_mv[0][0][1] = 0;
  761. s->last_mv[0][1][0] = 0;
  762. s->last_mv[0][1][1] = 0;
  763. s->mv[0][0][0] = 0;
  764. s->mv[0][0][1] = 0;
  765. } else if (mb_type & (MB_FOR | MB_BACK)) {
  766. /* motion vectors */
  767. s->mv_dir = 0;
  768. for(i=0;i<2;i++) {
  769. if (mb_type & (MB_FOR >> i)) {
  770. s->mv_dir |= (MV_DIR_FORWARD >> i);
  771. dprintf("motion_type=%d\n", motion_type);
  772. switch(motion_type) {
  773. case MT_FRAME: /* or MT_16X8 */
  774. if (s->picture_structure == PICT_FRAME) {
  775. /* MT_FRAME */
  776. s->mv_type = MV_TYPE_16X16;
  777. for(k=0;k<2;k++) {
  778. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  779. s->last_mv[i][0][k]);
  780. s->last_mv[i][0][k] = val;
  781. s->last_mv[i][1][k] = val;
  782. /* full_pel: only for mpeg1 */
  783. if (s->full_pel[i])
  784. val = val << 1;
  785. s->mv[i][0][k] = val;
  786. dprintf("mv%d: %d\n", k, val);
  787. }
  788. } else {
  789. /* MT_16X8 */
  790. s->mv_type = MV_TYPE_16X8;
  791. for(j=0;j<2;j++) {
  792. s->field_select[i][j] = get_bits1(&s->gb);
  793. for(k=0;k<2;k++) {
  794. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  795. s->last_mv[i][j][k]);
  796. s->last_mv[i][j][k] = val;
  797. s->mv[i][j][k] = val;
  798. }
  799. }
  800. }
  801. break;
  802. case MT_FIELD:
  803. if (s->picture_structure == PICT_FRAME) {
  804. s->mv_type = MV_TYPE_FIELD;
  805. for(j=0;j<2;j++) {
  806. s->field_select[i][j] = get_bits1(&s->gb);
  807. val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  808. s->last_mv[i][j][0]);
  809. s->last_mv[i][j][0] = val;
  810. s->mv[i][j][0] = val;
  811. dprintf("fmx=%d\n", val);
  812. val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  813. s->last_mv[i][j][1] >> 1);
  814. s->last_mv[i][j][1] = val << 1;
  815. s->mv[i][j][1] = val;
  816. dprintf("fmy=%d\n", val);
  817. }
  818. } else {
  819. s->mv_type = MV_TYPE_16X16;
  820. s->field_select[i][0] = get_bits1(&s->gb);
  821. for(k=0;k<2;k++) {
  822. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  823. s->last_mv[i][0][k]);
  824. s->last_mv[i][0][k] = val;
  825. s->last_mv[i][1][k] = val;
  826. s->mv[i][0][k] = val;
  827. }
  828. }
  829. break;
  830. case MT_DMV:
  831. {
  832. int dmx, dmy, mx, my, m;
  833. mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  834. s->last_mv[i][0][0]);
  835. s->last_mv[i][0][0] = mx;
  836. s->last_mv[i][1][0] = mx;
  837. dmx = get_dmv(s);
  838. my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  839. s->last_mv[i][0][1] >> 1);
  840. dmy = get_dmv(s);
  841. s->mv_type = MV_TYPE_DMV;
  842. /* XXX: totally broken */
  843. if (s->picture_structure == PICT_FRAME) {
  844. s->last_mv[i][0][1] = my << 1;
  845. s->last_mv[i][1][1] = my << 1;
  846. m = s->top_field_first ? 1 : 3;
  847. /* top -> top pred */
  848. s->mv[i][0][0] = mx;
  849. s->mv[i][0][1] = my << 1;
  850. s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  851. s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
  852. m = 4 - m;
  853. s->mv[i][2][0] = mx;
  854. s->mv[i][2][1] = my << 1;
  855. s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  856. s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
  857. } else {
  858. s->last_mv[i][0][1] = my;
  859. s->last_mv[i][1][1] = my;
  860. s->mv[i][0][0] = mx;
  861. s->mv[i][0][1] = my;
  862. s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx;
  863. s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1
  864. /* + 2 * cur_field */;
  865. }
  866. }
  867. break;
  868. }
  869. }
  870. }
  871. }
  872. if ((mb_type & MB_INTRA) && s->concealment_motion_vectors) {
  873. skip_bits1(&s->gb); /* marker */
  874. }
  875. if (mb_type & MB_PAT) {
  876. cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
  877. if (cbp < 0){
  878. fprintf(stderr, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
  879. return -1;
  880. }
  881. cbp++;
  882. }
  883. dprintf("cbp=%x\n", cbp);
  884. if (s->mpeg2) {
  885. if (s->mb_intra) {
  886. for(i=0;i<6;i++) {
  887. if (mpeg2_decode_block_intra(s, block[i], i) < 0)
  888. return -1;
  889. }
  890. } else {
  891. for(i=0;i<6;i++) {
  892. if (cbp & 32) {
  893. if (mpeg2_decode_block_non_intra(s, block[i], i) < 0)
  894. return -1;
  895. } else {
  896. s->block_last_index[i] = -1;
  897. }
  898. cbp+=cbp;
  899. }
  900. }
  901. } else {
  902. if (s->mb_intra) {
  903. for(i=0;i<6;i++) {
  904. if (mpeg1_decode_block_intra(s, block[i], i) < 0)
  905. return -1;
  906. }
  907. }else{
  908. for(i=0;i<6;i++) {
  909. if (cbp & 32) {
  910. if (mpeg1_decode_block_inter(s, block[i], i) < 0)
  911. return -1;
  912. } else {
  913. s->block_last_index[i] = -1;
  914. }
  915. cbp+=cbp;
  916. }
  917. }
  918. }
  919. return 0;
  920. }
  921. /* as h263, but only 17 codes */
  922. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
  923. {
  924. int code, sign, val, m, l, shift;
  925. code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
  926. if (code < 0) {
  927. return 0xffff;
  928. }
  929. if (code == 0) {
  930. return pred;
  931. }
  932. sign = get_bits1(&s->gb);
  933. shift = fcode - 1;
  934. val = (code - 1) << shift;
  935. if (shift > 0)
  936. val |= get_bits(&s->gb, shift);
  937. val++;
  938. if (sign)
  939. val = -val;
  940. val += pred;
  941. /* modulo decoding */
  942. l = (1 << shift) * 16;
  943. m = 2 * l;
  944. if (val < -l) {
  945. val += m;
  946. } else if (val >= l) {
  947. val -= m;
  948. }
  949. return val;
  950. }
  951. static inline int decode_dc(MpegEncContext *s, int component)
  952. {
  953. int code, diff;
  954. if (component == 0) {
  955. code = get_vlc2(&s->gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
  956. } else {
  957. code = get_vlc2(&s->gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
  958. }
  959. if (code < 0){
  960. fprintf(stderr, "invalid dc code at %d %d\n", s->mb_x, s->mb_y);
  961. return 0xffff;
  962. }
  963. if (code == 0) {
  964. diff = 0;
  965. } else {
  966. diff = get_bits(&s->gb, code);
  967. if ((diff & (1 << (code - 1))) == 0)
  968. diff = (-1 << code) | (diff + 1);
  969. }
  970. return diff;
  971. }
  972. static inline int mpeg1_decode_block_intra(MpegEncContext *s,
  973. DCTELEM *block,
  974. int n)
  975. {
  976. int level, dc, diff, i, j, run;
  977. int component;
  978. RLTable *rl = &rl_mpeg1;
  979. UINT8 * const scantable= s->intra_scantable.permutated;
  980. const UINT16 *quant_matrix= s->intra_matrix;
  981. const int qscale= s->qscale;
  982. /* DC coef */
  983. component = (n <= 3 ? 0 : n - 4 + 1);
  984. diff = decode_dc(s, component);
  985. if (diff >= 0xffff)
  986. return -1;
  987. dc = s->last_dc[component];
  988. dc += diff;
  989. s->last_dc[component] = dc;
  990. block[0] = dc<<3;
  991. dprintf("dc=%d diff=%d\n", dc, diff);
  992. i = 0;
  993. {
  994. OPEN_READER(re, &s->gb);
  995. /* now quantify & encode AC coefs */
  996. for(;;) {
  997. UPDATE_CACHE(re, &s->gb);
  998. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  999. if(level == 127){
  1000. break;
  1001. } else if(level != 0) {
  1002. i += run;
  1003. j = scantable[i];
  1004. level= (level*qscale*quant_matrix[j])>>3;
  1005. level= (level-1)|1;
  1006. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1007. LAST_SKIP_BITS(re, &s->gb, 1);
  1008. } else {
  1009. /* escape */
  1010. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1011. UPDATE_CACHE(re, &s->gb);
  1012. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  1013. if (level == -128) {
  1014. level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
  1015. } else if (level == 0) {
  1016. level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
  1017. }
  1018. i += run;
  1019. j = scantable[i];
  1020. if(level<0){
  1021. level= -level;
  1022. level= (level*qscale*quant_matrix[j])>>3;
  1023. level= (level-1)|1;
  1024. level= -level;
  1025. }else{
  1026. level= (level*qscale*quant_matrix[j])>>3;
  1027. level= (level-1)|1;
  1028. }
  1029. }
  1030. if (i > 63){
  1031. fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1032. return -1;
  1033. }
  1034. block[j] = level;
  1035. }
  1036. CLOSE_READER(re, &s->gb);
  1037. }
  1038. s->block_last_index[n] = i;
  1039. return 0;
  1040. }
  1041. static inline int mpeg1_decode_block_inter(MpegEncContext *s,
  1042. DCTELEM *block,
  1043. int n)
  1044. {
  1045. int level, i, j, run;
  1046. RLTable *rl = &rl_mpeg1;
  1047. UINT8 * const scantable= s->intra_scantable.permutated;
  1048. const UINT16 *quant_matrix= s->inter_matrix;
  1049. const int qscale= s->qscale;
  1050. {
  1051. int v;
  1052. OPEN_READER(re, &s->gb);
  1053. i = -1;
  1054. /* special case for the first coef. no need to add a second vlc table */
  1055. UPDATE_CACHE(re, &s->gb);
  1056. v= SHOW_UBITS(re, &s->gb, 2);
  1057. if (v & 2) {
  1058. LAST_SKIP_BITS(re, &s->gb, 2);
  1059. level= (3*qscale*quant_matrix[0])>>4;
  1060. level= (level-1)|1;
  1061. if(v&1)
  1062. level= -level;
  1063. block[0] = level;
  1064. i++;
  1065. }
  1066. /* now quantify & encode AC coefs */
  1067. for(;;) {
  1068. UPDATE_CACHE(re, &s->gb);
  1069. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  1070. if(level == 127){
  1071. break;
  1072. } else if(level != 0) {
  1073. i += run;
  1074. j = scantable[i];
  1075. level= ((level*2+1)*qscale*quant_matrix[j])>>4;
  1076. level= (level-1)|1;
  1077. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1078. LAST_SKIP_BITS(re, &s->gb, 1);
  1079. } else {
  1080. /* escape */
  1081. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1082. UPDATE_CACHE(re, &s->gb);
  1083. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  1084. if (level == -128) {
  1085. level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
  1086. } else if (level == 0) {
  1087. level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
  1088. }
  1089. i += run;
  1090. j = scantable[i];
  1091. if(level<0){
  1092. level= -level;
  1093. level= ((level*2+1)*qscale*quant_matrix[j])>>4;
  1094. level= (level-1)|1;
  1095. level= -level;
  1096. }else{
  1097. level= ((level*2+1)*qscale*quant_matrix[j])>>4;
  1098. level= (level-1)|1;
  1099. }
  1100. }
  1101. if (i > 63){
  1102. fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1103. return -1;
  1104. }
  1105. block[j] = level;
  1106. }
  1107. CLOSE_READER(re, &s->gb);
  1108. }
  1109. s->block_last_index[n] = i;
  1110. return 0;
  1111. }
  1112. /* Also does unquantization here, since I will never support mpeg2
  1113. encoding */
  1114. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
  1115. DCTELEM *block,
  1116. int n)
  1117. {
  1118. int level, i, j, run;
  1119. RLTable *rl = &rl_mpeg1;
  1120. UINT8 * const scantable= s->intra_scantable.permutated;
  1121. const UINT16 *quant_matrix;
  1122. const int qscale= s->qscale;
  1123. int mismatch;
  1124. mismatch = 1;
  1125. {
  1126. int v;
  1127. OPEN_READER(re, &s->gb);
  1128. i = -1;
  1129. if (n < 4)
  1130. quant_matrix = s->inter_matrix;
  1131. else
  1132. quant_matrix = s->chroma_inter_matrix;
  1133. /* special case for the first coef. no need to add a second vlc table */
  1134. UPDATE_CACHE(re, &s->gb);
  1135. v= SHOW_UBITS(re, &s->gb, 2);
  1136. if (v & 2) {
  1137. LAST_SKIP_BITS(re, &s->gb, 2);
  1138. level= (3*qscale*quant_matrix[0])>>5;
  1139. if(v&1)
  1140. level= -level;
  1141. block[0] = level;
  1142. mismatch ^= level;
  1143. i++;
  1144. }
  1145. /* now quantify & encode AC coefs */
  1146. for(;;) {
  1147. UPDATE_CACHE(re, &s->gb);
  1148. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  1149. if(level == 127){
  1150. break;
  1151. } else if(level != 0) {
  1152. i += run;
  1153. j = scantable[i];
  1154. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1155. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1156. LAST_SKIP_BITS(re, &s->gb, 1);
  1157. } else {
  1158. /* escape */
  1159. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1160. UPDATE_CACHE(re, &s->gb);
  1161. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1162. i += run;
  1163. j = scantable[i];
  1164. if(level<0){
  1165. level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
  1166. level= -level;
  1167. }else{
  1168. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1169. }
  1170. }
  1171. if (i > 63){
  1172. fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1173. return -1;
  1174. }
  1175. mismatch ^= level;
  1176. block[j] = level;
  1177. }
  1178. CLOSE_READER(re, &s->gb);
  1179. }
  1180. block[63] ^= (mismatch & 1);
  1181. s->block_last_index[n] = i;
  1182. return 0;
  1183. }
  1184. static inline int mpeg2_decode_block_intra(MpegEncContext *s,
  1185. DCTELEM *block,
  1186. int n)
  1187. {
  1188. int level, dc, diff, i, j, run;
  1189. int component;
  1190. RLTable *rl;
  1191. UINT8 * const scantable= s->intra_scantable.permutated;
  1192. const UINT16 *quant_matrix;
  1193. const int qscale= s->qscale;
  1194. int mismatch;
  1195. /* DC coef */
  1196. if (n < 4){
  1197. quant_matrix = s->intra_matrix;
  1198. component = 0;
  1199. }else{
  1200. quant_matrix = s->chroma_intra_matrix;
  1201. component = n - 3;
  1202. }
  1203. diff = decode_dc(s, component);
  1204. if (diff >= 0xffff)
  1205. return -1;
  1206. dc = s->last_dc[component];
  1207. dc += diff;
  1208. s->last_dc[component] = dc;
  1209. block[0] = dc << (3 - s->intra_dc_precision);
  1210. dprintf("dc=%d\n", block[0]);
  1211. mismatch = block[0] ^ 1;
  1212. i = 0;
  1213. if (s->intra_vlc_format)
  1214. rl = &rl_mpeg2;
  1215. else
  1216. rl = &rl_mpeg1;
  1217. {
  1218. OPEN_READER(re, &s->gb);
  1219. /* now quantify & encode AC coefs */
  1220. for(;;) {
  1221. UPDATE_CACHE(re, &s->gb);
  1222. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  1223. if(level == 127){
  1224. break;
  1225. } else if(level != 0) {
  1226. i += run;
  1227. j = scantable[i];
  1228. level= (level*qscale*quant_matrix[j])>>4;
  1229. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1230. LAST_SKIP_BITS(re, &s->gb, 1);
  1231. } else {
  1232. /* escape */
  1233. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1234. UPDATE_CACHE(re, &s->gb);
  1235. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1236. i += run;
  1237. j = scantable[i];
  1238. if(level<0){
  1239. level= (-level*qscale*quant_matrix[j])>>4;
  1240. level= -level;
  1241. }else{
  1242. level= (level*qscale*quant_matrix[j])>>4;
  1243. }
  1244. }
  1245. if (i > 63){
  1246. fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1247. return -1;
  1248. }
  1249. mismatch^= level;
  1250. block[j] = level;
  1251. }
  1252. CLOSE_READER(re, &s->gb);
  1253. }
  1254. block[63]^= mismatch&1;
  1255. s->block_last_index[n] = i;
  1256. return 0;
  1257. }
  1258. /* compressed picture size */
  1259. #define PICTURE_BUFFER_SIZE 100000
  1260. typedef struct Mpeg1Context {
  1261. MpegEncContext mpeg_enc_ctx;
  1262. UINT32 header_state;
  1263. int start_code; /* current start code */
  1264. UINT8 buffer[PICTURE_BUFFER_SIZE];
  1265. UINT8 *buf_ptr;
  1266. int buffer_size;
  1267. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  1268. int repeat_field; /* true if we must repeat the field */
  1269. } Mpeg1Context;
  1270. static int mpeg_decode_init(AVCodecContext *avctx)
  1271. {
  1272. Mpeg1Context *s = avctx->priv_data;
  1273. s->mpeg_enc_ctx.flags= avctx->flags;
  1274. common_init(&s->mpeg_enc_ctx);
  1275. init_vlcs(&s->mpeg_enc_ctx);
  1276. s->header_state = 0xff;
  1277. s->mpeg_enc_ctx_allocated = 0;
  1278. s->buffer_size = PICTURE_BUFFER_SIZE;
  1279. s->start_code = -1;
  1280. s->buf_ptr = s->buffer;
  1281. s->mpeg_enc_ctx.picture_number = 0;
  1282. s->repeat_field = 0;
  1283. s->mpeg_enc_ctx.codec_id= avctx->codec->id;
  1284. return 0;
  1285. }
  1286. /* return the 8 bit start code value and update the search
  1287. state. Return -1 if no start code found */
  1288. static int find_start_code(UINT8 **pbuf_ptr, UINT8 *buf_end,
  1289. UINT32 *header_state)
  1290. {
  1291. UINT8 *buf_ptr;
  1292. unsigned int state, v;
  1293. int val;
  1294. state = *header_state;
  1295. buf_ptr = *pbuf_ptr;
  1296. while (buf_ptr < buf_end) {
  1297. v = *buf_ptr++;
  1298. if (state == 0x000001) {
  1299. state = ((state << 8) | v) & 0xffffff;
  1300. val = state;
  1301. goto found;
  1302. }
  1303. state = ((state << 8) | v) & 0xffffff;
  1304. }
  1305. val = -1;
  1306. found:
  1307. *pbuf_ptr = buf_ptr;
  1308. *header_state = state;
  1309. return val;
  1310. }
  1311. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1312. UINT8 *buf, int buf_size)
  1313. {
  1314. Mpeg1Context *s1 = avctx->priv_data;
  1315. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1316. int ref, f_code;
  1317. init_get_bits(&s->gb, buf, buf_size);
  1318. ref = get_bits(&s->gb, 10); /* temporal ref */
  1319. s->pict_type = get_bits(&s->gb, 3);
  1320. dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number);
  1321. skip_bits(&s->gb, 16);
  1322. if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
  1323. s->full_pel[0] = get_bits1(&s->gb);
  1324. f_code = get_bits(&s->gb, 3);
  1325. if (f_code == 0)
  1326. return -1;
  1327. s->mpeg_f_code[0][0] = f_code;
  1328. s->mpeg_f_code[0][1] = f_code;
  1329. }
  1330. if (s->pict_type == B_TYPE) {
  1331. s->full_pel[1] = get_bits1(&s->gb);
  1332. f_code = get_bits(&s->gb, 3);
  1333. if (f_code == 0)
  1334. return -1;
  1335. s->mpeg_f_code[1][0] = f_code;
  1336. s->mpeg_f_code[1][1] = f_code;
  1337. }
  1338. s->current_picture.pict_type= s->pict_type;
  1339. s->current_picture.key_frame= s->pict_type == I_TYPE;
  1340. s->y_dc_scale = 8;
  1341. s->c_dc_scale = 8;
  1342. s->first_slice = 1;
  1343. return 0;
  1344. }
  1345. static void mpeg_decode_sequence_extension(MpegEncContext *s)
  1346. {
  1347. int horiz_size_ext, vert_size_ext;
  1348. int bit_rate_ext, vbv_buf_ext;
  1349. int frame_rate_ext_n, frame_rate_ext_d;
  1350. float aspect;
  1351. skip_bits(&s->gb, 8); /* profil and level */
  1352. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1353. skip_bits(&s->gb, 2); /* chroma_format */
  1354. horiz_size_ext = get_bits(&s->gb, 2);
  1355. vert_size_ext = get_bits(&s->gb, 2);
  1356. s->width |= (horiz_size_ext << 12);
  1357. s->height |= (vert_size_ext << 12);
  1358. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1359. s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400;
  1360. skip_bits1(&s->gb); /* marker */
  1361. vbv_buf_ext = get_bits(&s->gb, 8);
  1362. s->low_delay = get_bits1(&s->gb);
  1363. frame_rate_ext_n = get_bits(&s->gb, 2);
  1364. frame_rate_ext_d = get_bits(&s->gb, 5);
  1365. if (frame_rate_ext_d >= 1)
  1366. s->frame_rate = (s->frame_rate * frame_rate_ext_n) / frame_rate_ext_d;
  1367. dprintf("sequence extension\n");
  1368. s->mpeg2 = 1;
  1369. s->avctx->sub_id = 2; /* indicates mpeg2 found */
  1370. aspect= mpeg2_aspect[s->aspect_ratio_info];
  1371. if(aspect>0.0) s->avctx->aspect_ratio= s->width/(aspect*s->height);
  1372. else if(aspect<0.0) s->avctx->aspect_ratio= -1.0/aspect;
  1373. }
  1374. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1375. {
  1376. int i, v, j;
  1377. dprintf("matrix extension\n");
  1378. if (get_bits1(&s->gb)) {
  1379. for(i=0;i<64;i++) {
  1380. v = get_bits(&s->gb, 8);
  1381. j= s->idct_permutation[ ff_zigzag_direct[i] ];
  1382. s->intra_matrix[j] = v;
  1383. s->chroma_intra_matrix[j] = v;
  1384. }
  1385. }
  1386. if (get_bits1(&s->gb)) {
  1387. for(i=0;i<64;i++) {
  1388. v = get_bits(&s->gb, 8);
  1389. j= s->idct_permutation[ ff_zigzag_direct[i] ];
  1390. s->inter_matrix[j] = v;
  1391. s->chroma_inter_matrix[j] = v;
  1392. }
  1393. }
  1394. if (get_bits1(&s->gb)) {
  1395. for(i=0;i<64;i++) {
  1396. v = get_bits(&s->gb, 8);
  1397. j= s->idct_permutation[ ff_zigzag_direct[i] ];
  1398. s->chroma_intra_matrix[j] = v;
  1399. }
  1400. }
  1401. if (get_bits1(&s->gb)) {
  1402. for(i=0;i<64;i++) {
  1403. v = get_bits(&s->gb, 8);
  1404. j= s->idct_permutation[ ff_zigzag_direct[i] ];
  1405. s->chroma_inter_matrix[j] = v;
  1406. }
  1407. }
  1408. }
  1409. static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
  1410. {
  1411. s->full_pel[0] = s->full_pel[1] = 0;
  1412. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1413. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1414. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1415. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1416. s->intra_dc_precision = get_bits(&s->gb, 2);
  1417. s->picture_structure = get_bits(&s->gb, 2);
  1418. s->top_field_first = get_bits1(&s->gb);
  1419. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1420. s->concealment_motion_vectors = get_bits1(&s->gb);
  1421. s->q_scale_type = get_bits1(&s->gb);
  1422. s->intra_vlc_format = get_bits1(&s->gb);
  1423. s->alternate_scan = get_bits1(&s->gb);
  1424. s->repeat_first_field = get_bits1(&s->gb);
  1425. s->chroma_420_type = get_bits1(&s->gb);
  1426. s->progressive_frame = get_bits1(&s->gb);
  1427. if(s->alternate_scan){
  1428. ff_init_scantable(s, &s->inter_scantable , ff_alternate_vertical_scan);
  1429. ff_init_scantable(s, &s->intra_scantable , ff_alternate_vertical_scan);
  1430. ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_vertical_scan);
  1431. ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
  1432. }else{
  1433. ff_init_scantable(s, &s->inter_scantable , ff_zigzag_direct);
  1434. ff_init_scantable(s, &s->intra_scantable , ff_zigzag_direct);
  1435. ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  1436. ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
  1437. }
  1438. /* composite display not parsed */
  1439. dprintf("intra_dc_precision=%d\n", s->intra_dc_precision);
  1440. dprintf("picture_structure=%d\n", s->picture_structure);
  1441. dprintf("top field first=%d\n", s->top_field_first);
  1442. dprintf("repeat first field=%d\n", s->repeat_first_field);
  1443. dprintf("conceal=%d\n", s->concealment_motion_vectors);
  1444. dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
  1445. dprintf("alternate_scan=%d\n", s->alternate_scan);
  1446. dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1447. dprintf("progressive_frame=%d\n", s->progressive_frame);
  1448. }
  1449. static void mpeg_decode_extension(AVCodecContext *avctx,
  1450. UINT8 *buf, int buf_size)
  1451. {
  1452. Mpeg1Context *s1 = avctx->priv_data;
  1453. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1454. int ext_type;
  1455. init_get_bits(&s->gb, buf, buf_size);
  1456. ext_type = get_bits(&s->gb, 4);
  1457. switch(ext_type) {
  1458. case 0x1:
  1459. /* sequence ext */
  1460. mpeg_decode_sequence_extension(s);
  1461. break;
  1462. case 0x3:
  1463. /* quant matrix extension */
  1464. mpeg_decode_quant_matrix_extension(s);
  1465. break;
  1466. case 0x8:
  1467. /* picture extension */
  1468. mpeg_decode_picture_coding_extension(s);
  1469. break;
  1470. }
  1471. }
  1472. #define DECODE_SLICE_FATAL_ERROR -2
  1473. #define DECODE_SLICE_ERROR -1
  1474. #define DECODE_SLICE_OK 0
  1475. #define DECODE_SLICE_EOP 1
  1476. /**
  1477. * decodes a slice.
  1478. * @return DECODE_SLICE_FATAL_ERROR if a non recoverable error occured<br>
  1479. * DECODE_SLICE_ERROR if the slice is damaged<br>
  1480. * DECODE_SLICE_OK if this slice is ok<br>
  1481. * DECODE_SLICE_EOP if the end of the picture is reached
  1482. */
  1483. static int mpeg_decode_slice(AVCodecContext *avctx,
  1484. AVFrame *pict,
  1485. int start_code,
  1486. UINT8 *buf, int buf_size)
  1487. {
  1488. Mpeg1Context *s1 = avctx->priv_data;
  1489. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1490. int ret;
  1491. start_code = (start_code - 1) & 0xff;
  1492. if (start_code >= s->mb_height){
  1493. fprintf(stderr, "slice below image (%d >= %d)\n", start_code, s->mb_height);
  1494. return DECODE_SLICE_ERROR;
  1495. }
  1496. s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
  1497. s->last_dc[1] = s->last_dc[0];
  1498. s->last_dc[2] = s->last_dc[0];
  1499. memset(s->last_mv, 0, sizeof(s->last_mv));
  1500. /* start frame decoding */
  1501. if (s->first_slice) {
  1502. s->first_slice = 0;
  1503. if(MPV_frame_start(s, avctx) < 0)
  1504. return DECODE_SLICE_FATAL_ERROR;
  1505. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  1506. printf("qp:%d fc:%d%d%d%d %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
  1507. s->qscale, s->mpeg_f_code[0][0],s->mpeg_f_code[0][1],s->mpeg_f_code[1][0],s->mpeg_f_code[1][1],
  1508. s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")),
  1509. s->progressive_sequence ? "pro" :"", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
  1510. s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
  1511. s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
  1512. }
  1513. }
  1514. init_get_bits(&s->gb, buf, buf_size);
  1515. s->qscale = get_qscale(s);
  1516. /* extra slice info */
  1517. while (get_bits1(&s->gb) != 0) {
  1518. skip_bits(&s->gb, 8);
  1519. }
  1520. s->mb_x=0;
  1521. for(;;) {
  1522. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1523. if (code < 0)
  1524. return -1; /* error = end of slice, but empty slice is bad or?*/
  1525. if (code >= 33) {
  1526. if (code == 33) {
  1527. s->mb_x += 33;
  1528. }
  1529. /* otherwise, stuffing, nothing to do */
  1530. } else {
  1531. s->mb_x += code;
  1532. break;
  1533. }
  1534. }
  1535. s->mb_y = start_code;
  1536. s->mb_incr= 1;
  1537. for(;;) {
  1538. s->dsp.clear_blocks(s->block[0]);
  1539. ret = mpeg_decode_mb(s, s->block);
  1540. dprintf("ret=%d\n", ret);
  1541. if (ret < 0)
  1542. return -1;
  1543. MPV_decode_mb(s, s->block);
  1544. if (++s->mb_x >= s->mb_width) {
  1545. ff_draw_horiz_band(s);
  1546. s->mb_x = 0;
  1547. s->mb_y++;
  1548. PRINT_QP("%s", "\n");
  1549. }
  1550. PRINT_QP("%2d", s->qscale);
  1551. /* skip mb handling */
  1552. if (s->mb_incr == 0) {
  1553. /* read again increment */
  1554. s->mb_incr = 1;
  1555. for(;;) {
  1556. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1557. if (code < 0)
  1558. goto eos; /* error = end of slice */
  1559. if (code >= 33) {
  1560. if (code == 33) {
  1561. s->mb_incr += 33;
  1562. }
  1563. /* otherwise, stuffing, nothing to do */
  1564. } else {
  1565. s->mb_incr += code;
  1566. break;
  1567. }
  1568. }
  1569. }
  1570. if(s->mb_y >= s->mb_height){
  1571. fprintf(stderr, "slice too long\n");
  1572. return DECODE_SLICE_ERROR;
  1573. }
  1574. }
  1575. eos: //end of slice
  1576. emms_c();
  1577. /* end of slice reached */
  1578. if (/*s->mb_x == 0 &&*/
  1579. s->mb_y == s->mb_height) {
  1580. /* end of image */
  1581. if(s->mpeg2)
  1582. s->qscale >>=1;
  1583. MPV_frame_end(s);
  1584. if (s->pict_type == B_TYPE || s->low_delay) {
  1585. *pict= *(AVFrame*)&s->current_picture;
  1586. } else {
  1587. s->picture_number++;
  1588. /* latency of 1 frame for I and P frames */
  1589. /* XXX: use another variable than picture_number */
  1590. if (s->last_picture.data[0] == NULL) {
  1591. return DECODE_SLICE_OK;
  1592. } else {
  1593. *pict= *(AVFrame*)&s->last_picture;
  1594. }
  1595. }
  1596. return DECODE_SLICE_EOP;
  1597. } else {
  1598. return DECODE_SLICE_OK;
  1599. }
  1600. }
  1601. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1602. UINT8 *buf, int buf_size)
  1603. {
  1604. Mpeg1Context *s1 = avctx->priv_data;
  1605. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1606. int width, height, i, v, j;
  1607. float aspect;
  1608. init_get_bits(&s->gb, buf, buf_size);
  1609. width = get_bits(&s->gb, 12);
  1610. height = get_bits(&s->gb, 12);
  1611. s->aspect_ratio_info= get_bits(&s->gb, 4);
  1612. if(!s->mpeg2){
  1613. aspect= mpeg1_aspect[s->aspect_ratio_info];
  1614. if(aspect!=0.0) avctx->aspect_ratio= width/(aspect*height);
  1615. }
  1616. s->frame_rate_index = get_bits(&s->gb, 4);
  1617. if (s->frame_rate_index == 0)
  1618. return -1;
  1619. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1620. if (get_bits1(&s->gb) == 0) /* marker */
  1621. return -1;
  1622. if (width <= 0 || height <= 0 ||
  1623. (width % 2) != 0 || (height % 2) != 0)
  1624. return -1;
  1625. if (width != s->width ||
  1626. height != s->height) {
  1627. /* start new mpeg1 context decoding */
  1628. s->out_format = FMT_MPEG1;
  1629. if (s1->mpeg_enc_ctx_allocated) {
  1630. MPV_common_end(s);
  1631. }
  1632. s->width = width;
  1633. s->height = height;
  1634. avctx->has_b_frames= 1;
  1635. s->avctx = avctx;
  1636. avctx->width = width;
  1637. avctx->height = height;
  1638. if (s->frame_rate_index >= 9) {
  1639. /* at least give a valid frame rate (some old mpeg1 have this) */
  1640. avctx->frame_rate = 25 * FRAME_RATE_BASE;
  1641. } else {
  1642. avctx->frame_rate = frame_rate_tab[s->frame_rate_index];
  1643. }
  1644. s->frame_rate = avctx->frame_rate;
  1645. avctx->bit_rate = s->bit_rate;
  1646. if (MPV_common_init(s) < 0)
  1647. return -1;
  1648. s1->mpeg_enc_ctx_allocated = 1;
  1649. }
  1650. skip_bits(&s->gb, 10); /* vbv_buffer_size */
  1651. skip_bits(&s->gb, 1);
  1652. /* get matrix */
  1653. if (get_bits1(&s->gb)) {
  1654. for(i=0;i<64;i++) {
  1655. v = get_bits(&s->gb, 8);
  1656. j = s->intra_scantable.permutated[i];
  1657. s->intra_matrix[j] = v;
  1658. s->chroma_intra_matrix[j] = v;
  1659. }
  1660. #ifdef DEBUG
  1661. dprintf("intra matrix present\n");
  1662. for(i=0;i<64;i++)
  1663. dprintf(" %d", s->intra_matrix[s->intra_scantable.permutated[i]]);
  1664. printf("\n");
  1665. #endif
  1666. } else {
  1667. for(i=0;i<64;i++) {
  1668. int j= s->idct_permutation[i];
  1669. v = ff_mpeg1_default_intra_matrix[i];
  1670. s->intra_matrix[j] = v;
  1671. s->chroma_intra_matrix[j] = v;
  1672. }
  1673. }
  1674. if (get_bits1(&s->gb)) {
  1675. for(i=0;i<64;i++) {
  1676. v = get_bits(&s->gb, 8);
  1677. j = s->intra_scantable.permutated[i];
  1678. s->inter_matrix[j] = v;
  1679. s->chroma_inter_matrix[j] = v;
  1680. }
  1681. #ifdef DEBUG
  1682. dprintf("non intra matrix present\n");
  1683. for(i=0;i<64;i++)
  1684. dprintf(" %d", s->inter_matrix[s->intra_scantable.permutated[i]]);
  1685. printf("\n");
  1686. #endif
  1687. } else {
  1688. for(i=0;i<64;i++) {
  1689. int j= s->idct_permutation[i];
  1690. v = ff_mpeg1_default_non_intra_matrix[i];
  1691. s->inter_matrix[j] = v;
  1692. s->chroma_inter_matrix[j] = v;
  1693. }
  1694. }
  1695. /* we set mpeg2 parameters so that it emulates mpeg1 */
  1696. s->progressive_sequence = 1;
  1697. s->progressive_frame = 1;
  1698. s->picture_structure = PICT_FRAME;
  1699. s->frame_pred_frame_dct = 1;
  1700. s->mpeg2 = 0;
  1701. avctx->sub_id = 1; /* indicates mpeg1 */
  1702. return 0;
  1703. }
  1704. /* handle buffering and image synchronisation */
  1705. static int mpeg_decode_frame(AVCodecContext *avctx,
  1706. void *data, int *data_size,
  1707. UINT8 *buf, int buf_size)
  1708. {
  1709. Mpeg1Context *s = avctx->priv_data;
  1710. UINT8 *buf_end, *buf_ptr, *buf_start;
  1711. int len, start_code_found, ret, code, start_code, input_size;
  1712. AVFrame *picture = data;
  1713. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1714. dprintf("fill_buffer\n");
  1715. *data_size = 0;
  1716. /* special case for last picture */
  1717. if (buf_size == 0) {
  1718. if (s2->picture_number > 0) {
  1719. *picture= *(AVFrame*)&s2->next_picture;
  1720. *data_size = sizeof(AVFrame);
  1721. }
  1722. return 0;
  1723. }
  1724. buf_ptr = buf;
  1725. buf_end = buf + buf_size;
  1726. #if 0
  1727. if (s->repeat_field % 2 == 1) {
  1728. s->repeat_field++;
  1729. //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
  1730. // s2->picture_number, s->repeat_field);
  1731. if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
  1732. *data_size = sizeof(AVPicture);
  1733. goto the_end;
  1734. }
  1735. }
  1736. #endif
  1737. while (buf_ptr < buf_end) {
  1738. buf_start = buf_ptr;
  1739. /* find start next code */
  1740. code = find_start_code(&buf_ptr, buf_end, &s->header_state);
  1741. if (code >= 0) {
  1742. start_code_found = 1;
  1743. } else {
  1744. start_code_found = 0;
  1745. }
  1746. /* copy to buffer */
  1747. len = buf_ptr - buf_start;
  1748. if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
  1749. /* data too big : flush */
  1750. s->buf_ptr = s->buffer;
  1751. if (start_code_found)
  1752. s->start_code = code;
  1753. } else {
  1754. memcpy(s->buf_ptr, buf_start, len);
  1755. s->buf_ptr += len;
  1756. if( (!(s2->flags&CODEC_FLAG_TRUNCATED)) && (!start_code_found)
  1757. && s->buf_ptr+4<s->buffer+s->buffer_size){
  1758. start_code_found= 1;
  1759. code= 0x1FF;
  1760. s->header_state=0xFF;
  1761. s->buf_ptr[0]=0;
  1762. s->buf_ptr[1]=0;
  1763. s->buf_ptr[2]=1;
  1764. s->buf_ptr[3]=0xFF;
  1765. s->buf_ptr+=4;
  1766. }
  1767. if (start_code_found) {
  1768. /* prepare data for next start code */
  1769. input_size = s->buf_ptr - s->buffer;
  1770. start_code = s->start_code;
  1771. s->buf_ptr = s->buffer;
  1772. s->start_code = code;
  1773. switch(start_code) {
  1774. case SEQ_START_CODE:
  1775. mpeg1_decode_sequence(avctx, s->buffer,
  1776. input_size);
  1777. break;
  1778. case PICTURE_START_CODE:
  1779. /* we have a complete image : we try to decompress it */
  1780. mpeg1_decode_picture(avctx,
  1781. s->buffer, input_size);
  1782. break;
  1783. case EXT_START_CODE:
  1784. mpeg_decode_extension(avctx,
  1785. s->buffer, input_size);
  1786. break;
  1787. default:
  1788. if (start_code >= SLICE_MIN_START_CODE &&
  1789. start_code <= SLICE_MAX_START_CODE) {
  1790. /* skip b frames if we dont have reference frames */
  1791. if(s2->last_picture.data[0]==NULL && s2->pict_type==B_TYPE) break;
  1792. /* skip b frames if we are in a hurry */
  1793. if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
  1794. /* skip everything if we are in a hurry>=5 */
  1795. if(avctx->hurry_up>=5) break;
  1796. ret = mpeg_decode_slice(avctx, picture,
  1797. start_code, s->buffer, input_size);
  1798. if (ret == DECODE_SLICE_EOP) {
  1799. /* got a picture: exit */
  1800. /* first check if we must repeat the frame */
  1801. avctx->repeat_pict = 0;
  1802. #if 0
  1803. if (s2->progressive_frame && s2->repeat_first_field) {
  1804. //fprintf(stderr,"\nRepeat this frame: %d! pict: %d",avctx->frame_number,s2->picture_number);
  1805. //s2->repeat_first_field = 0;
  1806. //s2->progressive_frame = 0;
  1807. if (++s->repeat_field > 2)
  1808. s->repeat_field = 0;
  1809. avctx->repeat_pict = 1;
  1810. }
  1811. #endif
  1812. if (s2->repeat_first_field) {
  1813. if (s2->progressive_sequence) {
  1814. if (s2->top_field_first)
  1815. avctx->repeat_pict = 4;
  1816. else
  1817. avctx->repeat_pict = 2;
  1818. } else if (s2->progressive_frame) {
  1819. avctx->repeat_pict = 1;
  1820. }
  1821. }
  1822. *data_size = sizeof(AVPicture);
  1823. goto the_end;
  1824. }else if(ret<0){
  1825. fprintf(stderr,"Error while decoding slice\n");
  1826. if(ret==DECODE_SLICE_FATAL_ERROR) return -1;
  1827. }
  1828. }
  1829. break;
  1830. }
  1831. }
  1832. }
  1833. }
  1834. the_end:
  1835. return buf_ptr - buf;
  1836. }
  1837. static int mpeg_decode_end(AVCodecContext *avctx)
  1838. {
  1839. Mpeg1Context *s = avctx->priv_data;
  1840. if (s->mpeg_enc_ctx_allocated)
  1841. MPV_common_end(&s->mpeg_enc_ctx);
  1842. return 0;
  1843. }
  1844. AVCodec mpeg_decoder = {
  1845. "mpegvideo",
  1846. CODEC_TYPE_VIDEO,
  1847. CODEC_ID_MPEG1VIDEO,
  1848. sizeof(Mpeg1Context),
  1849. mpeg_decode_init,
  1850. NULL,
  1851. mpeg_decode_end,
  1852. mpeg_decode_frame,
  1853. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  1854. };