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.

1686 lines
52KB

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