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.

1719 lines
53KB

  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. } else {
  778. s->block_last_index[i] = -1;
  779. }
  780. }
  781. } else {
  782. for(i=0;i<6;i++) {
  783. if (cbp & (1 << (5 - i))) {
  784. if (mpeg2_decode_block_non_intra(s, block[i], i) < 0)
  785. return -1;
  786. } else {
  787. s->block_last_index[i] = -1;
  788. }
  789. }
  790. }
  791. } else {
  792. for(i=0;i<6;i++) {
  793. if (cbp & (1 << (5 - i))) {
  794. if (mpeg1_decode_block(s, block[i], i) < 0)
  795. return -1;
  796. } else {
  797. s->block_last_index[i] = -1;
  798. }
  799. }
  800. }
  801. return 0;
  802. }
  803. /* as h263, but only 17 codes */
  804. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
  805. {
  806. int code, sign, val, m, l, shift;
  807. code = get_vlc(&s->gb, &mv_vlc);
  808. if (code < 0) {
  809. return 0xffff;
  810. }
  811. if (code == 0) {
  812. return pred;
  813. }
  814. sign = get_bits1(&s->gb);
  815. shift = fcode - 1;
  816. val = (code - 1) << shift;
  817. if (shift > 0)
  818. val |= get_bits(&s->gb, shift);
  819. val++;
  820. if (sign)
  821. val = -val;
  822. val += pred;
  823. /* modulo decoding */
  824. l = (1 << shift) * 16;
  825. m = 2 * l;
  826. if (val < -l) {
  827. val += m;
  828. } else if (val >= l) {
  829. val -= m;
  830. }
  831. return val;
  832. }
  833. static inline int decode_dc(MpegEncContext *s, int component)
  834. {
  835. int code, diff;
  836. if (component == 0) {
  837. code = get_vlc(&s->gb, &dc_lum_vlc);
  838. } else {
  839. code = get_vlc(&s->gb, &dc_chroma_vlc);
  840. }
  841. if (code < 0)
  842. return 0xffff;
  843. if (code == 0) {
  844. diff = 0;
  845. } else {
  846. diff = get_bits(&s->gb, code);
  847. if ((diff & (1 << (code - 1))) == 0)
  848. diff = (-1 << code) | (diff + 1);
  849. }
  850. return diff;
  851. }
  852. static int mpeg1_decode_block(MpegEncContext *s,
  853. DCTELEM *block,
  854. int n)
  855. {
  856. int level, dc, diff, i, j, run;
  857. int code, component;
  858. RLTable *rl = &rl_mpeg1;
  859. if (s->mb_intra) {
  860. /* DC coef */
  861. component = (n <= 3 ? 0 : n - 4 + 1);
  862. diff = decode_dc(s, component);
  863. if (diff >= 0xffff)
  864. return -1;
  865. dc = s->last_dc[component];
  866. dc += diff;
  867. s->last_dc[component] = dc;
  868. block[0] = dc;
  869. dprintf("dc=%d diff=%d\n", dc, diff);
  870. i = 1;
  871. } else {
  872. int bit_cnt, v;
  873. UINT32 bit_buf;
  874. UINT8 *buf_ptr;
  875. i = 0;
  876. /* special case for the first coef. no need to add a second vlc table */
  877. SAVE_BITS(&s->gb);
  878. SHOW_BITS(&s->gb, v, 2);
  879. if (v & 2) {
  880. run = 0;
  881. level = 1 - ((v & 1) << 1);
  882. FLUSH_BITS(2);
  883. RESTORE_BITS(&s->gb);
  884. goto add_coef;
  885. }
  886. RESTORE_BITS(&s->gb);
  887. }
  888. /* now quantify & encode AC coefs */
  889. for(;;) {
  890. code = get_vlc(&s->gb, &rl->vlc);
  891. if (code < 0) {
  892. return -1;
  893. }
  894. if (code == 112) {
  895. break;
  896. } else if (code == 111) {
  897. /* escape */
  898. run = get_bits(&s->gb, 6);
  899. level = get_bits(&s->gb, 8);
  900. level = (level << 24) >> 24;
  901. if (level == -128) {
  902. level = get_bits(&s->gb, 8) - 256;
  903. } else if (level == 0) {
  904. level = get_bits(&s->gb, 8);
  905. }
  906. } else {
  907. run = rl->table_run[code];
  908. level = rl->table_level[code];
  909. if (get_bits1(&s->gb))
  910. level = -level;
  911. }
  912. i += run;
  913. if (i >= 64)
  914. return -1;
  915. add_coef:
  916. dprintf("%d: run=%d level=%d\n", n, run, level);
  917. j = zigzag_direct[i];
  918. block[j] = level;
  919. i++;
  920. }
  921. s->block_last_index[n] = i-1;
  922. return 0;
  923. }
  924. /* Also does unquantization here, since I will never support mpeg2
  925. encoding */
  926. static int mpeg2_decode_block_non_intra(MpegEncContext *s,
  927. DCTELEM *block,
  928. int n)
  929. {
  930. int level, i, j, run;
  931. int code;
  932. RLTable *rl = &rl_mpeg1;
  933. const UINT8 *scan_table;
  934. const UINT16 *matrix;
  935. int mismatch;
  936. if (s->alternate_scan)
  937. scan_table = ff_alternate_vertical_scan;
  938. else
  939. scan_table = zigzag_direct;
  940. mismatch = 1;
  941. {
  942. int bit_cnt, v;
  943. UINT32 bit_buf;
  944. UINT8 *buf_ptr;
  945. i = 0;
  946. if (n < 4)
  947. matrix = s->inter_matrix;
  948. else
  949. matrix = s->chroma_inter_matrix;
  950. /* special case for the first coef. no need to add a second vlc table */
  951. SAVE_BITS(&s->gb);
  952. SHOW_BITS(&s->gb, v, 2);
  953. if (v & 2) {
  954. run = 0;
  955. level = 1 - ((v & 1) << 1);
  956. FLUSH_BITS(2);
  957. RESTORE_BITS(&s->gb);
  958. goto add_coef;
  959. }
  960. RESTORE_BITS(&s->gb);
  961. }
  962. /* now quantify & encode AC coefs */
  963. for(;;) {
  964. code = get_vlc(&s->gb, &rl->vlc);
  965. if (code < 0)
  966. return -1;
  967. if (code == 112) {
  968. break;
  969. } else if (code == 111) {
  970. /* escape */
  971. run = get_bits(&s->gb, 6);
  972. level = get_bits(&s->gb, 12);
  973. level = (level << 20) >> 20;
  974. } else {
  975. run = rl->table_run[code];
  976. level = rl->table_level[code];
  977. if (get_bits1(&s->gb))
  978. level = -level;
  979. }
  980. i += run;
  981. if (i >= 64)
  982. return -1;
  983. add_coef:
  984. j = scan_table[i];
  985. dprintf("%d: run=%d level=%d\n", n, run, level);
  986. /* XXX: optimize */
  987. if (level > 0) {
  988. level = ((level * 2 + 1) * s->qscale * matrix[j]) >> 5;
  989. } else {
  990. level = ((-level * 2 + 1) * s->qscale * matrix[j]) >> 5;
  991. level = -level;
  992. }
  993. /* XXX: is it really necessary to saturate since the encoder
  994. knows whats going on ? */
  995. mismatch ^= level;
  996. block[j] = level;
  997. i++;
  998. }
  999. block[63] ^= (mismatch & 1);
  1000. s->block_last_index[n] = i;
  1001. return 0;
  1002. }
  1003. static int mpeg2_decode_block_intra(MpegEncContext *s,
  1004. DCTELEM *block,
  1005. int n)
  1006. {
  1007. int level, dc, diff, i, j, run;
  1008. int code, component;
  1009. RLTable *rl;
  1010. const UINT8 *scan_table;
  1011. const UINT16 *matrix;
  1012. int mismatch;
  1013. if (s->alternate_scan)
  1014. scan_table = ff_alternate_vertical_scan;
  1015. else
  1016. scan_table = zigzag_direct;
  1017. /* DC coef */
  1018. component = (n <= 3 ? 0 : n - 4 + 1);
  1019. diff = decode_dc(s, component);
  1020. if (diff >= 0xffff)
  1021. return -1;
  1022. dc = s->last_dc[component];
  1023. dc += diff;
  1024. s->last_dc[component] = dc;
  1025. block[0] = dc << (3 - s->intra_dc_precision);
  1026. dprintf("dc=%d\n", block[0]);
  1027. mismatch = block[0] ^ 1;
  1028. i = 1;
  1029. if (s->intra_vlc_format)
  1030. rl = &rl_mpeg2;
  1031. else
  1032. rl = &rl_mpeg1;
  1033. if (n < 4)
  1034. matrix = s->intra_matrix;
  1035. else
  1036. matrix = s->chroma_intra_matrix;
  1037. /* now quantify & encode AC coefs */
  1038. for(;;) {
  1039. code = get_vlc(&s->gb, &rl->vlc);
  1040. if (code < 0)
  1041. return -1;
  1042. if (code == 112) {
  1043. break;
  1044. } else if (code == 111) {
  1045. /* escape */
  1046. run = get_bits(&s->gb, 6);
  1047. level = get_bits(&s->gb, 12);
  1048. level = (level << 20) >> 20;
  1049. } else {
  1050. run = rl->table_run[code];
  1051. level = rl->table_level[code];
  1052. if (get_bits1(&s->gb))
  1053. level = -level;
  1054. }
  1055. i += run;
  1056. if (i >= 64)
  1057. return -1;
  1058. j = scan_table[i];
  1059. dprintf("%d: run=%d level=%d\n", n, run, level);
  1060. level = (level * s->qscale * matrix[j]) / 16;
  1061. /* XXX: is it really necessary to saturate since the encoder
  1062. knows whats going on ? */
  1063. mismatch ^= level;
  1064. block[j] = level;
  1065. i++;
  1066. }
  1067. block[63] ^= (mismatch & 1);
  1068. s->block_last_index[n] = i;
  1069. return 0;
  1070. }
  1071. /* compressed picture size */
  1072. #define PICTURE_BUFFER_SIZE 100000
  1073. typedef struct Mpeg1Context {
  1074. MpegEncContext mpeg_enc_ctx;
  1075. UINT32 header_state;
  1076. int start_code; /* current start code */
  1077. UINT8 buffer[PICTURE_BUFFER_SIZE];
  1078. UINT8 *buf_ptr;
  1079. int buffer_size;
  1080. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  1081. int repeat_field; /* true if we must repeat the field */
  1082. } Mpeg1Context;
  1083. static int mpeg_decode_init(AVCodecContext *avctx)
  1084. {
  1085. Mpeg1Context *s = avctx->priv_data;
  1086. s->header_state = 0xff;
  1087. s->mpeg_enc_ctx_allocated = 0;
  1088. s->buffer_size = PICTURE_BUFFER_SIZE;
  1089. s->start_code = -1;
  1090. s->buf_ptr = s->buffer;
  1091. s->mpeg_enc_ctx.picture_number = 0;
  1092. s->repeat_field = 0;
  1093. s->mpeg_enc_ctx.codec_id= avctx->codec->id;
  1094. avctx->mbskip_table= s->mpeg_enc_ctx.mbskip_table;
  1095. return 0;
  1096. }
  1097. /* return the 8 bit start code value and update the search
  1098. state. Return -1 if no start code found */
  1099. static int find_start_code(UINT8 **pbuf_ptr, UINT8 *buf_end,
  1100. UINT32 *header_state)
  1101. {
  1102. UINT8 *buf_ptr;
  1103. unsigned int state, v;
  1104. int val;
  1105. state = *header_state;
  1106. buf_ptr = *pbuf_ptr;
  1107. while (buf_ptr < buf_end) {
  1108. v = *buf_ptr++;
  1109. if (state == 0x000001) {
  1110. state = ((state << 8) | v) & 0xffffff;
  1111. val = state;
  1112. goto found;
  1113. }
  1114. state = ((state << 8) | v) & 0xffffff;
  1115. }
  1116. val = -1;
  1117. found:
  1118. *pbuf_ptr = buf_ptr;
  1119. *header_state = state;
  1120. return val;
  1121. }
  1122. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1123. UINT8 *buf, int buf_size)
  1124. {
  1125. Mpeg1Context *s1 = avctx->priv_data;
  1126. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1127. int ref, f_code;
  1128. init_get_bits(&s->gb, buf, buf_size);
  1129. ref = get_bits(&s->gb, 10); /* temporal ref */
  1130. s->pict_type = get_bits(&s->gb, 3);
  1131. dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number);
  1132. skip_bits(&s->gb, 16);
  1133. if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
  1134. s->full_pel[0] = get_bits1(&s->gb);
  1135. f_code = get_bits(&s->gb, 3);
  1136. if (f_code == 0)
  1137. return -1;
  1138. s->mpeg_f_code[0][0] = f_code;
  1139. s->mpeg_f_code[0][1] = f_code;
  1140. }
  1141. if (s->pict_type == B_TYPE) {
  1142. s->full_pel[1] = get_bits1(&s->gb);
  1143. f_code = get_bits(&s->gb, 3);
  1144. if (f_code == 0)
  1145. return -1;
  1146. s->mpeg_f_code[1][0] = f_code;
  1147. s->mpeg_f_code[1][1] = f_code;
  1148. }
  1149. s->y_dc_scale = 8;
  1150. s->c_dc_scale = 8;
  1151. s->first_slice = 1;
  1152. return 0;
  1153. }
  1154. static void mpeg_decode_sequence_extension(MpegEncContext *s)
  1155. {
  1156. int horiz_size_ext, vert_size_ext;
  1157. int bit_rate_ext, vbv_buf_ext, low_delay;
  1158. int frame_rate_ext_n, frame_rate_ext_d;
  1159. skip_bits(&s->gb, 8); /* profil and level */
  1160. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1161. skip_bits(&s->gb, 2); /* chroma_format */
  1162. horiz_size_ext = get_bits(&s->gb, 2);
  1163. vert_size_ext = get_bits(&s->gb, 2);
  1164. s->width |= (horiz_size_ext << 12);
  1165. s->height |= (vert_size_ext << 12);
  1166. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1167. s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400;
  1168. skip_bits1(&s->gb); /* marker */
  1169. vbv_buf_ext = get_bits(&s->gb, 8);
  1170. low_delay = get_bits1(&s->gb);
  1171. frame_rate_ext_n = get_bits(&s->gb, 2);
  1172. frame_rate_ext_d = get_bits(&s->gb, 5);
  1173. if (frame_rate_ext_d >= 1)
  1174. s->frame_rate = (s->frame_rate * frame_rate_ext_n) / frame_rate_ext_d;
  1175. dprintf("sequence extension\n");
  1176. s->mpeg2 = 1;
  1177. s->avctx->sub_id = 2; /* indicates mpeg2 found */
  1178. }
  1179. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1180. {
  1181. int i, v, j;
  1182. dprintf("matrix extension\n");
  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->intra_matrix[j] = v;
  1188. s->chroma_intra_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->inter_matrix[j] = v;
  1196. s->chroma_inter_matrix[j] = v;
  1197. }
  1198. }
  1199. if (get_bits1(&s->gb)) {
  1200. for(i=0;i<64;i++) {
  1201. v = get_bits(&s->gb, 8);
  1202. j = zigzag_direct[i];
  1203. s->chroma_intra_matrix[j] = v;
  1204. }
  1205. }
  1206. if (get_bits1(&s->gb)) {
  1207. for(i=0;i<64;i++) {
  1208. v = get_bits(&s->gb, 8);
  1209. j = zigzag_direct[i];
  1210. s->chroma_inter_matrix[j] = v;
  1211. }
  1212. }
  1213. }
  1214. static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
  1215. {
  1216. s->full_pel[0] = s->full_pel[1] = 0;
  1217. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1218. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1219. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1220. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1221. s->intra_dc_precision = get_bits(&s->gb, 2);
  1222. s->picture_structure = get_bits(&s->gb, 2);
  1223. s->top_field_first = get_bits1(&s->gb);
  1224. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1225. s->concealment_motion_vectors = get_bits1(&s->gb);
  1226. s->q_scale_type = get_bits1(&s->gb);
  1227. s->intra_vlc_format = get_bits1(&s->gb);
  1228. s->alternate_scan = get_bits1(&s->gb);
  1229. s->repeat_first_field = get_bits1(&s->gb);
  1230. s->chroma_420_type = get_bits1(&s->gb);
  1231. s->progressive_frame = get_bits1(&s->gb);
  1232. /* composite display not parsed */
  1233. dprintf("intra_dc_precision=%d\n", s->intra_dc_precision);
  1234. dprintf("picture_structure=%d\n", s->picture_structure);
  1235. dprintf("top field first=%d\n", s->top_field_first);
  1236. dprintf("repeat first field=%d\n", s->repeat_first_field);
  1237. dprintf("conceal=%d\n", s->concealment_motion_vectors);
  1238. dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
  1239. dprintf("alternate_scan=%d\n", s->alternate_scan);
  1240. dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1241. dprintf("progressive_frame=%d\n", s->progressive_frame);
  1242. }
  1243. static void mpeg_decode_extension(AVCodecContext *avctx,
  1244. UINT8 *buf, int buf_size)
  1245. {
  1246. Mpeg1Context *s1 = avctx->priv_data;
  1247. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1248. int ext_type;
  1249. init_get_bits(&s->gb, buf, buf_size);
  1250. ext_type = get_bits(&s->gb, 4);
  1251. switch(ext_type) {
  1252. case 0x1:
  1253. /* sequence ext */
  1254. mpeg_decode_sequence_extension(s);
  1255. break;
  1256. case 0x3:
  1257. /* quant matrix extension */
  1258. mpeg_decode_quant_matrix_extension(s);
  1259. break;
  1260. case 0x8:
  1261. /* picture extension */
  1262. mpeg_decode_picture_coding_extension(s);
  1263. break;
  1264. }
  1265. }
  1266. /* return 1 if end of frame */
  1267. static int mpeg_decode_slice(AVCodecContext *avctx,
  1268. AVPicture *pict,
  1269. int start_code,
  1270. UINT8 *buf, int buf_size)
  1271. {
  1272. Mpeg1Context *s1 = avctx->priv_data;
  1273. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1274. int ret;
  1275. start_code = (start_code - 1) & 0xff;
  1276. if (start_code >= s->mb_height)
  1277. return -1;
  1278. s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
  1279. s->last_dc[1] = s->last_dc[0];
  1280. s->last_dc[2] = s->last_dc[0];
  1281. memset(s->last_mv, 0, sizeof(s->last_mv));
  1282. s->mb_x = -1;
  1283. s->mb_y = start_code;
  1284. s->mb_incr = 0;
  1285. /* start frame decoding */
  1286. if (s->first_slice) {
  1287. s->first_slice = 0;
  1288. MPV_frame_start(s);
  1289. }
  1290. init_get_bits(&s->gb, buf, buf_size);
  1291. s->qscale = get_qscale(s);
  1292. /* extra slice info */
  1293. while (get_bits1(&s->gb) != 0) {
  1294. skip_bits(&s->gb, 8);
  1295. }
  1296. for(;;) {
  1297. clear_blocks(s->block[0]);
  1298. emms_c();
  1299. ret = mpeg_decode_mb(s, s->block);
  1300. dprintf("ret=%d\n", ret);
  1301. if (ret < 0)
  1302. return -1;
  1303. if (ret == 1)
  1304. break;
  1305. MPV_decode_mb(s, s->block);
  1306. }
  1307. emms_c();
  1308. /* end of slice reached */
  1309. if (s->mb_x == (s->mb_width - 1) &&
  1310. s->mb_y == (s->mb_height - 1)) {
  1311. /* end of image */
  1312. UINT8 **picture;
  1313. MPV_frame_end(s);
  1314. /* XXX: incorrect reported qscale for mpeg2 */
  1315. if (s->pict_type == B_TYPE) {
  1316. picture = s->current_picture;
  1317. avctx->quality = s->qscale;
  1318. } else {
  1319. /* latency of 1 frame for I and P frames */
  1320. /* XXX: use another variable than picture_number */
  1321. if (s->picture_number == 0) {
  1322. picture = NULL;
  1323. } else {
  1324. picture = s->last_picture;
  1325. avctx->quality = s->last_qscale;
  1326. }
  1327. s->last_qscale = s->qscale;
  1328. s->picture_number++;
  1329. }
  1330. if (picture) {
  1331. pict->data[0] = picture[0];
  1332. pict->data[1] = picture[1];
  1333. pict->data[2] = picture[2];
  1334. pict->linesize[0] = s->linesize;
  1335. pict->linesize[1] = s->linesize / 2;
  1336. pict->linesize[2] = s->linesize / 2;
  1337. return 1;
  1338. } else {
  1339. return 0;
  1340. }
  1341. } else {
  1342. return 0;
  1343. }
  1344. }
  1345. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1346. UINT8 *buf, int buf_size)
  1347. {
  1348. Mpeg1Context *s1 = avctx->priv_data;
  1349. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1350. int width, height, i, v, j;
  1351. init_get_bits(&s->gb, buf, buf_size);
  1352. width = get_bits(&s->gb, 12);
  1353. height = get_bits(&s->gb, 12);
  1354. skip_bits(&s->gb, 4);
  1355. s->frame_rate_index = get_bits(&s->gb, 4);
  1356. if (s->frame_rate_index == 0)
  1357. return -1;
  1358. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1359. if (get_bits1(&s->gb) == 0) /* marker */
  1360. return -1;
  1361. if (width <= 0 || height <= 0 ||
  1362. (width % 2) != 0 || (height % 2) != 0)
  1363. return -1;
  1364. if (width != s->width ||
  1365. height != s->height) {
  1366. /* start new mpeg1 context decoding */
  1367. s->out_format = FMT_MPEG1;
  1368. if (s1->mpeg_enc_ctx_allocated) {
  1369. MPV_common_end(s);
  1370. }
  1371. s->width = width;
  1372. s->height = height;
  1373. s->has_b_frames = 1;
  1374. s->avctx = avctx;
  1375. avctx->width = width;
  1376. avctx->height = height;
  1377. if (s->frame_rate_index >= 9) {
  1378. /* at least give a valid frame rate (some old mpeg1 have this) */
  1379. avctx->frame_rate = 25 * FRAME_RATE_BASE;
  1380. } else {
  1381. avctx->frame_rate = frame_rate_tab[s->frame_rate_index];
  1382. }
  1383. s->frame_rate = avctx->frame_rate;
  1384. avctx->bit_rate = s->bit_rate;
  1385. if (MPV_common_init(s) < 0)
  1386. return -1;
  1387. mpeg1_init_vlc(s);
  1388. s1->mpeg_enc_ctx_allocated = 1;
  1389. }
  1390. skip_bits(&s->gb, 10); /* vbv_buffer_size */
  1391. skip_bits(&s->gb, 1);
  1392. /* get matrix */
  1393. if (get_bits1(&s->gb)) {
  1394. for(i=0;i<64;i++) {
  1395. v = get_bits(&s->gb, 8);
  1396. j = zigzag_direct[i];
  1397. s->intra_matrix[j] = v;
  1398. s->chroma_intra_matrix[j] = v;
  1399. }
  1400. #ifdef DEBUG
  1401. dprintf("intra matrix present\n");
  1402. for(i=0;i<64;i++)
  1403. dprintf(" %d", s->intra_matrix[zigzag_direct[i]]);
  1404. printf("\n");
  1405. #endif
  1406. } else {
  1407. for(i=0;i<64;i++) {
  1408. v = default_intra_matrix[i];
  1409. s->intra_matrix[i] = v;
  1410. s->chroma_intra_matrix[i] = v;
  1411. }
  1412. }
  1413. if (get_bits1(&s->gb)) {
  1414. for(i=0;i<64;i++) {
  1415. v = get_bits(&s->gb, 8);
  1416. j = zigzag_direct[i];
  1417. s->inter_matrix[j] = v;
  1418. s->chroma_inter_matrix[j] = v;
  1419. }
  1420. #ifdef DEBUG
  1421. dprintf("non intra matrix present\n");
  1422. for(i=0;i<64;i++)
  1423. dprintf(" %d", s->inter_matrix[zigzag_direct[i]]);
  1424. printf("\n");
  1425. #endif
  1426. } else {
  1427. for(i=0;i<64;i++) {
  1428. v = default_non_intra_matrix[i];
  1429. s->inter_matrix[i] = v;
  1430. s->chroma_inter_matrix[i] = v;
  1431. }
  1432. }
  1433. /* we set mpeg2 parameters so that it emulates mpeg1 */
  1434. s->progressive_sequence = 1;
  1435. s->progressive_frame = 1;
  1436. s->picture_structure = PICT_FRAME;
  1437. s->frame_pred_frame_dct = 1;
  1438. s->mpeg2 = 0;
  1439. avctx->sub_id = 1; /* indicates mpeg1 */
  1440. return 0;
  1441. }
  1442. /* handle buffering and image synchronisation */
  1443. static int mpeg_decode_frame(AVCodecContext *avctx,
  1444. void *data, int *data_size,
  1445. UINT8 *buf, int buf_size)
  1446. {
  1447. Mpeg1Context *s = avctx->priv_data;
  1448. UINT8 *buf_end, *buf_ptr, *buf_start;
  1449. int len, start_code_found, ret, code, start_code, input_size;
  1450. AVPicture *picture = data;
  1451. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1452. dprintf("fill_buffer\n");
  1453. *data_size = 0;
  1454. /* special case for last picture */
  1455. if (buf_size == 0) {
  1456. if (s2->picture_number > 0) {
  1457. picture->data[0] = s2->next_picture[0];
  1458. picture->data[1] = s2->next_picture[1];
  1459. picture->data[2] = s2->next_picture[2];
  1460. picture->linesize[0] = s2->linesize;
  1461. picture->linesize[1] = s2->linesize / 2;
  1462. picture->linesize[2] = s2->linesize / 2;
  1463. *data_size = sizeof(AVPicture);
  1464. }
  1465. return 0;
  1466. }
  1467. buf_ptr = buf;
  1468. buf_end = buf + buf_size;
  1469. #if 0
  1470. if (s->repeat_field % 2 == 1) {
  1471. s->repeat_field++;
  1472. //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
  1473. // s2->picture_number, s->repeat_field);
  1474. if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
  1475. *data_size = sizeof(AVPicture);
  1476. goto the_end;
  1477. }
  1478. }
  1479. #endif
  1480. while (buf_ptr < buf_end) {
  1481. buf_start = buf_ptr;
  1482. /* find start next code */
  1483. code = find_start_code(&buf_ptr, buf_end, &s->header_state);
  1484. if (code >= 0) {
  1485. start_code_found = 1;
  1486. } else {
  1487. start_code_found = 0;
  1488. }
  1489. /* copy to buffer */
  1490. len = buf_ptr - buf_start;
  1491. if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
  1492. /* data too big : flush */
  1493. s->buf_ptr = s->buffer;
  1494. if (start_code_found)
  1495. s->start_code = code;
  1496. } else {
  1497. memcpy(s->buf_ptr, buf_start, len);
  1498. s->buf_ptr += len;
  1499. if (start_code_found) {
  1500. /* prepare data for next start code */
  1501. input_size = s->buf_ptr - s->buffer;
  1502. start_code = s->start_code;
  1503. s->buf_ptr = s->buffer;
  1504. s->start_code = code;
  1505. switch(start_code) {
  1506. case SEQ_START_CODE:
  1507. mpeg1_decode_sequence(avctx, s->buffer,
  1508. input_size);
  1509. break;
  1510. case PICTURE_START_CODE:
  1511. /* we have a complete image : we try to decompress it */
  1512. mpeg1_decode_picture(avctx,
  1513. s->buffer, input_size);
  1514. break;
  1515. case EXT_START_CODE:
  1516. mpeg_decode_extension(avctx,
  1517. s->buffer, input_size);
  1518. break;
  1519. default:
  1520. if (start_code >= SLICE_MIN_START_CODE &&
  1521. start_code <= SLICE_MAX_START_CODE) {
  1522. ret = mpeg_decode_slice(avctx, picture,
  1523. start_code, s->buffer, input_size);
  1524. if (ret == 1) {
  1525. /* got a picture: exit */
  1526. /* first check if we must repeat the frame */
  1527. avctx->repeat_pict = 0;
  1528. #if 0
  1529. if (s2->progressive_frame && s2->repeat_first_field) {
  1530. //fprintf(stderr,"\nRepeat this frame: %d! pict: %d",avctx->frame_number,s2->picture_number);
  1531. //s2->repeat_first_field = 0;
  1532. //s2->progressive_frame = 0;
  1533. if (++s->repeat_field > 2)
  1534. s->repeat_field = 0;
  1535. avctx->repeat_pict = 1;
  1536. }
  1537. #endif
  1538. if (s2->repeat_first_field) {
  1539. if (s2->progressive_sequence) {
  1540. if (s2->top_field_first)
  1541. avctx->repeat_pict = 4;
  1542. else
  1543. avctx->repeat_pict = 2;
  1544. } else if (s2->progressive_frame) {
  1545. avctx->repeat_pict = 1;
  1546. }
  1547. }
  1548. *data_size = sizeof(AVPicture);
  1549. goto the_end;
  1550. }
  1551. }
  1552. break;
  1553. }
  1554. }
  1555. }
  1556. }
  1557. the_end:
  1558. return buf_ptr - buf;
  1559. }
  1560. static int mpeg_decode_end(AVCodecContext *avctx)
  1561. {
  1562. Mpeg1Context *s = avctx->priv_data;
  1563. if (s->mpeg_enc_ctx_allocated)
  1564. MPV_common_end(&s->mpeg_enc_ctx);
  1565. return 0;
  1566. }
  1567. AVCodec mpeg_decoder = {
  1568. "mpegvideo",
  1569. CODEC_TYPE_VIDEO,
  1570. CODEC_ID_MPEG1VIDEO,
  1571. sizeof(Mpeg1Context),
  1572. mpeg_decode_init,
  1573. NULL,
  1574. mpeg_decode_end,
  1575. mpeg_decode_frame,
  1576. };