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.

1681 lines
52KB

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