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.

2062 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. assert(s->mb_skiped==0);
  669. if (--s->mb_incr != 0) {
  670. /* skip mb */
  671. s->mb_intra = 0;
  672. for(i=0;i<6;i++)
  673. s->block_last_index[i] = -1;
  674. s->mv_type = MV_TYPE_16X16;
  675. if (s->pict_type == P_TYPE) {
  676. /* if P type, zero motion vector is implied */
  677. s->mv_dir = MV_DIR_FORWARD;
  678. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  679. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  680. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  681. s->mb_skiped = 1;
  682. } else {
  683. /* if B type, reuse previous vectors and directions */
  684. s->mv[0][0][0] = s->last_mv[0][0][0];
  685. s->mv[0][0][1] = s->last_mv[0][0][1];
  686. s->mv[1][0][0] = s->last_mv[1][0][0];
  687. s->mv[1][0][1] = s->last_mv[1][0][1];
  688. if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
  689. s->mb_skiped = 1;
  690. }
  691. return 0;
  692. }
  693. switch(s->pict_type) {
  694. default:
  695. case I_TYPE:
  696. if (get_bits1(&s->gb) == 0) {
  697. if (get_bits1(&s->gb) == 0)
  698. return -1;
  699. mb_type = MB_QUANT | MB_INTRA;
  700. } else {
  701. mb_type = MB_INTRA;
  702. }
  703. break;
  704. case P_TYPE:
  705. mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
  706. if (mb_type < 0){
  707. fprintf(stderr, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
  708. return -1;
  709. }
  710. break;
  711. case B_TYPE:
  712. mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
  713. if (mb_type < 0){
  714. fprintf(stderr, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
  715. return -1;
  716. }
  717. break;
  718. }
  719. dprintf("mb_type=%x\n", mb_type);
  720. motion_type = 0; /* avoid warning */
  721. if (mb_type & (MB_FOR|MB_BACK)) {
  722. /* get additionnal motion vector type */
  723. if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct)
  724. motion_type = MT_FRAME;
  725. else
  726. motion_type = get_bits(&s->gb, 2);
  727. }
  728. /* compute dct type */
  729. if (s->picture_structure == PICT_FRAME &&
  730. !s->frame_pred_frame_dct &&
  731. (mb_type & (MB_PAT | MB_INTRA))) {
  732. s->interlaced_dct = get_bits1(&s->gb);
  733. #ifdef DEBUG
  734. if (s->interlaced_dct)
  735. printf("interlaced_dct\n");
  736. #endif
  737. } else {
  738. s->interlaced_dct = 0; /* frame based */
  739. }
  740. if (mb_type & MB_QUANT) {
  741. s->qscale = get_qscale(s);
  742. }
  743. if (mb_type & MB_INTRA) {
  744. if (s->concealment_motion_vectors) {
  745. /* just parse them */
  746. if (s->picture_structure != PICT_FRAME)
  747. skip_bits1(&s->gb); /* field select */
  748. mpeg_decode_motion(s, s->mpeg_f_code[0][0], 0);
  749. mpeg_decode_motion(s, s->mpeg_f_code[0][1], 0);
  750. }
  751. s->mb_intra = 1;
  752. cbp = 0x3f;
  753. memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
  754. } else {
  755. s->mb_intra = 0;
  756. cbp = 0;
  757. }
  758. /* special case of implicit zero motion vector */
  759. if (s->pict_type == P_TYPE && !(mb_type & MB_FOR)) {
  760. s->mv_dir = MV_DIR_FORWARD;
  761. s->mv_type = MV_TYPE_16X16;
  762. s->last_mv[0][0][0] = 0;
  763. s->last_mv[0][0][1] = 0;
  764. s->last_mv[0][1][0] = 0;
  765. s->last_mv[0][1][1] = 0;
  766. s->mv[0][0][0] = 0;
  767. s->mv[0][0][1] = 0;
  768. } else if (mb_type & (MB_FOR | MB_BACK)) {
  769. /* motion vectors */
  770. s->mv_dir = 0;
  771. for(i=0;i<2;i++) {
  772. if (mb_type & (MB_FOR >> i)) {
  773. s->mv_dir |= (MV_DIR_FORWARD >> i);
  774. dprintf("motion_type=%d\n", motion_type);
  775. switch(motion_type) {
  776. case MT_FRAME: /* or MT_16X8 */
  777. if (s->picture_structure == PICT_FRAME) {
  778. /* MT_FRAME */
  779. s->mv_type = MV_TYPE_16X16;
  780. for(k=0;k<2;k++) {
  781. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  782. s->last_mv[i][0][k]);
  783. s->last_mv[i][0][k] = val;
  784. s->last_mv[i][1][k] = val;
  785. /* full_pel: only for mpeg1 */
  786. if (s->full_pel[i])
  787. val = val << 1;
  788. s->mv[i][0][k] = val;
  789. dprintf("mv%d: %d\n", k, val);
  790. }
  791. } else {
  792. /* MT_16X8 */
  793. s->mv_type = MV_TYPE_16X8;
  794. for(j=0;j<2;j++) {
  795. s->field_select[i][j] = get_bits1(&s->gb);
  796. for(k=0;k<2;k++) {
  797. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  798. s->last_mv[i][j][k]);
  799. s->last_mv[i][j][k] = val;
  800. s->mv[i][j][k] = val;
  801. }
  802. }
  803. }
  804. break;
  805. case MT_FIELD:
  806. if (s->picture_structure == PICT_FRAME) {
  807. s->mv_type = MV_TYPE_FIELD;
  808. for(j=0;j<2;j++) {
  809. s->field_select[i][j] = get_bits1(&s->gb);
  810. val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  811. s->last_mv[i][j][0]);
  812. s->last_mv[i][j][0] = val;
  813. s->mv[i][j][0] = val;
  814. dprintf("fmx=%d\n", val);
  815. val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  816. s->last_mv[i][j][1] >> 1);
  817. s->last_mv[i][j][1] = val << 1;
  818. s->mv[i][j][1] = val;
  819. dprintf("fmy=%d\n", val);
  820. }
  821. } else {
  822. s->mv_type = MV_TYPE_16X16;
  823. s->field_select[i][0] = get_bits1(&s->gb);
  824. for(k=0;k<2;k++) {
  825. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  826. s->last_mv[i][0][k]);
  827. s->last_mv[i][0][k] = val;
  828. s->last_mv[i][1][k] = val;
  829. s->mv[i][0][k] = val;
  830. }
  831. }
  832. break;
  833. case MT_DMV:
  834. {
  835. int dmx, dmy, mx, my, m;
  836. mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  837. s->last_mv[i][0][0]);
  838. s->last_mv[i][0][0] = mx;
  839. s->last_mv[i][1][0] = mx;
  840. dmx = get_dmv(s);
  841. my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  842. s->last_mv[i][0][1] >> 1);
  843. dmy = get_dmv(s);
  844. s->mv_type = MV_TYPE_DMV;
  845. /* XXX: totally broken */
  846. if (s->picture_structure == PICT_FRAME) {
  847. s->last_mv[i][0][1] = my << 1;
  848. s->last_mv[i][1][1] = my << 1;
  849. m = s->top_field_first ? 1 : 3;
  850. /* top -> top pred */
  851. s->mv[i][0][0] = mx;
  852. s->mv[i][0][1] = my << 1;
  853. s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  854. s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
  855. m = 4 - m;
  856. s->mv[i][2][0] = mx;
  857. s->mv[i][2][1] = my << 1;
  858. s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  859. s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
  860. } else {
  861. s->last_mv[i][0][1] = my;
  862. s->last_mv[i][1][1] = my;
  863. s->mv[i][0][0] = mx;
  864. s->mv[i][0][1] = my;
  865. s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx;
  866. s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1
  867. /* + 2 * cur_field */;
  868. }
  869. }
  870. break;
  871. }
  872. }
  873. }
  874. }
  875. if ((mb_type & MB_INTRA) && s->concealment_motion_vectors) {
  876. skip_bits1(&s->gb); /* marker */
  877. }
  878. if (mb_type & MB_PAT) {
  879. cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
  880. if (cbp < 0){
  881. fprintf(stderr, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
  882. return -1;
  883. }
  884. cbp++;
  885. }
  886. dprintf("cbp=%x\n", cbp);
  887. if (s->mpeg2) {
  888. if (s->mb_intra) {
  889. for(i=0;i<6;i++) {
  890. if (mpeg2_decode_block_intra(s, block[i], i) < 0)
  891. return -1;
  892. }
  893. } else {
  894. for(i=0;i<6;i++) {
  895. if (cbp & 32) {
  896. if (mpeg2_decode_block_non_intra(s, block[i], i) < 0)
  897. return -1;
  898. } else {
  899. s->block_last_index[i] = -1;
  900. }
  901. cbp+=cbp;
  902. }
  903. }
  904. } else {
  905. if (s->mb_intra) {
  906. for(i=0;i<6;i++) {
  907. if (mpeg1_decode_block_intra(s, block[i], i) < 0)
  908. return -1;
  909. }
  910. }else{
  911. for(i=0;i<6;i++) {
  912. if (cbp & 32) {
  913. if (mpeg1_decode_block_inter(s, block[i], i) < 0)
  914. return -1;
  915. } else {
  916. s->block_last_index[i] = -1;
  917. }
  918. cbp+=cbp;
  919. }
  920. }
  921. }
  922. return 0;
  923. }
  924. /* as h263, but only 17 codes */
  925. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
  926. {
  927. int code, sign, val, m, l, shift;
  928. code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
  929. if (code < 0) {
  930. return 0xffff;
  931. }
  932. if (code == 0) {
  933. return pred;
  934. }
  935. sign = get_bits1(&s->gb);
  936. shift = fcode - 1;
  937. val = (code - 1) << shift;
  938. if (shift > 0)
  939. val |= get_bits(&s->gb, shift);
  940. val++;
  941. if (sign)
  942. val = -val;
  943. val += pred;
  944. /* modulo decoding */
  945. l = (1 << shift) * 16;
  946. m = 2 * l;
  947. if (val < -l) {
  948. val += m;
  949. } else if (val >= l) {
  950. val -= m;
  951. }
  952. return val;
  953. }
  954. static inline int decode_dc(MpegEncContext *s, int component)
  955. {
  956. int code, diff;
  957. if (component == 0) {
  958. code = get_vlc2(&s->gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
  959. } else {
  960. code = get_vlc2(&s->gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
  961. }
  962. if (code < 0){
  963. fprintf(stderr, "invalid dc code at %d %d\n", s->mb_x, s->mb_y);
  964. return 0xffff;
  965. }
  966. if (code == 0) {
  967. diff = 0;
  968. } else {
  969. diff = get_bits(&s->gb, code);
  970. if ((diff & (1 << (code - 1))) == 0)
  971. diff = (-1 << code) | (diff + 1);
  972. }
  973. return diff;
  974. }
  975. static inline int mpeg1_decode_block_intra(MpegEncContext *s,
  976. DCTELEM *block,
  977. int n)
  978. {
  979. int level, dc, diff, i, j, run;
  980. int component;
  981. RLTable *rl = &rl_mpeg1;
  982. UINT8 * const scantable= s->intra_scantable.permutated;
  983. const UINT16 *quant_matrix= s->intra_matrix;
  984. const int qscale= s->qscale;
  985. /* DC coef */
  986. component = (n <= 3 ? 0 : n - 4 + 1);
  987. diff = decode_dc(s, component);
  988. if (diff >= 0xffff)
  989. return -1;
  990. dc = s->last_dc[component];
  991. dc += diff;
  992. s->last_dc[component] = dc;
  993. block[0] = dc<<3;
  994. dprintf("dc=%d diff=%d\n", dc, diff);
  995. i = 0;
  996. {
  997. OPEN_READER(re, &s->gb);
  998. /* now quantify & encode AC coefs */
  999. for(;;) {
  1000. UPDATE_CACHE(re, &s->gb);
  1001. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  1002. if(level == 127){
  1003. break;
  1004. } else if(level != 0) {
  1005. i += run;
  1006. j = scantable[i];
  1007. level= (level*qscale*quant_matrix[j])>>3;
  1008. level= (level-1)|1;
  1009. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1010. LAST_SKIP_BITS(re, &s->gb, 1);
  1011. } else {
  1012. /* escape */
  1013. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1014. UPDATE_CACHE(re, &s->gb);
  1015. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  1016. if (level == -128) {
  1017. level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
  1018. } else if (level == 0) {
  1019. level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
  1020. }
  1021. i += run;
  1022. j = scantable[i];
  1023. if(level<0){
  1024. level= -level;
  1025. level= (level*qscale*quant_matrix[j])>>3;
  1026. level= (level-1)|1;
  1027. level= -level;
  1028. }else{
  1029. level= (level*qscale*quant_matrix[j])>>3;
  1030. level= (level-1)|1;
  1031. }
  1032. }
  1033. if (i > 63){
  1034. fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1035. return -1;
  1036. }
  1037. block[j] = level;
  1038. }
  1039. CLOSE_READER(re, &s->gb);
  1040. }
  1041. s->block_last_index[n] = i;
  1042. return 0;
  1043. }
  1044. static inline int mpeg1_decode_block_inter(MpegEncContext *s,
  1045. DCTELEM *block,
  1046. int n)
  1047. {
  1048. int level, i, j, run;
  1049. RLTable *rl = &rl_mpeg1;
  1050. UINT8 * const scantable= s->intra_scantable.permutated;
  1051. const UINT16 *quant_matrix= s->inter_matrix;
  1052. const int qscale= s->qscale;
  1053. {
  1054. int v;
  1055. OPEN_READER(re, &s->gb);
  1056. i = -1;
  1057. /* special case for the first coef. no need to add a second vlc table */
  1058. UPDATE_CACHE(re, &s->gb);
  1059. v= SHOW_UBITS(re, &s->gb, 2);
  1060. if (v & 2) {
  1061. LAST_SKIP_BITS(re, &s->gb, 2);
  1062. level= (3*qscale*quant_matrix[0])>>4;
  1063. level= (level-1)|1;
  1064. if(v&1)
  1065. level= -level;
  1066. block[0] = level;
  1067. i++;
  1068. }
  1069. /* now quantify & encode AC coefs */
  1070. for(;;) {
  1071. UPDATE_CACHE(re, &s->gb);
  1072. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  1073. if(level == 127){
  1074. break;
  1075. } else if(level != 0) {
  1076. i += run;
  1077. j = scantable[i];
  1078. level= ((level*2+1)*qscale*quant_matrix[j])>>4;
  1079. level= (level-1)|1;
  1080. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1081. LAST_SKIP_BITS(re, &s->gb, 1);
  1082. } else {
  1083. /* escape */
  1084. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1085. UPDATE_CACHE(re, &s->gb);
  1086. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  1087. if (level == -128) {
  1088. level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
  1089. } else if (level == 0) {
  1090. level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
  1091. }
  1092. i += run;
  1093. j = scantable[i];
  1094. if(level<0){
  1095. level= -level;
  1096. level= ((level*2+1)*qscale*quant_matrix[j])>>4;
  1097. level= (level-1)|1;
  1098. level= -level;
  1099. }else{
  1100. level= ((level*2+1)*qscale*quant_matrix[j])>>4;
  1101. level= (level-1)|1;
  1102. }
  1103. }
  1104. if (i > 63){
  1105. fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1106. return -1;
  1107. }
  1108. block[j] = level;
  1109. }
  1110. CLOSE_READER(re, &s->gb);
  1111. }
  1112. s->block_last_index[n] = i;
  1113. return 0;
  1114. }
  1115. /* Also does unquantization here, since I will never support mpeg2
  1116. encoding */
  1117. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
  1118. DCTELEM *block,
  1119. int n)
  1120. {
  1121. int level, i, j, run;
  1122. RLTable *rl = &rl_mpeg1;
  1123. UINT8 * const scantable= s->intra_scantable.permutated;
  1124. const UINT16 *quant_matrix;
  1125. const int qscale= s->qscale;
  1126. int mismatch;
  1127. mismatch = 1;
  1128. {
  1129. int v;
  1130. OPEN_READER(re, &s->gb);
  1131. i = -1;
  1132. if (n < 4)
  1133. quant_matrix = s->inter_matrix;
  1134. else
  1135. quant_matrix = s->chroma_inter_matrix;
  1136. /* special case for the first coef. no need to add a second vlc table */
  1137. UPDATE_CACHE(re, &s->gb);
  1138. v= SHOW_UBITS(re, &s->gb, 2);
  1139. if (v & 2) {
  1140. LAST_SKIP_BITS(re, &s->gb, 2);
  1141. level= (3*qscale*quant_matrix[0])>>5;
  1142. if(v&1)
  1143. level= -level;
  1144. block[0] = level;
  1145. mismatch ^= level;
  1146. i++;
  1147. }
  1148. /* now quantify & encode AC coefs */
  1149. for(;;) {
  1150. UPDATE_CACHE(re, &s->gb);
  1151. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  1152. if(level == 127){
  1153. break;
  1154. } else if(level != 0) {
  1155. i += run;
  1156. j = scantable[i];
  1157. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1158. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1159. LAST_SKIP_BITS(re, &s->gb, 1);
  1160. } else {
  1161. /* escape */
  1162. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1163. UPDATE_CACHE(re, &s->gb);
  1164. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1165. i += run;
  1166. j = scantable[i];
  1167. if(level<0){
  1168. level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
  1169. level= -level;
  1170. }else{
  1171. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1172. }
  1173. }
  1174. if (i > 63){
  1175. fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1176. return -1;
  1177. }
  1178. mismatch ^= level;
  1179. block[j] = level;
  1180. }
  1181. CLOSE_READER(re, &s->gb);
  1182. }
  1183. block[63] ^= (mismatch & 1);
  1184. s->block_last_index[n] = i;
  1185. return 0;
  1186. }
  1187. static inline int mpeg2_decode_block_intra(MpegEncContext *s,
  1188. DCTELEM *block,
  1189. int n)
  1190. {
  1191. int level, dc, diff, i, j, run;
  1192. int component;
  1193. RLTable *rl;
  1194. UINT8 * const scantable= s->intra_scantable.permutated;
  1195. const UINT16 *quant_matrix;
  1196. const int qscale= s->qscale;
  1197. int mismatch;
  1198. /* DC coef */
  1199. if (n < 4){
  1200. quant_matrix = s->intra_matrix;
  1201. component = 0;
  1202. }else{
  1203. quant_matrix = s->chroma_intra_matrix;
  1204. component = n - 3;
  1205. }
  1206. diff = decode_dc(s, component);
  1207. if (diff >= 0xffff)
  1208. return -1;
  1209. dc = s->last_dc[component];
  1210. dc += diff;
  1211. s->last_dc[component] = dc;
  1212. block[0] = dc << (3 - s->intra_dc_precision);
  1213. dprintf("dc=%d\n", block[0]);
  1214. mismatch = block[0] ^ 1;
  1215. i = 0;
  1216. if (s->intra_vlc_format)
  1217. rl = &rl_mpeg2;
  1218. else
  1219. rl = &rl_mpeg1;
  1220. {
  1221. OPEN_READER(re, &s->gb);
  1222. /* now quantify & encode AC coefs */
  1223. for(;;) {
  1224. UPDATE_CACHE(re, &s->gb);
  1225. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  1226. if(level == 127){
  1227. break;
  1228. } else if(level != 0) {
  1229. i += run;
  1230. j = scantable[i];
  1231. level= (level*qscale*quant_matrix[j])>>4;
  1232. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1233. LAST_SKIP_BITS(re, &s->gb, 1);
  1234. } else {
  1235. /* escape */
  1236. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1237. UPDATE_CACHE(re, &s->gb);
  1238. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1239. i += run;
  1240. j = scantable[i];
  1241. if(level<0){
  1242. level= (-level*qscale*quant_matrix[j])>>4;
  1243. level= -level;
  1244. }else{
  1245. level= (level*qscale*quant_matrix[j])>>4;
  1246. }
  1247. }
  1248. if (i > 63){
  1249. fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1250. return -1;
  1251. }
  1252. mismatch^= level;
  1253. block[j] = level;
  1254. }
  1255. CLOSE_READER(re, &s->gb);
  1256. }
  1257. block[63]^= mismatch&1;
  1258. s->block_last_index[n] = i;
  1259. return 0;
  1260. }
  1261. /* compressed picture size */
  1262. #define PICTURE_BUFFER_SIZE 100000
  1263. typedef struct Mpeg1Context {
  1264. MpegEncContext mpeg_enc_ctx;
  1265. UINT32 header_state;
  1266. int start_code; /* current start code */
  1267. UINT8 buffer[PICTURE_BUFFER_SIZE];
  1268. UINT8 *buf_ptr;
  1269. int buffer_size;
  1270. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  1271. int repeat_field; /* true if we must repeat the field */
  1272. } Mpeg1Context;
  1273. static int mpeg_decode_init(AVCodecContext *avctx)
  1274. {
  1275. Mpeg1Context *s = avctx->priv_data;
  1276. s->mpeg_enc_ctx.flags= avctx->flags;
  1277. common_init(&s->mpeg_enc_ctx);
  1278. init_vlcs(&s->mpeg_enc_ctx);
  1279. s->header_state = 0xff;
  1280. s->mpeg_enc_ctx_allocated = 0;
  1281. s->buffer_size = PICTURE_BUFFER_SIZE;
  1282. s->start_code = -1;
  1283. s->buf_ptr = s->buffer;
  1284. s->mpeg_enc_ctx.picture_number = 0;
  1285. s->repeat_field = 0;
  1286. s->mpeg_enc_ctx.codec_id= avctx->codec->id;
  1287. return 0;
  1288. }
  1289. /* return the 8 bit start code value and update the search
  1290. state. Return -1 if no start code found */
  1291. static int find_start_code(UINT8 **pbuf_ptr, UINT8 *buf_end,
  1292. UINT32 *header_state)
  1293. {
  1294. UINT8 *buf_ptr;
  1295. unsigned int state, v;
  1296. int val;
  1297. state = *header_state;
  1298. buf_ptr = *pbuf_ptr;
  1299. while (buf_ptr < buf_end) {
  1300. v = *buf_ptr++;
  1301. if (state == 0x000001) {
  1302. state = ((state << 8) | v) & 0xffffff;
  1303. val = state;
  1304. goto found;
  1305. }
  1306. state = ((state << 8) | v) & 0xffffff;
  1307. }
  1308. val = -1;
  1309. found:
  1310. *pbuf_ptr = buf_ptr;
  1311. *header_state = state;
  1312. return val;
  1313. }
  1314. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1315. UINT8 *buf, int buf_size)
  1316. {
  1317. Mpeg1Context *s1 = avctx->priv_data;
  1318. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1319. int ref, f_code;
  1320. init_get_bits(&s->gb, buf, buf_size*8);
  1321. ref = get_bits(&s->gb, 10); /* temporal ref */
  1322. s->pict_type = get_bits(&s->gb, 3);
  1323. dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number);
  1324. skip_bits(&s->gb, 16);
  1325. if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
  1326. s->full_pel[0] = get_bits1(&s->gb);
  1327. f_code = get_bits(&s->gb, 3);
  1328. if (f_code == 0)
  1329. return -1;
  1330. s->mpeg_f_code[0][0] = f_code;
  1331. s->mpeg_f_code[0][1] = f_code;
  1332. }
  1333. if (s->pict_type == B_TYPE) {
  1334. s->full_pel[1] = get_bits1(&s->gb);
  1335. f_code = get_bits(&s->gb, 3);
  1336. if (f_code == 0)
  1337. return -1;
  1338. s->mpeg_f_code[1][0] = f_code;
  1339. s->mpeg_f_code[1][1] = f_code;
  1340. }
  1341. s->current_picture.pict_type= s->pict_type;
  1342. s->current_picture.key_frame= s->pict_type == I_TYPE;
  1343. s->y_dc_scale = 8;
  1344. s->c_dc_scale = 8;
  1345. s->first_slice = 1;
  1346. return 0;
  1347. }
  1348. static void mpeg_decode_sequence_extension(MpegEncContext *s)
  1349. {
  1350. int horiz_size_ext, vert_size_ext;
  1351. int bit_rate_ext, vbv_buf_ext;
  1352. int frame_rate_ext_n, frame_rate_ext_d;
  1353. float aspect;
  1354. skip_bits(&s->gb, 8); /* profil and level */
  1355. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1356. skip_bits(&s->gb, 2); /* chroma_format */
  1357. horiz_size_ext = get_bits(&s->gb, 2);
  1358. vert_size_ext = get_bits(&s->gb, 2);
  1359. s->width |= (horiz_size_ext << 12);
  1360. s->height |= (vert_size_ext << 12);
  1361. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1362. s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400;
  1363. skip_bits1(&s->gb); /* marker */
  1364. vbv_buf_ext = get_bits(&s->gb, 8);
  1365. s->low_delay = get_bits1(&s->gb);
  1366. frame_rate_ext_n = get_bits(&s->gb, 2);
  1367. frame_rate_ext_d = get_bits(&s->gb, 5);
  1368. if (frame_rate_ext_d >= 1)
  1369. s->frame_rate = (s->frame_rate * frame_rate_ext_n) / frame_rate_ext_d;
  1370. dprintf("sequence extension\n");
  1371. s->mpeg2 = 1;
  1372. s->avctx->sub_id = 2; /* indicates mpeg2 found */
  1373. aspect= mpeg2_aspect[s->aspect_ratio_info];
  1374. if(aspect>0.0) s->avctx->aspect_ratio= s->width/(aspect*s->height);
  1375. else if(aspect<0.0) s->avctx->aspect_ratio= -1.0/aspect;
  1376. }
  1377. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1378. {
  1379. int i, v, j;
  1380. dprintf("matrix extension\n");
  1381. if (get_bits1(&s->gb)) {
  1382. for(i=0;i<64;i++) {
  1383. v = get_bits(&s->gb, 8);
  1384. j= s->idct_permutation[ ff_zigzag_direct[i] ];
  1385. s->intra_matrix[j] = v;
  1386. s->chroma_intra_matrix[j] = v;
  1387. }
  1388. }
  1389. if (get_bits1(&s->gb)) {
  1390. for(i=0;i<64;i++) {
  1391. v = get_bits(&s->gb, 8);
  1392. j= s->idct_permutation[ ff_zigzag_direct[i] ];
  1393. s->inter_matrix[j] = v;
  1394. s->chroma_inter_matrix[j] = v;
  1395. }
  1396. }
  1397. if (get_bits1(&s->gb)) {
  1398. for(i=0;i<64;i++) {
  1399. v = get_bits(&s->gb, 8);
  1400. j= s->idct_permutation[ ff_zigzag_direct[i] ];
  1401. s->chroma_intra_matrix[j] = v;
  1402. }
  1403. }
  1404. if (get_bits1(&s->gb)) {
  1405. for(i=0;i<64;i++) {
  1406. v = get_bits(&s->gb, 8);
  1407. j= s->idct_permutation[ ff_zigzag_direct[i] ];
  1408. s->chroma_inter_matrix[j] = v;
  1409. }
  1410. }
  1411. }
  1412. static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
  1413. {
  1414. s->full_pel[0] = s->full_pel[1] = 0;
  1415. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1416. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1417. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1418. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1419. s->intra_dc_precision = get_bits(&s->gb, 2);
  1420. s->picture_structure = get_bits(&s->gb, 2);
  1421. s->top_field_first = get_bits1(&s->gb);
  1422. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1423. s->concealment_motion_vectors = get_bits1(&s->gb);
  1424. s->q_scale_type = get_bits1(&s->gb);
  1425. s->intra_vlc_format = get_bits1(&s->gb);
  1426. s->alternate_scan = get_bits1(&s->gb);
  1427. s->repeat_first_field = get_bits1(&s->gb);
  1428. s->chroma_420_type = get_bits1(&s->gb);
  1429. s->progressive_frame = get_bits1(&s->gb);
  1430. if(s->alternate_scan){
  1431. ff_init_scantable(s, &s->inter_scantable , ff_alternate_vertical_scan);
  1432. ff_init_scantable(s, &s->intra_scantable , ff_alternate_vertical_scan);
  1433. ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_vertical_scan);
  1434. ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
  1435. }else{
  1436. ff_init_scantable(s, &s->inter_scantable , ff_zigzag_direct);
  1437. ff_init_scantable(s, &s->intra_scantable , ff_zigzag_direct);
  1438. ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  1439. ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
  1440. }
  1441. /* composite display not parsed */
  1442. dprintf("intra_dc_precision=%d\n", s->intra_dc_precision);
  1443. dprintf("picture_structure=%d\n", s->picture_structure);
  1444. dprintf("top field first=%d\n", s->top_field_first);
  1445. dprintf("repeat first field=%d\n", s->repeat_first_field);
  1446. dprintf("conceal=%d\n", s->concealment_motion_vectors);
  1447. dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
  1448. dprintf("alternate_scan=%d\n", s->alternate_scan);
  1449. dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1450. dprintf("progressive_frame=%d\n", s->progressive_frame);
  1451. }
  1452. static void mpeg_decode_extension(AVCodecContext *avctx,
  1453. UINT8 *buf, int buf_size)
  1454. {
  1455. Mpeg1Context *s1 = avctx->priv_data;
  1456. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1457. int ext_type;
  1458. init_get_bits(&s->gb, buf, buf_size*8);
  1459. ext_type = get_bits(&s->gb, 4);
  1460. switch(ext_type) {
  1461. case 0x1:
  1462. /* sequence ext */
  1463. mpeg_decode_sequence_extension(s);
  1464. break;
  1465. case 0x3:
  1466. /* quant matrix extension */
  1467. mpeg_decode_quant_matrix_extension(s);
  1468. break;
  1469. case 0x8:
  1470. /* picture extension */
  1471. mpeg_decode_picture_coding_extension(s);
  1472. break;
  1473. }
  1474. }
  1475. #define DECODE_SLICE_FATAL_ERROR -2
  1476. #define DECODE_SLICE_ERROR -1
  1477. #define DECODE_SLICE_OK 0
  1478. #define DECODE_SLICE_EOP 1
  1479. /**
  1480. * decodes a slice.
  1481. * @return DECODE_SLICE_FATAL_ERROR if a non recoverable error occured<br>
  1482. * DECODE_SLICE_ERROR if the slice is damaged<br>
  1483. * DECODE_SLICE_OK if this slice is ok<br>
  1484. * DECODE_SLICE_EOP if the end of the picture is reached
  1485. */
  1486. static int mpeg_decode_slice(AVCodecContext *avctx,
  1487. AVFrame *pict,
  1488. int start_code,
  1489. UINT8 *buf, int buf_size)
  1490. {
  1491. Mpeg1Context *s1 = avctx->priv_data;
  1492. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1493. int ret;
  1494. start_code = (start_code - 1) & 0xff;
  1495. if (start_code >= s->mb_height){
  1496. fprintf(stderr, "slice below image (%d >= %d)\n", start_code, s->mb_height);
  1497. return DECODE_SLICE_ERROR;
  1498. }
  1499. s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
  1500. s->last_dc[1] = s->last_dc[0];
  1501. s->last_dc[2] = s->last_dc[0];
  1502. memset(s->last_mv, 0, sizeof(s->last_mv));
  1503. /* start frame decoding */
  1504. if (s->first_slice) {
  1505. s->first_slice = 0;
  1506. if(MPV_frame_start(s, avctx) < 0)
  1507. return DECODE_SLICE_FATAL_ERROR;
  1508. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  1509. printf("qp:%d fc:%2d%2d%2d%2d %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
  1510. 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],
  1511. s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")),
  1512. s->progressive_sequence ? "pro" :"", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
  1513. s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
  1514. s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
  1515. }
  1516. }
  1517. init_get_bits(&s->gb, buf, buf_size*8);
  1518. s->qscale = get_qscale(s);
  1519. /* extra slice info */
  1520. while (get_bits1(&s->gb) != 0) {
  1521. skip_bits(&s->gb, 8);
  1522. }
  1523. s->mb_x=0;
  1524. for(;;) {
  1525. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1526. if (code < 0)
  1527. return -1; /* error = end of slice, but empty slice is bad or?*/
  1528. if (code >= 33) {
  1529. if (code == 33) {
  1530. s->mb_x += 33;
  1531. }
  1532. /* otherwise, stuffing, nothing to do */
  1533. } else {
  1534. s->mb_x += code;
  1535. break;
  1536. }
  1537. }
  1538. s->mb_y = start_code;
  1539. s->mb_incr= 1;
  1540. for(;;) {
  1541. s->dsp.clear_blocks(s->block[0]);
  1542. ret = mpeg_decode_mb(s, s->block);
  1543. dprintf("ret=%d\n", ret);
  1544. if (ret < 0)
  1545. return -1;
  1546. MPV_decode_mb(s, s->block);
  1547. if (++s->mb_x >= s->mb_width) {
  1548. ff_draw_horiz_band(s);
  1549. s->mb_x = 0;
  1550. s->mb_y++;
  1551. PRINT_QP("%s", "\n");
  1552. }
  1553. PRINT_QP("%2d", s->qscale);
  1554. /* skip mb handling */
  1555. if (s->mb_incr == 0) {
  1556. /* read again increment */
  1557. s->mb_incr = 1;
  1558. for(;;) {
  1559. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1560. if (code < 0)
  1561. goto eos; /* error = end of slice */
  1562. if (code >= 33) {
  1563. if (code == 33) {
  1564. s->mb_incr += 33;
  1565. }
  1566. /* otherwise, stuffing, nothing to do */
  1567. } else {
  1568. s->mb_incr += code;
  1569. break;
  1570. }
  1571. }
  1572. }
  1573. if(s->mb_y >= s->mb_height){
  1574. fprintf(stderr, "slice too long\n");
  1575. return DECODE_SLICE_ERROR;
  1576. }
  1577. }
  1578. eos: //end of slice
  1579. emms_c();
  1580. /* end of slice reached */
  1581. if (/*s->mb_x == 0 &&*/
  1582. s->mb_y == s->mb_height) {
  1583. /* end of image */
  1584. if(s->mpeg2)
  1585. s->qscale >>=1;
  1586. MPV_frame_end(s);
  1587. if (s->pict_type == B_TYPE || s->low_delay) {
  1588. *pict= *(AVFrame*)&s->current_picture;
  1589. } else {
  1590. s->picture_number++;
  1591. /* latency of 1 frame for I and P frames */
  1592. /* XXX: use another variable than picture_number */
  1593. if (s->last_picture.data[0] == NULL) {
  1594. return DECODE_SLICE_OK;
  1595. } else {
  1596. *pict= *(AVFrame*)&s->last_picture;
  1597. }
  1598. }
  1599. return DECODE_SLICE_EOP;
  1600. } else {
  1601. return DECODE_SLICE_OK;
  1602. }
  1603. }
  1604. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1605. UINT8 *buf, int buf_size)
  1606. {
  1607. Mpeg1Context *s1 = avctx->priv_data;
  1608. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1609. int width, height, i, v, j;
  1610. float aspect;
  1611. init_get_bits(&s->gb, buf, buf_size*8);
  1612. width = get_bits(&s->gb, 12);
  1613. height = get_bits(&s->gb, 12);
  1614. s->aspect_ratio_info= get_bits(&s->gb, 4);
  1615. if(!s->mpeg2){
  1616. aspect= mpeg1_aspect[s->aspect_ratio_info];
  1617. if(aspect!=0.0) avctx->aspect_ratio= width/(aspect*height);
  1618. }
  1619. s->frame_rate_index = get_bits(&s->gb, 4);
  1620. if (s->frame_rate_index == 0)
  1621. return -1;
  1622. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1623. if (get_bits1(&s->gb) == 0) /* marker */
  1624. return -1;
  1625. if (width <= 0 || height <= 0 ||
  1626. (width % 2) != 0 || (height % 2) != 0)
  1627. return -1;
  1628. if (width != s->width ||
  1629. height != s->height) {
  1630. /* start new mpeg1 context decoding */
  1631. s->out_format = FMT_MPEG1;
  1632. if (s1->mpeg_enc_ctx_allocated) {
  1633. MPV_common_end(s);
  1634. }
  1635. s->width = width;
  1636. s->height = height;
  1637. avctx->has_b_frames= 1;
  1638. s->avctx = avctx;
  1639. avctx->width = width;
  1640. avctx->height = height;
  1641. if (s->frame_rate_index >= 9) {
  1642. /* at least give a valid frame rate (some old mpeg1 have this) */
  1643. avctx->frame_rate = 25 * FRAME_RATE_BASE;
  1644. } else {
  1645. avctx->frame_rate = frame_rate_tab[s->frame_rate_index];
  1646. }
  1647. s->frame_rate = avctx->frame_rate;
  1648. avctx->bit_rate = s->bit_rate;
  1649. if (MPV_common_init(s) < 0)
  1650. return -1;
  1651. s1->mpeg_enc_ctx_allocated = 1;
  1652. }
  1653. skip_bits(&s->gb, 10); /* vbv_buffer_size */
  1654. skip_bits(&s->gb, 1);
  1655. /* get matrix */
  1656. if (get_bits1(&s->gb)) {
  1657. for(i=0;i<64;i++) {
  1658. v = get_bits(&s->gb, 8);
  1659. j = s->intra_scantable.permutated[i];
  1660. s->intra_matrix[j] = v;
  1661. s->chroma_intra_matrix[j] = v;
  1662. }
  1663. #ifdef DEBUG
  1664. dprintf("intra matrix present\n");
  1665. for(i=0;i<64;i++)
  1666. dprintf(" %d", s->intra_matrix[s->intra_scantable.permutated[i]]);
  1667. printf("\n");
  1668. #endif
  1669. } else {
  1670. for(i=0;i<64;i++) {
  1671. int j= s->idct_permutation[i];
  1672. v = ff_mpeg1_default_intra_matrix[i];
  1673. s->intra_matrix[j] = v;
  1674. s->chroma_intra_matrix[j] = v;
  1675. }
  1676. }
  1677. if (get_bits1(&s->gb)) {
  1678. for(i=0;i<64;i++) {
  1679. v = get_bits(&s->gb, 8);
  1680. j = s->intra_scantable.permutated[i];
  1681. s->inter_matrix[j] = v;
  1682. s->chroma_inter_matrix[j] = v;
  1683. }
  1684. #ifdef DEBUG
  1685. dprintf("non intra matrix present\n");
  1686. for(i=0;i<64;i++)
  1687. dprintf(" %d", s->inter_matrix[s->intra_scantable.permutated[i]]);
  1688. printf("\n");
  1689. #endif
  1690. } else {
  1691. for(i=0;i<64;i++) {
  1692. int j= s->idct_permutation[i];
  1693. v = ff_mpeg1_default_non_intra_matrix[i];
  1694. s->inter_matrix[j] = v;
  1695. s->chroma_inter_matrix[j] = v;
  1696. }
  1697. }
  1698. /* we set mpeg2 parameters so that it emulates mpeg1 */
  1699. s->progressive_sequence = 1;
  1700. s->progressive_frame = 1;
  1701. s->picture_structure = PICT_FRAME;
  1702. s->frame_pred_frame_dct = 1;
  1703. s->mpeg2 = 0;
  1704. avctx->sub_id = 1; /* indicates mpeg1 */
  1705. return 0;
  1706. }
  1707. /* handle buffering and image synchronisation */
  1708. static int mpeg_decode_frame(AVCodecContext *avctx,
  1709. void *data, int *data_size,
  1710. UINT8 *buf, int buf_size)
  1711. {
  1712. Mpeg1Context *s = avctx->priv_data;
  1713. UINT8 *buf_end, *buf_ptr, *buf_start;
  1714. int len, start_code_found, ret, code, start_code, input_size;
  1715. AVFrame *picture = data;
  1716. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1717. dprintf("fill_buffer\n");
  1718. *data_size = 0;
  1719. /* special case for last picture */
  1720. if (buf_size == 0) {
  1721. if (s2->picture_number > 0) {
  1722. *picture= *(AVFrame*)&s2->next_picture;
  1723. *data_size = sizeof(AVFrame);
  1724. }
  1725. return 0;
  1726. }
  1727. buf_ptr = buf;
  1728. buf_end = buf + buf_size;
  1729. #if 0
  1730. if (s->repeat_field % 2 == 1) {
  1731. s->repeat_field++;
  1732. //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
  1733. // s2->picture_number, s->repeat_field);
  1734. if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
  1735. *data_size = sizeof(AVPicture);
  1736. goto the_end;
  1737. }
  1738. }
  1739. #endif
  1740. while (buf_ptr < buf_end) {
  1741. buf_start = buf_ptr;
  1742. /* find start next code */
  1743. code = find_start_code(&buf_ptr, buf_end, &s->header_state);
  1744. if (code >= 0) {
  1745. start_code_found = 1;
  1746. } else {
  1747. start_code_found = 0;
  1748. }
  1749. /* copy to buffer */
  1750. len = buf_ptr - buf_start;
  1751. if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
  1752. /* data too big : flush */
  1753. s->buf_ptr = s->buffer;
  1754. if (start_code_found)
  1755. s->start_code = code;
  1756. } else {
  1757. memcpy(s->buf_ptr, buf_start, len);
  1758. s->buf_ptr += len;
  1759. if( (!(s2->flags&CODEC_FLAG_TRUNCATED)) && (!start_code_found)
  1760. && s->buf_ptr+4<s->buffer+s->buffer_size){
  1761. start_code_found= 1;
  1762. code= 0x1FF;
  1763. s->header_state=0xFF;
  1764. s->buf_ptr[0]=0;
  1765. s->buf_ptr[1]=0;
  1766. s->buf_ptr[2]=1;
  1767. s->buf_ptr[3]=0xFF;
  1768. s->buf_ptr+=4;
  1769. }
  1770. if (start_code_found) {
  1771. /* prepare data for next start code */
  1772. input_size = s->buf_ptr - s->buffer;
  1773. start_code = s->start_code;
  1774. s->buf_ptr = s->buffer;
  1775. s->start_code = code;
  1776. switch(start_code) {
  1777. case SEQ_START_CODE:
  1778. mpeg1_decode_sequence(avctx, s->buffer,
  1779. input_size);
  1780. break;
  1781. case PICTURE_START_CODE:
  1782. /* we have a complete image : we try to decompress it */
  1783. mpeg1_decode_picture(avctx,
  1784. s->buffer, input_size);
  1785. break;
  1786. case EXT_START_CODE:
  1787. mpeg_decode_extension(avctx,
  1788. s->buffer, input_size);
  1789. break;
  1790. default:
  1791. if (start_code >= SLICE_MIN_START_CODE &&
  1792. start_code <= SLICE_MAX_START_CODE) {
  1793. /* skip b frames if we dont have reference frames */
  1794. if(s2->last_picture.data[0]==NULL && s2->pict_type==B_TYPE) break;
  1795. /* skip b frames if we are in a hurry */
  1796. if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
  1797. /* skip everything if we are in a hurry>=5 */
  1798. if(avctx->hurry_up>=5) break;
  1799. ret = mpeg_decode_slice(avctx, picture,
  1800. start_code, s->buffer, input_size);
  1801. if (ret == DECODE_SLICE_EOP) {
  1802. /* got a picture: exit */
  1803. /* first check if we must repeat the frame */
  1804. avctx->repeat_pict = 0;
  1805. #if 0
  1806. if (s2->progressive_frame && s2->repeat_first_field) {
  1807. //fprintf(stderr,"\nRepeat this frame: %d! pict: %d",avctx->frame_number,s2->picture_number);
  1808. //s2->repeat_first_field = 0;
  1809. //s2->progressive_frame = 0;
  1810. if (++s->repeat_field > 2)
  1811. s->repeat_field = 0;
  1812. avctx->repeat_pict = 1;
  1813. }
  1814. #endif
  1815. if (s2->repeat_first_field) {
  1816. if (s2->progressive_sequence) {
  1817. if (s2->top_field_first)
  1818. avctx->repeat_pict = 4;
  1819. else
  1820. avctx->repeat_pict = 2;
  1821. } else if (s2->progressive_frame) {
  1822. avctx->repeat_pict = 1;
  1823. }
  1824. }
  1825. *data_size = sizeof(AVPicture);
  1826. goto the_end;
  1827. }else if(ret<0){
  1828. fprintf(stderr,"Error while decoding slice\n");
  1829. if(ret==DECODE_SLICE_FATAL_ERROR) return -1;
  1830. }
  1831. }
  1832. break;
  1833. }
  1834. }
  1835. }
  1836. }
  1837. the_end:
  1838. return buf_ptr - buf;
  1839. }
  1840. static int mpeg_decode_end(AVCodecContext *avctx)
  1841. {
  1842. Mpeg1Context *s = avctx->priv_data;
  1843. if (s->mpeg_enc_ctx_allocated)
  1844. MPV_common_end(&s->mpeg_enc_ctx);
  1845. return 0;
  1846. }
  1847. AVCodec mpeg_decoder = {
  1848. "mpegvideo",
  1849. CODEC_TYPE_VIDEO,
  1850. CODEC_ID_MPEG1VIDEO,
  1851. sizeof(Mpeg1Context),
  1852. mpeg_decode_init,
  1853. NULL,
  1854. mpeg_decode_end,
  1855. mpeg_decode_frame,
  1856. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  1857. };