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.

1724 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 v;
  875. OPEN_READER(re, &s->gb);
  876. i = 0;
  877. /* special case for the first coef. no need to add a second vlc table */
  878. UPDATE_CACHE(re, &s->gb);
  879. v= SHOW_UBITS(re, &s->gb, 2);
  880. if (v & 2) {
  881. run = 0;
  882. level = 1 - ((v & 1) << 1);
  883. SKIP_BITS(re, &s->gb, 2);
  884. CLOSE_READER(re, &s->gb);
  885. goto add_coef;
  886. }
  887. CLOSE_READER(re, &s->gb);
  888. }
  889. /* now quantify & encode AC coefs */
  890. for(;;) {
  891. code = get_vlc(&s->gb, &rl->vlc);
  892. if (code < 0) {
  893. return -1;
  894. }
  895. if (code == 112) {
  896. break;
  897. } else if (code == 111) {
  898. /* escape */
  899. run = get_bits(&s->gb, 6);
  900. level = get_bits(&s->gb, 8);
  901. level = (level << 24) >> 24;
  902. if (level == -128) {
  903. level = get_bits(&s->gb, 8) - 256;
  904. } else if (level == 0) {
  905. level = get_bits(&s->gb, 8);
  906. }
  907. } else {
  908. run = rl->table_run[code];
  909. level = rl->table_level[code];
  910. if (get_bits1(&s->gb))
  911. level = -level;
  912. }
  913. i += run;
  914. if (i >= 64)
  915. return -1;
  916. add_coef:
  917. dprintf("%d: run=%d level=%d\n", n, run, level);
  918. j = zigzag_direct[i];
  919. block[j] = level;
  920. i++;
  921. }
  922. s->block_last_index[n] = i-1;
  923. return 0;
  924. }
  925. /* Also does unquantization here, since I will never support mpeg2
  926. encoding */
  927. static int mpeg2_decode_block_non_intra(MpegEncContext *s,
  928. DCTELEM *block,
  929. int n)
  930. {
  931. int level, i, j, run;
  932. int code;
  933. RLTable *rl = &rl_mpeg1;
  934. const UINT8 *scan_table;
  935. const UINT16 *matrix;
  936. int mismatch;
  937. if (s->alternate_scan)
  938. scan_table = ff_alternate_vertical_scan;
  939. else
  940. scan_table = zigzag_direct;
  941. mismatch = 1;
  942. {
  943. int v;
  944. OPEN_READER(re, &s->gb);
  945. i = 0;
  946. if (n < 4)
  947. matrix = s->inter_matrix;
  948. else
  949. matrix = s->chroma_inter_matrix;
  950. /* special case for the first coef. no need to add a second vlc table */
  951. UPDATE_CACHE(re, &s->gb);
  952. v= SHOW_UBITS(re, &s->gb, 2);
  953. if (v & 2) {
  954. run = 0;
  955. level = 1 - ((v & 1) << 1);
  956. SKIP_BITS(re, &s->gb, 2);
  957. CLOSE_READER(re, &s->gb);
  958. goto add_coef;
  959. }
  960. CLOSE_READER(re, &s->gb);
  961. }
  962. /* now quantify & encode AC coefs */
  963. for(;;) {
  964. code = get_vlc(&s->gb, &rl->vlc);
  965. if (code < 0)
  966. return -1;
  967. if (code == 112) {
  968. break;
  969. } else if (code == 111) {
  970. /* escape */
  971. run = get_bits(&s->gb, 6);
  972. level = get_bits(&s->gb, 12);
  973. level = (level << 20) >> 20;
  974. } else {
  975. run = rl->table_run[code];
  976. level = rl->table_level[code];
  977. if (get_bits1(&s->gb))
  978. level = -level;
  979. }
  980. i += run;
  981. if (i >= 64)
  982. return -1;
  983. add_coef:
  984. j = scan_table[i];
  985. dprintf("%d: run=%d level=%d\n", n, run, level);
  986. /* XXX: optimize */
  987. if (level > 0) {
  988. level = ((level * 2 + 1) * s->qscale * matrix[j]) >> 5;
  989. } else {
  990. level = ((-level * 2 + 1) * s->qscale * matrix[j]) >> 5;
  991. level = -level;
  992. }
  993. /* XXX: is it really necessary to saturate since the encoder
  994. knows whats going on ? */
  995. mismatch ^= level;
  996. block[j] = level;
  997. i++;
  998. }
  999. block[63] ^= (mismatch & 1);
  1000. s->block_last_index[n] = i;
  1001. return 0;
  1002. }
  1003. static int mpeg2_decode_block_intra(MpegEncContext *s,
  1004. DCTELEM *block,
  1005. int n)
  1006. {
  1007. int level, dc, diff, i, j, run;
  1008. int code, component;
  1009. RLTable *rl;
  1010. const UINT8 *scan_table;
  1011. const UINT16 *matrix;
  1012. int mismatch;
  1013. if (s->alternate_scan)
  1014. scan_table = ff_alternate_vertical_scan;
  1015. else
  1016. scan_table = zigzag_direct;
  1017. /* DC coef */
  1018. component = (n <= 3 ? 0 : n - 4 + 1);
  1019. diff = decode_dc(s, component);
  1020. if (diff >= 0xffff)
  1021. return -1;
  1022. dc = s->last_dc[component];
  1023. dc += diff;
  1024. s->last_dc[component] = dc;
  1025. block[0] = dc << (3 - s->intra_dc_precision);
  1026. dprintf("dc=%d\n", block[0]);
  1027. mismatch = block[0] ^ 1;
  1028. i = 1;
  1029. if (s->intra_vlc_format)
  1030. rl = &rl_mpeg2;
  1031. else
  1032. rl = &rl_mpeg1;
  1033. if (n < 4)
  1034. matrix = s->intra_matrix;
  1035. else
  1036. matrix = s->chroma_intra_matrix;
  1037. /* now quantify & encode AC coefs */
  1038. for(;;) {
  1039. code = get_vlc(&s->gb, &rl->vlc);
  1040. if (code < 0)
  1041. return -1;
  1042. if (code == 112) {
  1043. break;
  1044. } else if (code == 111) {
  1045. /* escape */
  1046. run = get_bits(&s->gb, 6);
  1047. level = get_bits(&s->gb, 12);
  1048. level = (level << 20) >> 20;
  1049. } else {
  1050. run = rl->table_run[code];
  1051. level = rl->table_level[code];
  1052. if (get_bits1(&s->gb))
  1053. level = -level;
  1054. }
  1055. i += run;
  1056. if (i >= 64)
  1057. return -1;
  1058. j = scan_table[i];
  1059. dprintf("%d: run=%d level=%d\n", n, run, level);
  1060. level = (level * s->qscale * matrix[j]) / 16;
  1061. /* XXX: is it really necessary to saturate since the encoder
  1062. knows whats going on ? */
  1063. mismatch ^= level;
  1064. block[j] = level;
  1065. i++;
  1066. }
  1067. block[63] ^= (mismatch & 1);
  1068. s->block_last_index[n] = i;
  1069. return 0;
  1070. }
  1071. /* compressed picture size */
  1072. #define PICTURE_BUFFER_SIZE 100000
  1073. typedef struct Mpeg1Context {
  1074. MpegEncContext mpeg_enc_ctx;
  1075. UINT32 header_state;
  1076. int start_code; /* current start code */
  1077. UINT8 buffer[PICTURE_BUFFER_SIZE];
  1078. UINT8 *buf_ptr;
  1079. int buffer_size;
  1080. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  1081. int repeat_field; /* true if we must repeat the field */
  1082. } Mpeg1Context;
  1083. static int mpeg_decode_init(AVCodecContext *avctx)
  1084. {
  1085. Mpeg1Context *s = avctx->priv_data;
  1086. common_init(&s->mpeg_enc_ctx);
  1087. s->header_state = 0xff;
  1088. s->mpeg_enc_ctx_allocated = 0;
  1089. s->buffer_size = PICTURE_BUFFER_SIZE;
  1090. s->start_code = -1;
  1091. s->buf_ptr = s->buffer;
  1092. s->mpeg_enc_ctx.picture_number = 0;
  1093. s->repeat_field = 0;
  1094. s->mpeg_enc_ctx.codec_id= avctx->codec->id;
  1095. avctx->mbskip_table= s->mpeg_enc_ctx.mbskip_table;
  1096. s->mpeg_enc_ctx.flags= avctx->flags;
  1097. return 0;
  1098. }
  1099. /* return the 8 bit start code value and update the search
  1100. state. Return -1 if no start code found */
  1101. static int find_start_code(UINT8 **pbuf_ptr, UINT8 *buf_end,
  1102. UINT32 *header_state)
  1103. {
  1104. UINT8 *buf_ptr;
  1105. unsigned int state, v;
  1106. int val;
  1107. state = *header_state;
  1108. buf_ptr = *pbuf_ptr;
  1109. while (buf_ptr < buf_end) {
  1110. v = *buf_ptr++;
  1111. if (state == 0x000001) {
  1112. state = ((state << 8) | v) & 0xffffff;
  1113. val = state;
  1114. goto found;
  1115. }
  1116. state = ((state << 8) | v) & 0xffffff;
  1117. }
  1118. val = -1;
  1119. found:
  1120. *pbuf_ptr = buf_ptr;
  1121. *header_state = state;
  1122. return val;
  1123. }
  1124. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1125. UINT8 *buf, int buf_size)
  1126. {
  1127. Mpeg1Context *s1 = avctx->priv_data;
  1128. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1129. int ref, f_code;
  1130. init_get_bits(&s->gb, buf, buf_size);
  1131. ref = get_bits(&s->gb, 10); /* temporal ref */
  1132. s->pict_type = get_bits(&s->gb, 3);
  1133. dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number);
  1134. skip_bits(&s->gb, 16);
  1135. if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
  1136. s->full_pel[0] = get_bits1(&s->gb);
  1137. f_code = get_bits(&s->gb, 3);
  1138. if (f_code == 0)
  1139. return -1;
  1140. s->mpeg_f_code[0][0] = f_code;
  1141. s->mpeg_f_code[0][1] = f_code;
  1142. }
  1143. if (s->pict_type == B_TYPE) {
  1144. s->full_pel[1] = get_bits1(&s->gb);
  1145. f_code = get_bits(&s->gb, 3);
  1146. if (f_code == 0)
  1147. return -1;
  1148. s->mpeg_f_code[1][0] = f_code;
  1149. s->mpeg_f_code[1][1] = f_code;
  1150. }
  1151. s->y_dc_scale = 8;
  1152. s->c_dc_scale = 8;
  1153. s->first_slice = 1;
  1154. return 0;
  1155. }
  1156. static void mpeg_decode_sequence_extension(MpegEncContext *s)
  1157. {
  1158. int horiz_size_ext, vert_size_ext;
  1159. int bit_rate_ext, vbv_buf_ext, low_delay;
  1160. int frame_rate_ext_n, frame_rate_ext_d;
  1161. skip_bits(&s->gb, 8); /* profil and level */
  1162. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1163. skip_bits(&s->gb, 2); /* chroma_format */
  1164. horiz_size_ext = get_bits(&s->gb, 2);
  1165. vert_size_ext = get_bits(&s->gb, 2);
  1166. s->width |= (horiz_size_ext << 12);
  1167. s->height |= (vert_size_ext << 12);
  1168. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1169. s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400;
  1170. skip_bits1(&s->gb); /* marker */
  1171. vbv_buf_ext = get_bits(&s->gb, 8);
  1172. low_delay = get_bits1(&s->gb);
  1173. frame_rate_ext_n = get_bits(&s->gb, 2);
  1174. frame_rate_ext_d = get_bits(&s->gb, 5);
  1175. if (frame_rate_ext_d >= 1)
  1176. s->frame_rate = (s->frame_rate * frame_rate_ext_n) / frame_rate_ext_d;
  1177. dprintf("sequence extension\n");
  1178. s->mpeg2 = 1;
  1179. s->avctx->sub_id = 2; /* indicates mpeg2 found */
  1180. }
  1181. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1182. {
  1183. int i, v, j;
  1184. dprintf("matrix extension\n");
  1185. if (get_bits1(&s->gb)) {
  1186. for(i=0;i<64;i++) {
  1187. v = get_bits(&s->gb, 8);
  1188. j = zigzag_direct[i];
  1189. s->intra_matrix[j] = v;
  1190. s->chroma_intra_matrix[j] = v;
  1191. }
  1192. }
  1193. if (get_bits1(&s->gb)) {
  1194. for(i=0;i<64;i++) {
  1195. v = get_bits(&s->gb, 8);
  1196. j = zigzag_direct[i];
  1197. s->inter_matrix[j] = v;
  1198. s->chroma_inter_matrix[j] = v;
  1199. }
  1200. }
  1201. if (get_bits1(&s->gb)) {
  1202. for(i=0;i<64;i++) {
  1203. v = get_bits(&s->gb, 8);
  1204. j = zigzag_direct[i];
  1205. s->chroma_intra_matrix[j] = v;
  1206. }
  1207. }
  1208. if (get_bits1(&s->gb)) {
  1209. for(i=0;i<64;i++) {
  1210. v = get_bits(&s->gb, 8);
  1211. j = zigzag_direct[i];
  1212. s->chroma_inter_matrix[j] = v;
  1213. }
  1214. }
  1215. }
  1216. static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
  1217. {
  1218. s->full_pel[0] = s->full_pel[1] = 0;
  1219. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1220. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1221. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1222. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1223. s->intra_dc_precision = get_bits(&s->gb, 2);
  1224. s->picture_structure = get_bits(&s->gb, 2);
  1225. s->top_field_first = get_bits1(&s->gb);
  1226. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1227. s->concealment_motion_vectors = get_bits1(&s->gb);
  1228. s->q_scale_type = get_bits1(&s->gb);
  1229. s->intra_vlc_format = get_bits1(&s->gb);
  1230. s->alternate_scan = get_bits1(&s->gb);
  1231. s->repeat_first_field = get_bits1(&s->gb);
  1232. s->chroma_420_type = get_bits1(&s->gb);
  1233. s->progressive_frame = get_bits1(&s->gb);
  1234. /* composite display not parsed */
  1235. dprintf("intra_dc_precision=%d\n", s->intra_dc_precision);
  1236. dprintf("picture_structure=%d\n", s->picture_structure);
  1237. dprintf("top field first=%d\n", s->top_field_first);
  1238. dprintf("repeat first field=%d\n", s->repeat_first_field);
  1239. dprintf("conceal=%d\n", s->concealment_motion_vectors);
  1240. dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
  1241. dprintf("alternate_scan=%d\n", s->alternate_scan);
  1242. dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1243. dprintf("progressive_frame=%d\n", s->progressive_frame);
  1244. }
  1245. static void mpeg_decode_extension(AVCodecContext *avctx,
  1246. UINT8 *buf, int buf_size)
  1247. {
  1248. Mpeg1Context *s1 = avctx->priv_data;
  1249. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1250. int ext_type;
  1251. init_get_bits(&s->gb, buf, buf_size);
  1252. ext_type = get_bits(&s->gb, 4);
  1253. switch(ext_type) {
  1254. case 0x1:
  1255. /* sequence ext */
  1256. mpeg_decode_sequence_extension(s);
  1257. break;
  1258. case 0x3:
  1259. /* quant matrix extension */
  1260. mpeg_decode_quant_matrix_extension(s);
  1261. break;
  1262. case 0x8:
  1263. /* picture extension */
  1264. mpeg_decode_picture_coding_extension(s);
  1265. break;
  1266. }
  1267. }
  1268. /* return 1 if end of frame */
  1269. static int mpeg_decode_slice(AVCodecContext *avctx,
  1270. AVPicture *pict,
  1271. int start_code,
  1272. UINT8 *buf, int buf_size)
  1273. {
  1274. Mpeg1Context *s1 = avctx->priv_data;
  1275. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1276. int ret;
  1277. start_code = (start_code - 1) & 0xff;
  1278. if (start_code >= s->mb_height)
  1279. return -1;
  1280. s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
  1281. s->last_dc[1] = s->last_dc[0];
  1282. s->last_dc[2] = s->last_dc[0];
  1283. memset(s->last_mv, 0, sizeof(s->last_mv));
  1284. s->mb_x = -1;
  1285. s->mb_y = start_code;
  1286. s->mb_incr = 0;
  1287. /* start frame decoding */
  1288. if (s->first_slice) {
  1289. s->first_slice = 0;
  1290. MPV_frame_start(s);
  1291. }
  1292. init_get_bits(&s->gb, buf, buf_size);
  1293. s->qscale = get_qscale(s);
  1294. /* extra slice info */
  1295. while (get_bits1(&s->gb) != 0) {
  1296. skip_bits(&s->gb, 8);
  1297. }
  1298. for(;;) {
  1299. clear_blocks(s->block[0]);
  1300. emms_c();
  1301. ret = mpeg_decode_mb(s, s->block);
  1302. dprintf("ret=%d\n", ret);
  1303. if (ret < 0)
  1304. return -1;
  1305. if (ret == 1)
  1306. break;
  1307. MPV_decode_mb(s, s->block);
  1308. }
  1309. emms_c();
  1310. /* end of slice reached */
  1311. if (s->mb_x == (s->mb_width - 1) &&
  1312. s->mb_y == (s->mb_height - 1)) {
  1313. /* end of image */
  1314. UINT8 **picture;
  1315. MPV_frame_end(s);
  1316. /* XXX: incorrect reported qscale for mpeg2 */
  1317. if (s->pict_type == B_TYPE) {
  1318. picture = s->current_picture;
  1319. avctx->quality = s->qscale;
  1320. } else {
  1321. /* latency of 1 frame for I and P frames */
  1322. /* XXX: use another variable than picture_number */
  1323. if (s->picture_number == 0) {
  1324. picture = NULL;
  1325. } else {
  1326. picture = s->last_picture;
  1327. avctx->quality = s->last_qscale;
  1328. }
  1329. s->last_qscale = s->qscale;
  1330. s->picture_number++;
  1331. }
  1332. if (picture) {
  1333. pict->data[0] = picture[0];
  1334. pict->data[1] = picture[1];
  1335. pict->data[2] = picture[2];
  1336. pict->linesize[0] = s->linesize;
  1337. pict->linesize[1] = s->linesize / 2;
  1338. pict->linesize[2] = s->linesize / 2;
  1339. return 1;
  1340. } else {
  1341. return 0;
  1342. }
  1343. } else {
  1344. return 0;
  1345. }
  1346. }
  1347. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1348. UINT8 *buf, int buf_size)
  1349. {
  1350. Mpeg1Context *s1 = avctx->priv_data;
  1351. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1352. int width, height, i, v, j;
  1353. init_get_bits(&s->gb, buf, buf_size);
  1354. width = get_bits(&s->gb, 12);
  1355. height = get_bits(&s->gb, 12);
  1356. skip_bits(&s->gb, 4);
  1357. s->frame_rate_index = get_bits(&s->gb, 4);
  1358. if (s->frame_rate_index == 0)
  1359. return -1;
  1360. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1361. if (get_bits1(&s->gb) == 0) /* marker */
  1362. return -1;
  1363. if (width <= 0 || height <= 0 ||
  1364. (width % 2) != 0 || (height % 2) != 0)
  1365. return -1;
  1366. if (width != s->width ||
  1367. height != s->height) {
  1368. /* start new mpeg1 context decoding */
  1369. s->out_format = FMT_MPEG1;
  1370. if (s1->mpeg_enc_ctx_allocated) {
  1371. MPV_common_end(s);
  1372. }
  1373. s->width = width;
  1374. s->height = height;
  1375. s->has_b_frames = 1;
  1376. s->avctx = avctx;
  1377. avctx->width = width;
  1378. avctx->height = height;
  1379. if (s->frame_rate_index >= 9) {
  1380. /* at least give a valid frame rate (some old mpeg1 have this) */
  1381. avctx->frame_rate = 25 * FRAME_RATE_BASE;
  1382. } else {
  1383. avctx->frame_rate = frame_rate_tab[s->frame_rate_index];
  1384. }
  1385. s->frame_rate = avctx->frame_rate;
  1386. avctx->bit_rate = s->bit_rate;
  1387. if (MPV_common_init(s) < 0)
  1388. return -1;
  1389. mpeg1_init_vlc(s);
  1390. s1->mpeg_enc_ctx_allocated = 1;
  1391. }
  1392. skip_bits(&s->gb, 10); /* vbv_buffer_size */
  1393. skip_bits(&s->gb, 1);
  1394. /* get matrix */
  1395. if (get_bits1(&s->gb)) {
  1396. for(i=0;i<64;i++) {
  1397. v = get_bits(&s->gb, 8);
  1398. j = zigzag_direct[i];
  1399. s->intra_matrix[j] = v;
  1400. s->chroma_intra_matrix[j] = v;
  1401. }
  1402. #ifdef DEBUG
  1403. dprintf("intra matrix present\n");
  1404. for(i=0;i<64;i++)
  1405. dprintf(" %d", s->intra_matrix[zigzag_direct[i]]);
  1406. printf("\n");
  1407. #endif
  1408. } else {
  1409. for(i=0;i<64;i++) {
  1410. v = default_intra_matrix[i];
  1411. s->intra_matrix[i] = v;
  1412. s->chroma_intra_matrix[i] = v;
  1413. }
  1414. }
  1415. if (get_bits1(&s->gb)) {
  1416. for(i=0;i<64;i++) {
  1417. v = get_bits(&s->gb, 8);
  1418. j = zigzag_direct[i];
  1419. s->inter_matrix[j] = v;
  1420. s->chroma_inter_matrix[j] = v;
  1421. }
  1422. #ifdef DEBUG
  1423. dprintf("non intra matrix present\n");
  1424. for(i=0;i<64;i++)
  1425. dprintf(" %d", s->inter_matrix[zigzag_direct[i]]);
  1426. printf("\n");
  1427. #endif
  1428. } else {
  1429. for(i=0;i<64;i++) {
  1430. v = default_non_intra_matrix[i];
  1431. s->inter_matrix[i] = v;
  1432. s->chroma_inter_matrix[i] = v;
  1433. }
  1434. }
  1435. /* we set mpeg2 parameters so that it emulates mpeg1 */
  1436. s->progressive_sequence = 1;
  1437. s->progressive_frame = 1;
  1438. s->picture_structure = PICT_FRAME;
  1439. s->frame_pred_frame_dct = 1;
  1440. s->mpeg2 = 0;
  1441. avctx->sub_id = 1; /* indicates mpeg1 */
  1442. return 0;
  1443. }
  1444. /* handle buffering and image synchronisation */
  1445. static int mpeg_decode_frame(AVCodecContext *avctx,
  1446. void *data, int *data_size,
  1447. UINT8 *buf, int buf_size)
  1448. {
  1449. Mpeg1Context *s = avctx->priv_data;
  1450. UINT8 *buf_end, *buf_ptr, *buf_start;
  1451. int len, start_code_found, ret, code, start_code, input_size;
  1452. AVPicture *picture = data;
  1453. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1454. dprintf("fill_buffer\n");
  1455. *data_size = 0;
  1456. /* special case for last picture */
  1457. if (buf_size == 0) {
  1458. if (s2->picture_number > 0) {
  1459. picture->data[0] = s2->next_picture[0];
  1460. picture->data[1] = s2->next_picture[1];
  1461. picture->data[2] = s2->next_picture[2];
  1462. picture->linesize[0] = s2->linesize;
  1463. picture->linesize[1] = s2->linesize / 2;
  1464. picture->linesize[2] = s2->linesize / 2;
  1465. *data_size = sizeof(AVPicture);
  1466. }
  1467. return 0;
  1468. }
  1469. buf_ptr = buf;
  1470. buf_end = buf + buf_size;
  1471. #if 0
  1472. if (s->repeat_field % 2 == 1) {
  1473. s->repeat_field++;
  1474. //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
  1475. // s2->picture_number, s->repeat_field);
  1476. if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
  1477. *data_size = sizeof(AVPicture);
  1478. goto the_end;
  1479. }
  1480. }
  1481. #endif
  1482. while (buf_ptr < buf_end) {
  1483. buf_start = buf_ptr;
  1484. /* find start next code */
  1485. code = find_start_code(&buf_ptr, buf_end, &s->header_state);
  1486. if (code >= 0) {
  1487. start_code_found = 1;
  1488. } else {
  1489. start_code_found = 0;
  1490. }
  1491. /* copy to buffer */
  1492. len = buf_ptr - buf_start;
  1493. if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
  1494. /* data too big : flush */
  1495. s->buf_ptr = s->buffer;
  1496. if (start_code_found)
  1497. s->start_code = code;
  1498. } else {
  1499. memcpy(s->buf_ptr, buf_start, len);
  1500. s->buf_ptr += len;
  1501. if (start_code_found) {
  1502. /* prepare data for next start code */
  1503. input_size = s->buf_ptr - s->buffer;
  1504. start_code = s->start_code;
  1505. s->buf_ptr = s->buffer;
  1506. s->start_code = code;
  1507. switch(start_code) {
  1508. case SEQ_START_CODE:
  1509. mpeg1_decode_sequence(avctx, s->buffer,
  1510. input_size);
  1511. break;
  1512. case PICTURE_START_CODE:
  1513. /* we have a complete image : we try to decompress it */
  1514. mpeg1_decode_picture(avctx,
  1515. s->buffer, input_size);
  1516. break;
  1517. case EXT_START_CODE:
  1518. mpeg_decode_extension(avctx,
  1519. s->buffer, input_size);
  1520. break;
  1521. default:
  1522. if (start_code >= SLICE_MIN_START_CODE &&
  1523. start_code <= SLICE_MAX_START_CODE) {
  1524. ret = mpeg_decode_slice(avctx, picture,
  1525. start_code, s->buffer, input_size);
  1526. if (ret == 1) {
  1527. /* got a picture: exit */
  1528. /* first check if we must repeat the frame */
  1529. avctx->repeat_pict = 0;
  1530. #if 0
  1531. if (s2->progressive_frame && s2->repeat_first_field) {
  1532. //fprintf(stderr,"\nRepeat this frame: %d! pict: %d",avctx->frame_number,s2->picture_number);
  1533. //s2->repeat_first_field = 0;
  1534. //s2->progressive_frame = 0;
  1535. if (++s->repeat_field > 2)
  1536. s->repeat_field = 0;
  1537. avctx->repeat_pict = 1;
  1538. }
  1539. #endif
  1540. if (s2->repeat_first_field) {
  1541. if (s2->progressive_sequence) {
  1542. if (s2->top_field_first)
  1543. avctx->repeat_pict = 4;
  1544. else
  1545. avctx->repeat_pict = 2;
  1546. } else if (s2->progressive_frame) {
  1547. avctx->repeat_pict = 1;
  1548. }
  1549. }
  1550. *data_size = sizeof(AVPicture);
  1551. goto the_end;
  1552. }
  1553. }
  1554. break;
  1555. }
  1556. }
  1557. }
  1558. }
  1559. the_end:
  1560. return buf_ptr - buf;
  1561. }
  1562. static int mpeg_decode_end(AVCodecContext *avctx)
  1563. {
  1564. Mpeg1Context *s = avctx->priv_data;
  1565. if (s->mpeg_enc_ctx_allocated)
  1566. MPV_common_end(&s->mpeg_enc_ctx);
  1567. return 0;
  1568. }
  1569. AVCodec mpeg_decoder = {
  1570. "mpegvideo",
  1571. CODEC_TYPE_VIDEO,
  1572. CODEC_ID_MPEG1VIDEO,
  1573. sizeof(Mpeg1Context),
  1574. mpeg_decode_init,
  1575. NULL,
  1576. mpeg_decode_end,
  1577. mpeg_decode_frame,
  1578. };