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.

1550 lines
48KB

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