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.

1548 lines
47KB

  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->mv[0][0][0] = 0;
  575. s->mv[0][0][1] = 0;
  576. } else if (mb_type & (MB_FOR | MB_BACK)) {
  577. /* motion vectors */
  578. s->mv_dir = 0;
  579. for(i=0;i<2;i++) {
  580. if (mb_type & (MB_FOR >> i)) {
  581. s->mv_dir |= (MV_DIR_FORWARD >> i);
  582. dprintf("mv_type=%d\n", motion_type);
  583. switch(motion_type) {
  584. case MT_FRAME: /* or MT_16X8 */
  585. if (s->picture_structure == PICT_FRAME) {
  586. /* MT_FRAME */
  587. s->mv_type = MV_TYPE_16X16;
  588. for(k=0;k<2;k++) {
  589. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  590. s->last_mv[i][0][k]);
  591. s->last_mv[i][0][k] = val;
  592. s->last_mv[i][1][k] = val;
  593. /* full_pel: only for mpeg1 */
  594. if (s->full_pel[i])
  595. val = val << 1;
  596. s->mv[i][0][k] = val;
  597. dprintf("mv%d: %d\n", k, val);
  598. }
  599. } else {
  600. /* MT_16X8 */
  601. s->mv_type = MV_TYPE_16X8;
  602. for(j=0;j<2;j++) {
  603. s->field_select[i][j] = get_bits1(&s->gb);
  604. for(k=0;k<2;k++) {
  605. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  606. s->last_mv[i][j][k]);
  607. s->last_mv[i][j][k] = val;
  608. s->mv[i][j][k] = val;
  609. }
  610. }
  611. }
  612. break;
  613. case MT_FIELD:
  614. if (s->picture_structure == PICT_FRAME) {
  615. s->mv_type = MV_TYPE_FIELD;
  616. for(j=0;j<2;j++) {
  617. s->field_select[i][j] = get_bits1(&s->gb);
  618. val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  619. s->last_mv[i][j][0]);
  620. s->last_mv[i][j][0] = val;
  621. s->mv[i][j][0] = val;
  622. dprintf("fmx=%d\n", val);
  623. val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  624. s->last_mv[i][j][1] >> 1);
  625. s->last_mv[i][j][1] = val << 1;
  626. s->mv[i][j][1] = val;
  627. dprintf("fmy=%d\n", val);
  628. }
  629. } else {
  630. s->mv_type = MV_TYPE_16X16;
  631. s->field_select[i][0] = get_bits1(&s->gb);
  632. for(k=0;k<2;k++) {
  633. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  634. s->last_mv[i][0][k]);
  635. s->last_mv[i][0][k] = val;
  636. s->last_mv[i][1][k] = val;
  637. s->mv[i][0][k] = val;
  638. }
  639. }
  640. break;
  641. case MT_DMV:
  642. {
  643. int dmx, dmy, mx, my, m;
  644. mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  645. s->last_mv[i][0][0]);
  646. s->last_mv[i][0][0] = mx;
  647. s->last_mv[i][1][0] = mx;
  648. dmx = get_dmv(s);
  649. my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  650. s->last_mv[i][0][1] >> 1);
  651. dmy = get_dmv(s);
  652. s->mv_type = MV_TYPE_DMV;
  653. /* XXX: totally broken */
  654. if (s->picture_structure == PICT_FRAME) {
  655. s->last_mv[i][0][1] = my << 1;
  656. s->last_mv[i][1][1] = my << 1;
  657. m = s->top_field_first ? 1 : 3;
  658. /* top -> top pred */
  659. s->mv[i][0][0] = mx;
  660. s->mv[i][0][1] = my << 1;
  661. s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  662. s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
  663. m = 4 - m;
  664. s->mv[i][2][0] = mx;
  665. s->mv[i][2][1] = my << 1;
  666. s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  667. s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
  668. } else {
  669. s->last_mv[i][0][1] = my;
  670. s->last_mv[i][1][1] = my;
  671. s->mv[i][0][0] = mx;
  672. s->mv[i][0][1] = my;
  673. s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx;
  674. s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1
  675. /* + 2 * cur_field */;
  676. }
  677. }
  678. break;
  679. }
  680. }
  681. }
  682. }
  683. if ((mb_type & MB_INTRA) && s->concealment_motion_vectors) {
  684. skip_bits1(&s->gb); /* marker */
  685. }
  686. if (mb_type & MB_PAT) {
  687. cbp = get_vlc(&s->gb, &mb_pat_vlc);
  688. if (cbp < 0)
  689. return -1;
  690. cbp++;
  691. }
  692. dprintf("cbp=%x\n", cbp);
  693. if (s->mpeg2) {
  694. if (s->mb_intra) {
  695. for(i=0;i<6;i++) {
  696. if (cbp & (1 << (5 - i))) {
  697. if (mpeg2_decode_block_intra(s, block[i], i) < 0)
  698. return -1;
  699. }
  700. }
  701. } else {
  702. for(i=0;i<6;i++) {
  703. if (cbp & (1 << (5 - i))) {
  704. if (mpeg2_decode_block_non_intra(s, block[i], i) < 0)
  705. return -1;
  706. }
  707. }
  708. }
  709. } else {
  710. for(i=0;i<6;i++) {
  711. if (cbp & (1 << (5 - i))) {
  712. if (mpeg1_decode_block(s, block[i], i) < 0)
  713. return -1;
  714. }
  715. }
  716. }
  717. return 0;
  718. }
  719. /* as h263, but only 17 codes */
  720. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
  721. {
  722. int code, sign, val, m, l, shift;
  723. code = get_vlc(&s->gb, &mv_vlc);
  724. if (code < 0) {
  725. return 0xffff;
  726. }
  727. if (code == 0) {
  728. return pred;
  729. }
  730. sign = get_bits1(&s->gb);
  731. shift = fcode - 1;
  732. val = (code - 1) << shift;
  733. if (shift > 0)
  734. val |= get_bits(&s->gb, shift);
  735. val++;
  736. if (sign)
  737. val = -val;
  738. val += pred;
  739. /* modulo decoding */
  740. l = (1 << shift) * 16;
  741. m = 2 * l;
  742. if (val < -l) {
  743. val += m;
  744. } else if (val >= l) {
  745. val -= m;
  746. }
  747. return val;
  748. }
  749. static inline int decode_dc(MpegEncContext *s, int component)
  750. {
  751. int code, diff;
  752. if (component == 0) {
  753. code = get_vlc(&s->gb, &dc_lum_vlc);
  754. } else {
  755. code = get_vlc(&s->gb, &dc_chroma_vlc);
  756. }
  757. if (code < 0)
  758. return 0xffff;
  759. if (code == 0) {
  760. diff = 0;
  761. } else {
  762. diff = get_bits(&s->gb, code);
  763. if ((diff & (1 << (code - 1))) == 0)
  764. diff = (-1 << code) | (diff + 1);
  765. }
  766. return diff;
  767. }
  768. static int mpeg1_decode_block(MpegEncContext *s,
  769. DCTELEM *block,
  770. int n)
  771. {
  772. int level, dc, diff, i, j, run;
  773. int code, component;
  774. RLTable *rl = &rl_mpeg1;
  775. if (s->mb_intra) {
  776. /* DC coef */
  777. component = (n <= 3 ? 0 : n - 4 + 1);
  778. diff = decode_dc(s, component);
  779. if (diff >= 0xffff)
  780. return -1;
  781. dc = s->last_dc[component];
  782. dc += diff;
  783. s->last_dc[component] = dc;
  784. block[0] = dc;
  785. dprintf("dc=%d diff=%d\n", dc, diff);
  786. i = 1;
  787. } else {
  788. int bit_cnt, v;
  789. UINT32 bit_buf;
  790. UINT8 *buf_ptr;
  791. i = 0;
  792. /* special case for the first coef. no need to add a second vlc table */
  793. SAVE_BITS(&s->gb);
  794. SHOW_BITS(&s->gb, v, 2);
  795. if (v & 2) {
  796. run = 0;
  797. level = 1 - ((v & 1) << 1);
  798. FLUSH_BITS(2);
  799. RESTORE_BITS(&s->gb);
  800. goto add_coef;
  801. }
  802. RESTORE_BITS(&s->gb);
  803. }
  804. /* now quantify & encode AC coefs */
  805. for(;;) {
  806. code = get_vlc(&s->gb, &rl->vlc);
  807. if (code < 0) {
  808. return -1;
  809. }
  810. if (code == 112) {
  811. break;
  812. } else if (code == 111) {
  813. /* escape */
  814. run = get_bits(&s->gb, 6);
  815. level = get_bits(&s->gb, 8);
  816. level = (level << 24) >> 24;
  817. if (level == -128) {
  818. level = get_bits(&s->gb, 8) - 256;
  819. } else if (level == 0) {
  820. level = get_bits(&s->gb, 8);
  821. }
  822. } else {
  823. run = rl->table_run[code];
  824. level = rl->table_level[code];
  825. if (get_bits1(&s->gb))
  826. level = -level;
  827. }
  828. i += run;
  829. if (i >= 64)
  830. return -1;
  831. add_coef:
  832. dprintf("%d: run=%d level=%d\n", n, run, level);
  833. j = zigzag_direct[i];
  834. block[j] = level;
  835. i++;
  836. }
  837. s->block_last_index[n] = i;
  838. return 0;
  839. }
  840. /* Also does unquantization here, since I will never support mpeg2
  841. encoding */
  842. static int mpeg2_decode_block_non_intra(MpegEncContext *s,
  843. DCTELEM *block,
  844. int n)
  845. {
  846. int level, i, j, run;
  847. int code;
  848. RLTable *rl = &rl_mpeg1;
  849. const UINT8 *scan_table;
  850. const UINT16 *matrix;
  851. int mismatch;
  852. if (s->alternate_scan)
  853. scan_table = ff_alternate_vertical_scan;
  854. else
  855. scan_table = zigzag_direct;
  856. mismatch = 1;
  857. {
  858. int bit_cnt, v;
  859. UINT32 bit_buf;
  860. UINT8 *buf_ptr;
  861. i = 0;
  862. if (n < 4)
  863. matrix = s->non_intra_matrix;
  864. else
  865. matrix = s->chroma_non_intra_matrix;
  866. /* special case for the first coef. no need to add a second vlc table */
  867. SAVE_BITS(&s->gb);
  868. SHOW_BITS(&s->gb, v, 2);
  869. if (v & 2) {
  870. run = 0;
  871. level = 1 - ((v & 1) << 1);
  872. FLUSH_BITS(2);
  873. RESTORE_BITS(&s->gb);
  874. goto add_coef;
  875. }
  876. RESTORE_BITS(&s->gb);
  877. }
  878. /* now quantify & encode AC coefs */
  879. for(;;) {
  880. code = get_vlc(&s->gb, &rl->vlc);
  881. if (code < 0)
  882. return -1;
  883. if (code == 112) {
  884. break;
  885. } else if (code == 111) {
  886. /* escape */
  887. run = get_bits(&s->gb, 6);
  888. level = get_bits(&s->gb, 12);
  889. level = (level << 20) >> 20;
  890. } else {
  891. run = rl->table_run[code];
  892. level = rl->table_level[code];
  893. if (get_bits1(&s->gb))
  894. level = -level;
  895. }
  896. i += run;
  897. if (i >= 64)
  898. return -1;
  899. add_coef:
  900. j = scan_table[i];
  901. dprintf("%d: run=%d level=%d\n", n, run, level);
  902. level = ((level * 2 + 1) * s->qscale * matrix[j]) / 32;
  903. /* XXX: is it really necessary to saturate since the encoder
  904. knows whats going on ? */
  905. mismatch ^= level;
  906. block[j] = level;
  907. i++;
  908. }
  909. block[63] ^= (mismatch & 1);
  910. s->block_last_index[n] = i;
  911. return 0;
  912. }
  913. static int mpeg2_decode_block_intra(MpegEncContext *s,
  914. DCTELEM *block,
  915. int n)
  916. {
  917. int level, dc, diff, i, j, run;
  918. int code, component;
  919. RLTable *rl;
  920. const UINT8 *scan_table;
  921. const UINT16 *matrix;
  922. int mismatch;
  923. if (s->alternate_scan)
  924. scan_table = ff_alternate_vertical_scan;
  925. else
  926. scan_table = zigzag_direct;
  927. mismatch = 1;
  928. /* DC coef */
  929. component = (n <= 3 ? 0 : n - 4 + 1);
  930. diff = decode_dc(s, component);
  931. if (diff >= 0xffff)
  932. return -1;
  933. dc = s->last_dc[component];
  934. dc += diff;
  935. s->last_dc[component] = dc;
  936. block[0] = dc << (3 - s->intra_dc_precision);
  937. dprintf("dc=%d\n", block[0]);
  938. i = 1;
  939. if (s->intra_vlc_format)
  940. rl = &rl_mpeg2;
  941. else
  942. rl = &rl_mpeg1;
  943. if (n < 4)
  944. matrix = s->intra_matrix;
  945. else
  946. matrix = s->chroma_intra_matrix;
  947. /* now quantify & encode AC coefs */
  948. for(;;) {
  949. code = get_vlc(&s->gb, &rl->vlc);
  950. if (code < 0)
  951. return -1;
  952. if (code == 112) {
  953. break;
  954. } else if (code == 111) {
  955. /* escape */
  956. run = get_bits(&s->gb, 6);
  957. level = get_bits(&s->gb, 12);
  958. level = (level << 20) >> 20;
  959. } else {
  960. run = rl->table_run[code];
  961. level = rl->table_level[code];
  962. if (get_bits1(&s->gb))
  963. level = -level;
  964. }
  965. i += run;
  966. if (i >= 64)
  967. return -1;
  968. j = scan_table[i];
  969. dprintf("%d: run=%d level=%d\n", n, run, level);
  970. level = (level * s->qscale * matrix[j]) / 16;
  971. /* XXX: is it really necessary to saturate since the encoder
  972. knows whats going on ? */
  973. mismatch ^= level;
  974. block[j] = level;
  975. i++;
  976. }
  977. block[63] ^= (mismatch & 1);
  978. s->block_last_index[n] = i;
  979. return 0;
  980. }
  981. /* compressed picture size */
  982. #define PICTURE_BUFFER_SIZE 100000
  983. typedef struct Mpeg1Context {
  984. MpegEncContext mpeg_enc_ctx;
  985. UINT32 header_state;
  986. int start_code; /* current start code */
  987. UINT8 buffer[PICTURE_BUFFER_SIZE];
  988. UINT8 *buf_ptr;
  989. int buffer_size;
  990. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  991. } Mpeg1Context;
  992. static int mpeg_decode_init(AVCodecContext *avctx)
  993. {
  994. Mpeg1Context *s = avctx->priv_data;
  995. s->header_state = 0xff;
  996. s->mpeg_enc_ctx_allocated = 0;
  997. s->buffer_size = PICTURE_BUFFER_SIZE;
  998. s->start_code = -1;
  999. s->buf_ptr = s->buffer;
  1000. s->mpeg_enc_ctx.picture_number = 0;
  1001. return 0;
  1002. }
  1003. /* return the 8 bit start code value and update the search
  1004. state. Return -1 if no start code found */
  1005. static int find_start_code(UINT8 **pbuf_ptr, UINT8 *buf_end,
  1006. UINT32 *header_state)
  1007. {
  1008. UINT8 *buf_ptr;
  1009. unsigned int state, v;
  1010. int val;
  1011. state = *header_state;
  1012. buf_ptr = *pbuf_ptr;
  1013. while (buf_ptr < buf_end) {
  1014. v = *buf_ptr++;
  1015. if (state == 0x000001) {
  1016. state = ((state << 8) | v) & 0xffffff;
  1017. val = state;
  1018. goto found;
  1019. }
  1020. state = ((state << 8) | v) & 0xffffff;
  1021. }
  1022. val = -1;
  1023. found:
  1024. *pbuf_ptr = buf_ptr;
  1025. *header_state = state;
  1026. return val;
  1027. }
  1028. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1029. UINT8 *buf, int buf_size)
  1030. {
  1031. Mpeg1Context *s1 = avctx->priv_data;
  1032. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1033. int ref, f_code;
  1034. init_get_bits(&s->gb, buf, buf_size);
  1035. ref = get_bits(&s->gb, 10); /* temporal ref */
  1036. s->pict_type = get_bits(&s->gb, 3);
  1037. dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number);
  1038. skip_bits(&s->gb, 16);
  1039. if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
  1040. s->full_pel[0] = get_bits1(&s->gb);
  1041. f_code = get_bits(&s->gb, 3);
  1042. if (f_code == 0)
  1043. return -1;
  1044. s->mpeg_f_code[0][0] = f_code;
  1045. s->mpeg_f_code[0][1] = f_code;
  1046. }
  1047. if (s->pict_type == B_TYPE) {
  1048. s->full_pel[1] = get_bits1(&s->gb);
  1049. f_code = get_bits(&s->gb, 3);
  1050. if (f_code == 0)
  1051. return -1;
  1052. s->mpeg_f_code[1][0] = f_code;
  1053. s->mpeg_f_code[1][1] = f_code;
  1054. }
  1055. s->y_dc_scale = 8;
  1056. s->c_dc_scale = 8;
  1057. s->first_slice = 1;
  1058. return 0;
  1059. }
  1060. static void mpeg_decode_sequence_extension(MpegEncContext *s)
  1061. {
  1062. int horiz_size_ext, vert_size_ext;
  1063. int bit_rate_ext, vbv_buf_ext, low_delay;
  1064. int frame_rate_ext_n, frame_rate_ext_d;
  1065. skip_bits(&s->gb, 8); /* profil and level */
  1066. skip_bits(&s->gb, 1); /* progressive_sequence */
  1067. skip_bits(&s->gb, 2); /* chroma_format */
  1068. horiz_size_ext = get_bits(&s->gb, 2);
  1069. vert_size_ext = get_bits(&s->gb, 2);
  1070. s->width |= (horiz_size_ext << 12);
  1071. s->height |= (vert_size_ext << 12);
  1072. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1073. s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400;
  1074. skip_bits1(&s->gb); /* marker */
  1075. vbv_buf_ext = get_bits(&s->gb, 8);
  1076. low_delay = get_bits1(&s->gb);
  1077. frame_rate_ext_n = get_bits(&s->gb, 2);
  1078. frame_rate_ext_d = get_bits(&s->gb, 5);
  1079. if (frame_rate_ext_d >= 1)
  1080. s->frame_rate = (s->frame_rate * frame_rate_ext_n) / frame_rate_ext_d;
  1081. dprintf("sequence extension\n");
  1082. s->mpeg2 = 1;
  1083. }
  1084. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1085. {
  1086. int i, v, j;
  1087. if (get_bits1(&s->gb)) {
  1088. for(i=0;i<64;i++) {
  1089. v = get_bits(&s->gb, 8);
  1090. j = block_permute_op(i);
  1091. s->intra_matrix[j] = v;
  1092. s->chroma_intra_matrix[j] = v;
  1093. }
  1094. }
  1095. if (get_bits1(&s->gb)) {
  1096. for(i=0;i<64;i++) {
  1097. v = get_bits(&s->gb, 8);
  1098. j = block_permute_op(i);
  1099. s->non_intra_matrix[j] = v;
  1100. s->chroma_non_intra_matrix[j] = v;
  1101. }
  1102. }
  1103. if (get_bits1(&s->gb)) {
  1104. for(i=0;i<64;i++) {
  1105. v = get_bits(&s->gb, 8);
  1106. j = block_permute_op(i);
  1107. s->chroma_intra_matrix[j] = v;
  1108. }
  1109. }
  1110. if (get_bits1(&s->gb)) {
  1111. for(i=0;i<64;i++) {
  1112. v = get_bits(&s->gb, 8);
  1113. j = block_permute_op(i);
  1114. s->chroma_non_intra_matrix[j] = v;
  1115. }
  1116. }
  1117. }
  1118. static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
  1119. {
  1120. s->full_pel[0] = s->full_pel[1] = 0;
  1121. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1122. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1123. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1124. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1125. s->intra_dc_precision = get_bits(&s->gb, 2);
  1126. s->picture_structure = get_bits(&s->gb, 2);
  1127. s->top_field_first = get_bits1(&s->gb);
  1128. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1129. s->concealment_motion_vectors = get_bits1(&s->gb);
  1130. s->q_scale_type = get_bits1(&s->gb);
  1131. s->intra_vlc_format = get_bits1(&s->gb);
  1132. s->alternate_scan = get_bits1(&s->gb);
  1133. s->repeat_first_field = get_bits1(&s->gb);
  1134. s->chroma_420_type = get_bits1(&s->gb);
  1135. s->progressive_frame = get_bits1(&s->gb);
  1136. /* composite display not parsed */
  1137. dprintf("dc_preci=%d\n", s->intra_dc_precision);
  1138. dprintf("pict_structure=%d\n", s->picture_structure);
  1139. dprintf("conceal=%d\n", s->concealment_motion_vectors);
  1140. dprintf("intrafmt=%d\n", s->intra_vlc_format);
  1141. dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1142. }
  1143. static void mpeg_decode_extension(AVCodecContext *avctx,
  1144. UINT8 *buf, int buf_size)
  1145. {
  1146. Mpeg1Context *s1 = avctx->priv_data;
  1147. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1148. int ext_type;
  1149. init_get_bits(&s->gb, buf, buf_size);
  1150. ext_type = get_bits(&s->gb, 4);
  1151. switch(ext_type) {
  1152. case 0x1:
  1153. /* sequence ext */
  1154. mpeg_decode_sequence_extension(s);
  1155. break;
  1156. case 0x3:
  1157. /* quant matrix extension */
  1158. mpeg_decode_quant_matrix_extension(s);
  1159. break;
  1160. case 0x8:
  1161. /* picture extension */
  1162. mpeg_decode_picture_coding_extension(s);
  1163. break;
  1164. }
  1165. }
  1166. /* return 1 if end of frame */
  1167. static int mpeg_decode_slice(AVCodecContext *avctx,
  1168. AVPicture *pict,
  1169. int start_code,
  1170. UINT8 *buf, int buf_size)
  1171. {
  1172. Mpeg1Context *s1 = avctx->priv_data;
  1173. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1174. int ret;
  1175. start_code = (start_code - 1) & 0xff;
  1176. if (start_code >= s->mb_height)
  1177. return -1;
  1178. s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
  1179. s->last_dc[1] = s->last_dc[0];
  1180. s->last_dc[2] = s->last_dc[0];
  1181. memset(s->last_mv, 0, sizeof(s->last_mv));
  1182. s->mb_x = -1;
  1183. s->mb_y = start_code;
  1184. s->mb_incr = 0;
  1185. /* start frame decoding */
  1186. if (s->first_slice) {
  1187. s->first_slice = 0;
  1188. MPV_frame_start(s);
  1189. }
  1190. init_get_bits(&s->gb, buf, buf_size);
  1191. s->qscale = get_qscale(s);
  1192. /* extra slice info */
  1193. while (get_bits1(&s->gb) != 0) {
  1194. skip_bits(&s->gb, 8);
  1195. }
  1196. for(;;) {
  1197. memset(s->block, 0, sizeof(s->block));
  1198. ret = mpeg_decode_mb(s, s->block);
  1199. dprintf("ret=%d\n", ret);
  1200. if (ret < 0)
  1201. return -1;
  1202. if (ret == 1)
  1203. break;
  1204. MPV_decode_mb(s, s->block);
  1205. }
  1206. /* end of slice reached */
  1207. if (s->mb_x == (s->mb_width - 1) &&
  1208. s->mb_y == (s->mb_height - 1)) {
  1209. /* end of image */
  1210. UINT8 **picture;
  1211. MPV_frame_end(s);
  1212. /* XXX: incorrect reported qscale for mpeg2 */
  1213. if (s->pict_type == B_TYPE) {
  1214. picture = s->current_picture;
  1215. avctx->quality = s->qscale;
  1216. } else {
  1217. /* latency of 1 frame for I and P frames */
  1218. /* XXX: use another variable than picture_number */
  1219. if (s->picture_number == 0) {
  1220. picture = NULL;
  1221. } else {
  1222. picture = s->last_picture;
  1223. avctx->quality = s->last_qscale;
  1224. }
  1225. s->last_qscale = s->qscale;
  1226. s->picture_number++;
  1227. }
  1228. if (picture) {
  1229. pict->data[0] = picture[0];
  1230. pict->data[1] = picture[1];
  1231. pict->data[2] = picture[2];
  1232. pict->linesize[0] = s->linesize;
  1233. pict->linesize[1] = s->linesize / 2;
  1234. pict->linesize[2] = s->linesize / 2;
  1235. return 1;
  1236. } else {
  1237. return 0;
  1238. }
  1239. } else {
  1240. return 0;
  1241. }
  1242. }
  1243. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1244. UINT8 *buf, int buf_size)
  1245. {
  1246. Mpeg1Context *s1 = avctx->priv_data;
  1247. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1248. int width, height, i, v, j;
  1249. init_get_bits(&s->gb, buf, buf_size);
  1250. width = get_bits(&s->gb, 12);
  1251. height = get_bits(&s->gb, 12);
  1252. skip_bits(&s->gb, 4);
  1253. s->frame_rate_index = get_bits(&s->gb, 4);
  1254. if (s->frame_rate_index == 0)
  1255. return -1;
  1256. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1257. if (get_bits1(&s->gb) == 0) /* marker */
  1258. return -1;
  1259. if (width <= 0 || height <= 0 ||
  1260. (width % 2) != 0 || (height % 2) != 0)
  1261. return -1;
  1262. if (width != s->width ||
  1263. height != s->height) {
  1264. /* start new mpeg1 context decoding */
  1265. s->out_format = FMT_MPEG1;
  1266. if (s1->mpeg_enc_ctx_allocated) {
  1267. MPV_common_end(s);
  1268. }
  1269. s->width = width;
  1270. s->height = height;
  1271. s->has_b_frames = 1;
  1272. avctx->width = width;
  1273. avctx->height = height;
  1274. avctx->frame_rate = frame_rate_tab[s->frame_rate_index];
  1275. avctx->bit_rate = s->bit_rate;
  1276. if (MPV_common_init(s) < 0)
  1277. return -1;
  1278. mpeg1_init_vlc(s);
  1279. s1->mpeg_enc_ctx_allocated = 1;
  1280. }
  1281. skip_bits(&s->gb, 10); /* vbv_buffer_size */
  1282. skip_bits(&s->gb, 1);
  1283. /* get matrix */
  1284. if (get_bits1(&s->gb)) {
  1285. for(i=0;i<64;i++) {
  1286. v = get_bits(&s->gb, 8);
  1287. j = block_permute_op(i);
  1288. s->intra_matrix[j] = v;
  1289. s->chroma_intra_matrix[j] = v;
  1290. }
  1291. } else {
  1292. for(i=0;i<64;i++) {
  1293. v = default_intra_matrix[i];
  1294. s->intra_matrix[i] = v;
  1295. s->chroma_intra_matrix[i] = v;
  1296. }
  1297. }
  1298. if (get_bits1(&s->gb)) {
  1299. for(i=0;i<64;i++) {
  1300. v = get_bits(&s->gb, 8);
  1301. j = block_permute_op(i);
  1302. s->non_intra_matrix[j] = v;
  1303. s->chroma_non_intra_matrix[j] = v;
  1304. }
  1305. } else {
  1306. for(i=0;i<64;i++) {
  1307. v = default_non_intra_matrix[i];
  1308. s->non_intra_matrix[i] = v;
  1309. s->chroma_non_intra_matrix[i] = v;
  1310. }
  1311. }
  1312. /* we set mpeg2 parameters so that it emulates mpeg1 */
  1313. s->progressive_sequence = 1;
  1314. s->progressive_frame = 1;
  1315. s->picture_structure = PICT_FRAME;
  1316. s->frame_pred_frame_dct = 1;
  1317. s->mpeg2 = 0;
  1318. return 0;
  1319. }
  1320. /* handle buffering and image synchronisation */
  1321. static int mpeg_decode_frame(AVCodecContext *avctx,
  1322. void *data, int *data_size,
  1323. UINT8 *buf, int buf_size)
  1324. {
  1325. Mpeg1Context *s = avctx->priv_data;
  1326. UINT8 *buf_end, *buf_ptr, *buf_start;
  1327. int len, start_code_found, ret, code, start_code, input_size;
  1328. AVPicture *picture = data;
  1329. dprintf("fill_buffer\n");
  1330. *data_size = 0;
  1331. /* special case for last picture */
  1332. if (buf_size == 0) {
  1333. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1334. if (s2->picture_number > 0) {
  1335. picture->data[0] = s2->next_picture[0];
  1336. picture->data[1] = s2->next_picture[1];
  1337. picture->data[2] = s2->next_picture[2];
  1338. picture->linesize[0] = s2->linesize;
  1339. picture->linesize[1] = s2->linesize / 2;
  1340. picture->linesize[2] = s2->linesize / 2;
  1341. *data_size = sizeof(AVPicture);
  1342. }
  1343. return 0;
  1344. }
  1345. buf_ptr = buf;
  1346. buf_end = buf + buf_size;
  1347. while (buf_ptr < buf_end) {
  1348. buf_start = buf_ptr;
  1349. /* find start next code */
  1350. code = find_start_code(&buf_ptr, buf_end, &s->header_state);
  1351. if (code >= 0) {
  1352. start_code_found = 1;
  1353. } else {
  1354. start_code_found = 0;
  1355. }
  1356. /* copy to buffer */
  1357. len = buf_ptr - buf_start;
  1358. if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
  1359. /* data too big : flush */
  1360. s->buf_ptr = s->buffer;
  1361. if (start_code_found)
  1362. s->start_code = code;
  1363. } else {
  1364. memcpy(s->buf_ptr, buf_start, len);
  1365. s->buf_ptr += len;
  1366. if (start_code_found) {
  1367. /* prepare data for next start code */
  1368. input_size = s->buf_ptr - s->buffer;
  1369. start_code = s->start_code;
  1370. s->buf_ptr = s->buffer;
  1371. s->start_code = code;
  1372. switch(start_code) {
  1373. case SEQ_START_CODE:
  1374. mpeg1_decode_sequence(avctx, s->buffer,
  1375. input_size);
  1376. break;
  1377. case PICTURE_START_CODE:
  1378. /* we have a complete image : we try to decompress it */
  1379. mpeg1_decode_picture(avctx,
  1380. s->buffer, input_size);
  1381. break;
  1382. case EXT_START_CODE:
  1383. mpeg_decode_extension(avctx,
  1384. s->buffer, input_size);
  1385. break;
  1386. default:
  1387. if (start_code >= SLICE_MIN_START_CODE &&
  1388. start_code <= SLICE_MAX_START_CODE) {
  1389. ret = mpeg_decode_slice(avctx, picture,
  1390. start_code, s->buffer, input_size);
  1391. if (ret == 1) {
  1392. /* got a picture: exit */
  1393. *data_size = sizeof(AVPicture);
  1394. goto the_end;
  1395. }
  1396. }
  1397. break;
  1398. }
  1399. }
  1400. }
  1401. }
  1402. the_end:
  1403. return buf_ptr - buf;
  1404. }
  1405. static int mpeg_decode_end(AVCodecContext *avctx)
  1406. {
  1407. Mpeg1Context *s = avctx->priv_data;
  1408. if (s->mpeg_enc_ctx_allocated)
  1409. MPV_common_end(&s->mpeg_enc_ctx);
  1410. return 0;
  1411. }
  1412. AVCodec mpeg_decoder = {
  1413. "mpegvideo",
  1414. CODEC_TYPE_VIDEO,
  1415. CODEC_ID_MPEG1VIDEO,
  1416. sizeof(Mpeg1Context),
  1417. mpeg_decode_init,
  1418. NULL,
  1419. mpeg_decode_end,
  1420. mpeg_decode_frame,
  1421. };