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.

1718 lines
53KB

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