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.

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