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.

1732 lines
54KB

  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. #define DC_VLC_BITS 9
  470. #define MV_VLC_BITS 9
  471. #define MBINCR_VLC_BITS 9
  472. #define MB_PAT_VLC_BITS 9
  473. #define MB_PTYPE_VLC_BITS 6
  474. #define MB_BTYPE_VLC_BITS 6
  475. #define TEX_VLC_BITS 9
  476. void mpeg1_init_vlc(MpegEncContext *s)
  477. {
  478. static int done = 0;
  479. if (!done) {
  480. done = 1;
  481. init_vlc(&dc_lum_vlc, DC_VLC_BITS, 10/*12*/,
  482. vlc_dc_lum_bits, 1, 1,
  483. vlc_dc_lum_code, 2, 2);
  484. init_vlc(&dc_chroma_vlc, DC_VLC_BITS, 10/*12*/,
  485. vlc_dc_chroma_bits, 1, 1,
  486. vlc_dc_chroma_code, 2, 2);
  487. init_vlc(&mv_vlc, MV_VLC_BITS, 17,
  488. &mbMotionVectorTable[0][1], 2, 1,
  489. &mbMotionVectorTable[0][0], 2, 1);
  490. init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 35,
  491. &mbAddrIncrTable[0][1], 2, 1,
  492. &mbAddrIncrTable[0][0], 2, 1);
  493. init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 63,
  494. &mbPatTable[0][1], 2, 1,
  495. &mbPatTable[0][0], 2, 1);
  496. init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 32,
  497. &table_mb_ptype[0][1], 2, 1,
  498. &table_mb_ptype[0][0], 2, 1);
  499. init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 32,
  500. &table_mb_btype[0][1], 2, 1,
  501. &table_mb_btype[0][0], 2, 1);
  502. init_rl(&rl_mpeg1);
  503. init_rl(&rl_mpeg2);
  504. /* cannot use generic init because we must add the EOB code */
  505. init_vlc(&rl_mpeg1.vlc, TEX_VLC_BITS, rl_mpeg1.n + 2,
  506. &rl_mpeg1.table_vlc[0][1], 4, 2,
  507. &rl_mpeg1.table_vlc[0][0], 4, 2);
  508. init_vlc(&rl_mpeg2.vlc, TEX_VLC_BITS, rl_mpeg2.n + 2,
  509. &rl_mpeg2.table_vlc[0][1], 4, 2,
  510. &rl_mpeg2.table_vlc[0][0], 4, 2);
  511. }
  512. }
  513. static inline int get_dmv(MpegEncContext *s)
  514. {
  515. if(get_bits1(&s->gb))
  516. return 1 - (get_bits1(&s->gb) << 1);
  517. else
  518. return 0;
  519. }
  520. static inline int get_qscale(MpegEncContext *s)
  521. {
  522. int qscale;
  523. if (s->mpeg2) {
  524. if (s->q_scale_type) {
  525. qscale = non_linear_qscale[get_bits(&s->gb, 5)];
  526. } else {
  527. qscale = get_bits(&s->gb, 5) << 1;
  528. }
  529. } else {
  530. /* for mpeg1, we use the generic unquant code */
  531. qscale = get_bits(&s->gb, 5);
  532. }
  533. return qscale;
  534. }
  535. /* motion type (for mpeg2) */
  536. #define MT_FIELD 1
  537. #define MT_FRAME 2
  538. #define MT_16X8 2
  539. #define MT_DMV 3
  540. static int mpeg_decode_mb(MpegEncContext *s,
  541. DCTELEM block[6][64])
  542. {
  543. int i, j, k, cbp, val, code, mb_type, motion_type;
  544. /* skip mb handling */
  545. if (s->mb_incr == 0) {
  546. /* read again increment */
  547. s->mb_incr = 1;
  548. for(;;) {
  549. code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  550. if (code < 0)
  551. return 1; /* error = end of slice */
  552. if (code >= 33) {
  553. if (code == 33) {
  554. s->mb_incr += 33;
  555. }
  556. /* otherwise, stuffing, nothing to do */
  557. } else {
  558. s->mb_incr += code;
  559. break;
  560. }
  561. }
  562. }
  563. if (++s->mb_x >= s->mb_width) {
  564. s->mb_x = 0;
  565. if (s->mb_y >= (s->mb_height - 1))
  566. return -1;
  567. s->mb_y++;
  568. }
  569. dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
  570. if (--s->mb_incr != 0) {
  571. /* skip mb */
  572. s->mb_intra = 0;
  573. for(i=0;i<6;i++)
  574. s->block_last_index[i] = -1;
  575. s->mv_type = MV_TYPE_16X16;
  576. if (s->pict_type == P_TYPE) {
  577. /* if P type, zero motion vector is implied */
  578. s->mv_dir = MV_DIR_FORWARD;
  579. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  580. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  581. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  582. } else {
  583. /* if B type, reuse previous vectors and directions */
  584. s->mv[0][0][0] = s->last_mv[0][0][0];
  585. s->mv[0][0][1] = s->last_mv[0][0][1];
  586. s->mv[1][0][0] = s->last_mv[1][0][0];
  587. s->mv[1][0][1] = s->last_mv[1][0][1];
  588. }
  589. s->mb_skiped = 1;
  590. return 0;
  591. }
  592. switch(s->pict_type) {
  593. default:
  594. case I_TYPE:
  595. if (get_bits1(&s->gb) == 0) {
  596. if (get_bits1(&s->gb) == 0)
  597. return -1;
  598. mb_type = MB_QUANT | MB_INTRA;
  599. } else {
  600. mb_type = MB_INTRA;
  601. }
  602. break;
  603. case P_TYPE:
  604. mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
  605. if (mb_type < 0)
  606. return -1;
  607. break;
  608. case B_TYPE:
  609. mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
  610. if (mb_type < 0)
  611. return -1;
  612. break;
  613. }
  614. dprintf("mb_type=%x\n", mb_type);
  615. motion_type = 0; /* avoid warning */
  616. if (mb_type & (MB_FOR|MB_BACK)) {
  617. /* get additionnal motion vector type */
  618. if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct)
  619. motion_type = MT_FRAME;
  620. else
  621. motion_type = get_bits(&s->gb, 2);
  622. }
  623. /* compute dct type */
  624. if (s->picture_structure == PICT_FRAME &&
  625. !s->frame_pred_frame_dct &&
  626. (mb_type & (MB_PAT | MB_INTRA))) {
  627. s->interlaced_dct = get_bits1(&s->gb);
  628. #ifdef DEBUG
  629. if (s->interlaced_dct)
  630. printf("interlaced_dct\n");
  631. #endif
  632. } else {
  633. s->interlaced_dct = 0; /* frame based */
  634. }
  635. if (mb_type & MB_QUANT) {
  636. s->qscale = get_qscale(s);
  637. }
  638. if (mb_type & MB_INTRA) {
  639. if (s->concealment_motion_vectors) {
  640. /* just parse them */
  641. if (s->picture_structure != PICT_FRAME)
  642. skip_bits1(&s->gb); /* field select */
  643. mpeg_decode_motion(s, s->mpeg_f_code[0][0], 0);
  644. mpeg_decode_motion(s, s->mpeg_f_code[0][1], 0);
  645. }
  646. s->mb_intra = 1;
  647. cbp = 0x3f;
  648. memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
  649. } else {
  650. s->mb_intra = 0;
  651. cbp = 0;
  652. }
  653. /* special case of implicit zero motion vector */
  654. if (s->pict_type == P_TYPE && !(mb_type & MB_FOR)) {
  655. s->mv_dir = MV_DIR_FORWARD;
  656. s->mv_type = MV_TYPE_16X16;
  657. s->last_mv[0][0][0] = 0;
  658. s->last_mv[0][0][1] = 0;
  659. s->last_mv[0][1][0] = 0;
  660. s->last_mv[0][1][1] = 0;
  661. s->mv[0][0][0] = 0;
  662. s->mv[0][0][1] = 0;
  663. } else if (mb_type & (MB_FOR | MB_BACK)) {
  664. /* motion vectors */
  665. s->mv_dir = 0;
  666. for(i=0;i<2;i++) {
  667. if (mb_type & (MB_FOR >> i)) {
  668. s->mv_dir |= (MV_DIR_FORWARD >> i);
  669. dprintf("motion_type=%d\n", motion_type);
  670. switch(motion_type) {
  671. case MT_FRAME: /* or MT_16X8 */
  672. if (s->picture_structure == PICT_FRAME) {
  673. /* MT_FRAME */
  674. s->mv_type = MV_TYPE_16X16;
  675. for(k=0;k<2;k++) {
  676. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  677. s->last_mv[i][0][k]);
  678. s->last_mv[i][0][k] = val;
  679. s->last_mv[i][1][k] = val;
  680. /* full_pel: only for mpeg1 */
  681. if (s->full_pel[i])
  682. val = val << 1;
  683. s->mv[i][0][k] = val;
  684. dprintf("mv%d: %d\n", k, val);
  685. }
  686. } else {
  687. /* MT_16X8 */
  688. s->mv_type = MV_TYPE_16X8;
  689. for(j=0;j<2;j++) {
  690. s->field_select[i][j] = get_bits1(&s->gb);
  691. for(k=0;k<2;k++) {
  692. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  693. s->last_mv[i][j][k]);
  694. s->last_mv[i][j][k] = val;
  695. s->mv[i][j][k] = val;
  696. }
  697. }
  698. }
  699. break;
  700. case MT_FIELD:
  701. if (s->picture_structure == PICT_FRAME) {
  702. s->mv_type = MV_TYPE_FIELD;
  703. for(j=0;j<2;j++) {
  704. s->field_select[i][j] = get_bits1(&s->gb);
  705. val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  706. s->last_mv[i][j][0]);
  707. s->last_mv[i][j][0] = val;
  708. s->mv[i][j][0] = val;
  709. dprintf("fmx=%d\n", val);
  710. val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  711. s->last_mv[i][j][1] >> 1);
  712. s->last_mv[i][j][1] = val << 1;
  713. s->mv[i][j][1] = val;
  714. dprintf("fmy=%d\n", val);
  715. }
  716. } else {
  717. s->mv_type = MV_TYPE_16X16;
  718. s->field_select[i][0] = get_bits1(&s->gb);
  719. for(k=0;k<2;k++) {
  720. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  721. s->last_mv[i][0][k]);
  722. s->last_mv[i][0][k] = val;
  723. s->last_mv[i][1][k] = val;
  724. s->mv[i][0][k] = val;
  725. }
  726. }
  727. break;
  728. case MT_DMV:
  729. {
  730. int dmx, dmy, mx, my, m;
  731. mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  732. s->last_mv[i][0][0]);
  733. s->last_mv[i][0][0] = mx;
  734. s->last_mv[i][1][0] = mx;
  735. dmx = get_dmv(s);
  736. my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  737. s->last_mv[i][0][1] >> 1);
  738. dmy = get_dmv(s);
  739. s->mv_type = MV_TYPE_DMV;
  740. /* XXX: totally broken */
  741. if (s->picture_structure == PICT_FRAME) {
  742. s->last_mv[i][0][1] = my << 1;
  743. s->last_mv[i][1][1] = my << 1;
  744. m = s->top_field_first ? 1 : 3;
  745. /* top -> top pred */
  746. s->mv[i][0][0] = mx;
  747. s->mv[i][0][1] = my << 1;
  748. s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  749. s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
  750. m = 4 - m;
  751. s->mv[i][2][0] = mx;
  752. s->mv[i][2][1] = my << 1;
  753. s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  754. s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
  755. } else {
  756. s->last_mv[i][0][1] = my;
  757. s->last_mv[i][1][1] = my;
  758. s->mv[i][0][0] = mx;
  759. s->mv[i][0][1] = my;
  760. s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx;
  761. s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1
  762. /* + 2 * cur_field */;
  763. }
  764. }
  765. break;
  766. }
  767. }
  768. }
  769. }
  770. if ((mb_type & MB_INTRA) && s->concealment_motion_vectors) {
  771. skip_bits1(&s->gb); /* marker */
  772. }
  773. if (mb_type & MB_PAT) {
  774. cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
  775. if (cbp < 0)
  776. return -1;
  777. cbp++;
  778. }
  779. dprintf("cbp=%x\n", cbp);
  780. if (s->mpeg2) {
  781. if (s->mb_intra) {
  782. for(i=0;i<6;i++) {
  783. if (cbp & (1 << (5 - i))) {
  784. if (mpeg2_decode_block_intra(s, block[i], i) < 0)
  785. return -1;
  786. } else {
  787. s->block_last_index[i] = -1;
  788. }
  789. }
  790. } else {
  791. for(i=0;i<6;i++) {
  792. if (cbp & (1 << (5 - i))) {
  793. if (mpeg2_decode_block_non_intra(s, block[i], i) < 0)
  794. return -1;
  795. } else {
  796. s->block_last_index[i] = -1;
  797. }
  798. }
  799. }
  800. } else {
  801. for(i=0;i<6;i++) {
  802. if (cbp & (1 << (5 - i))) {
  803. if (mpeg1_decode_block(s, block[i], i) < 0)
  804. return -1;
  805. } else {
  806. s->block_last_index[i] = -1;
  807. }
  808. }
  809. }
  810. return 0;
  811. }
  812. /* as h263, but only 17 codes */
  813. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
  814. {
  815. int code, sign, val, m, l, shift;
  816. code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
  817. if (code < 0) {
  818. return 0xffff;
  819. }
  820. if (code == 0) {
  821. return pred;
  822. }
  823. sign = get_bits1(&s->gb);
  824. shift = fcode - 1;
  825. val = (code - 1) << shift;
  826. if (shift > 0)
  827. val |= get_bits(&s->gb, shift);
  828. val++;
  829. if (sign)
  830. val = -val;
  831. val += pred;
  832. /* modulo decoding */
  833. l = (1 << shift) * 16;
  834. m = 2 * l;
  835. if (val < -l) {
  836. val += m;
  837. } else if (val >= l) {
  838. val -= m;
  839. }
  840. return val;
  841. }
  842. static inline int decode_dc(MpegEncContext *s, int component)
  843. {
  844. int code, diff;
  845. if (component == 0) {
  846. code = get_vlc2(&s->gb, dc_lum_vlc.table, DC_VLC_BITS, 1);
  847. } else {
  848. code = get_vlc2(&s->gb, dc_chroma_vlc.table, DC_VLC_BITS, 1);
  849. }
  850. if (code < 0)
  851. return 0xffff;
  852. if (code == 0) {
  853. diff = 0;
  854. } else {
  855. diff = get_bits(&s->gb, code);
  856. if ((diff & (1 << (code - 1))) == 0)
  857. diff = (-1 << code) | (diff + 1);
  858. }
  859. return diff;
  860. }
  861. static int mpeg1_decode_block(MpegEncContext *s,
  862. DCTELEM *block,
  863. int n)
  864. {
  865. int level, dc, diff, i, j, run;
  866. int code, component;
  867. RLTable *rl = &rl_mpeg1;
  868. if (s->mb_intra) {
  869. /* DC coef */
  870. component = (n <= 3 ? 0 : n - 4 + 1);
  871. diff = decode_dc(s, component);
  872. if (diff >= 0xffff)
  873. return -1;
  874. dc = s->last_dc[component];
  875. dc += diff;
  876. s->last_dc[component] = dc;
  877. block[0] = dc;
  878. dprintf("dc=%d diff=%d\n", dc, diff);
  879. i = 1;
  880. } else {
  881. int v;
  882. OPEN_READER(re, &s->gb);
  883. i = 0;
  884. /* special case for the first coef. no need to add a second vlc table */
  885. UPDATE_CACHE(re, &s->gb);
  886. v= SHOW_UBITS(re, &s->gb, 2);
  887. if (v & 2) {
  888. run = 0;
  889. level = 1 - ((v & 1) << 1);
  890. SKIP_BITS(re, &s->gb, 2);
  891. CLOSE_READER(re, &s->gb);
  892. goto add_coef;
  893. }
  894. CLOSE_READER(re, &s->gb);
  895. }
  896. /* now quantify & encode AC coefs */
  897. for(;;) {
  898. code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
  899. if (code < 0) {
  900. return -1;
  901. }
  902. if (code == 112) {
  903. break;
  904. } else if (code == 111) {
  905. /* escape */
  906. run = get_bits(&s->gb, 6);
  907. level = get_bits(&s->gb, 8);
  908. level = (level << 24) >> 24;
  909. if (level == -128) {
  910. level = get_bits(&s->gb, 8) - 256;
  911. } else if (level == 0) {
  912. level = get_bits(&s->gb, 8);
  913. }
  914. } else {
  915. run = rl->table_run[code];
  916. level = rl->table_level[code];
  917. if (get_bits1(&s->gb))
  918. level = -level;
  919. }
  920. i += run;
  921. if (i >= 64)
  922. return -1;
  923. add_coef:
  924. dprintf("%d: run=%d level=%d\n", n, run, level);
  925. j = zigzag_direct[i];
  926. block[j] = level;
  927. i++;
  928. }
  929. s->block_last_index[n] = i-1;
  930. return 0;
  931. }
  932. /* Also does unquantization here, since I will never support mpeg2
  933. encoding */
  934. static int mpeg2_decode_block_non_intra(MpegEncContext *s,
  935. DCTELEM *block,
  936. int n)
  937. {
  938. int level, i, j, run;
  939. int code;
  940. RLTable *rl = &rl_mpeg1;
  941. const UINT8 *scan_table;
  942. const UINT16 *matrix;
  943. int mismatch;
  944. if (s->alternate_scan)
  945. scan_table = ff_alternate_vertical_scan;
  946. else
  947. scan_table = zigzag_direct;
  948. mismatch = 1;
  949. {
  950. int v;
  951. OPEN_READER(re, &s->gb);
  952. i = 0;
  953. if (n < 4)
  954. matrix = s->inter_matrix;
  955. else
  956. matrix = s->chroma_inter_matrix;
  957. /* special case for the first coef. no need to add a second vlc table */
  958. UPDATE_CACHE(re, &s->gb);
  959. v= SHOW_UBITS(re, &s->gb, 2);
  960. if (v & 2) {
  961. run = 0;
  962. level = 1 - ((v & 1) << 1);
  963. SKIP_BITS(re, &s->gb, 2);
  964. CLOSE_READER(re, &s->gb);
  965. goto add_coef;
  966. }
  967. CLOSE_READER(re, &s->gb);
  968. }
  969. /* now quantify & encode AC coefs */
  970. for(;;) {
  971. code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
  972. if (code < 0)
  973. return -1;
  974. if (code == 112) {
  975. break;
  976. } else if (code == 111) {
  977. /* escape */
  978. run = get_bits(&s->gb, 6);
  979. level = get_bits(&s->gb, 12);
  980. level = (level << 20) >> 20;
  981. } else {
  982. run = rl->table_run[code];
  983. level = rl->table_level[code];
  984. if (get_bits1(&s->gb))
  985. level = -level;
  986. }
  987. i += run;
  988. if (i >= 64)
  989. return -1;
  990. add_coef:
  991. j = scan_table[i];
  992. dprintf("%d: run=%d level=%d\n", n, run, level);
  993. /* XXX: optimize */
  994. if (level > 0) {
  995. level = ((level * 2 + 1) * s->qscale * matrix[j]) >> 5;
  996. } else {
  997. level = ((-level * 2 + 1) * s->qscale * matrix[j]) >> 5;
  998. level = -level;
  999. }
  1000. /* XXX: is it really necessary to saturate since the encoder
  1001. knows whats going on ? */
  1002. mismatch ^= level;
  1003. block[j] = level;
  1004. i++;
  1005. }
  1006. block[63] ^= (mismatch & 1);
  1007. s->block_last_index[n] = i;
  1008. return 0;
  1009. }
  1010. static int mpeg2_decode_block_intra(MpegEncContext *s,
  1011. DCTELEM *block,
  1012. int n)
  1013. {
  1014. int level, dc, diff, i, j, run;
  1015. int code, component;
  1016. RLTable *rl;
  1017. const UINT8 *scan_table;
  1018. const UINT16 *matrix;
  1019. int mismatch;
  1020. if (s->alternate_scan)
  1021. scan_table = ff_alternate_vertical_scan;
  1022. else
  1023. scan_table = zigzag_direct;
  1024. /* DC coef */
  1025. component = (n <= 3 ? 0 : n - 4 + 1);
  1026. diff = decode_dc(s, component);
  1027. if (diff >= 0xffff)
  1028. return -1;
  1029. dc = s->last_dc[component];
  1030. dc += diff;
  1031. s->last_dc[component] = dc;
  1032. block[0] = dc << (3 - s->intra_dc_precision);
  1033. dprintf("dc=%d\n", block[0]);
  1034. mismatch = block[0] ^ 1;
  1035. i = 1;
  1036. if (s->intra_vlc_format)
  1037. rl = &rl_mpeg2;
  1038. else
  1039. rl = &rl_mpeg1;
  1040. if (n < 4)
  1041. matrix = s->intra_matrix;
  1042. else
  1043. matrix = s->chroma_intra_matrix;
  1044. /* now quantify & encode AC coefs */
  1045. for(;;) {
  1046. code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
  1047. if (code < 0)
  1048. return -1;
  1049. if (code == 112) {
  1050. break;
  1051. } else if (code == 111) {
  1052. /* escape */
  1053. run = get_bits(&s->gb, 6);
  1054. level = get_bits(&s->gb, 12);
  1055. level = (level << 20) >> 20;
  1056. } else {
  1057. run = rl->table_run[code];
  1058. level = rl->table_level[code];
  1059. if (get_bits1(&s->gb))
  1060. level = -level;
  1061. }
  1062. i += run;
  1063. if (i >= 64)
  1064. return -1;
  1065. j = scan_table[i];
  1066. dprintf("%d: run=%d level=%d\n", n, run, level);
  1067. level = (level * s->qscale * matrix[j]) / 16;
  1068. /* XXX: is it really necessary to saturate since the encoder
  1069. knows whats going on ? */
  1070. mismatch ^= level;
  1071. block[j] = level;
  1072. i++;
  1073. }
  1074. block[63] ^= (mismatch & 1);
  1075. s->block_last_index[n] = i;
  1076. return 0;
  1077. }
  1078. /* compressed picture size */
  1079. #define PICTURE_BUFFER_SIZE 100000
  1080. typedef struct Mpeg1Context {
  1081. MpegEncContext mpeg_enc_ctx;
  1082. UINT32 header_state;
  1083. int start_code; /* current start code */
  1084. UINT8 buffer[PICTURE_BUFFER_SIZE];
  1085. UINT8 *buf_ptr;
  1086. int buffer_size;
  1087. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  1088. int repeat_field; /* true if we must repeat the field */
  1089. } Mpeg1Context;
  1090. static int mpeg_decode_init(AVCodecContext *avctx)
  1091. {
  1092. Mpeg1Context *s = avctx->priv_data;
  1093. common_init(&s->mpeg_enc_ctx);
  1094. s->header_state = 0xff;
  1095. s->mpeg_enc_ctx_allocated = 0;
  1096. s->buffer_size = PICTURE_BUFFER_SIZE;
  1097. s->start_code = -1;
  1098. s->buf_ptr = s->buffer;
  1099. s->mpeg_enc_ctx.picture_number = 0;
  1100. s->repeat_field = 0;
  1101. s->mpeg_enc_ctx.codec_id= avctx->codec->id;
  1102. avctx->mbskip_table= s->mpeg_enc_ctx.mbskip_table;
  1103. s->mpeg_enc_ctx.flags= avctx->flags;
  1104. return 0;
  1105. }
  1106. /* return the 8 bit start code value and update the search
  1107. state. Return -1 if no start code found */
  1108. static int find_start_code(UINT8 **pbuf_ptr, UINT8 *buf_end,
  1109. UINT32 *header_state)
  1110. {
  1111. UINT8 *buf_ptr;
  1112. unsigned int state, v;
  1113. int val;
  1114. state = *header_state;
  1115. buf_ptr = *pbuf_ptr;
  1116. while (buf_ptr < buf_end) {
  1117. v = *buf_ptr++;
  1118. if (state == 0x000001) {
  1119. state = ((state << 8) | v) & 0xffffff;
  1120. val = state;
  1121. goto found;
  1122. }
  1123. state = ((state << 8) | v) & 0xffffff;
  1124. }
  1125. val = -1;
  1126. found:
  1127. *pbuf_ptr = buf_ptr;
  1128. *header_state = state;
  1129. return val;
  1130. }
  1131. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1132. UINT8 *buf, int buf_size)
  1133. {
  1134. Mpeg1Context *s1 = avctx->priv_data;
  1135. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1136. int ref, f_code;
  1137. init_get_bits(&s->gb, buf, buf_size);
  1138. ref = get_bits(&s->gb, 10); /* temporal ref */
  1139. s->pict_type = get_bits(&s->gb, 3);
  1140. dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number);
  1141. skip_bits(&s->gb, 16);
  1142. if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
  1143. s->full_pel[0] = get_bits1(&s->gb);
  1144. f_code = get_bits(&s->gb, 3);
  1145. if (f_code == 0)
  1146. return -1;
  1147. s->mpeg_f_code[0][0] = f_code;
  1148. s->mpeg_f_code[0][1] = f_code;
  1149. }
  1150. if (s->pict_type == B_TYPE) {
  1151. s->full_pel[1] = get_bits1(&s->gb);
  1152. f_code = get_bits(&s->gb, 3);
  1153. if (f_code == 0)
  1154. return -1;
  1155. s->mpeg_f_code[1][0] = f_code;
  1156. s->mpeg_f_code[1][1] = f_code;
  1157. }
  1158. s->y_dc_scale = 8;
  1159. s->c_dc_scale = 8;
  1160. s->first_slice = 1;
  1161. return 0;
  1162. }
  1163. static void mpeg_decode_sequence_extension(MpegEncContext *s)
  1164. {
  1165. int horiz_size_ext, vert_size_ext;
  1166. int bit_rate_ext, vbv_buf_ext, low_delay;
  1167. int frame_rate_ext_n, frame_rate_ext_d;
  1168. skip_bits(&s->gb, 8); /* profil and level */
  1169. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1170. skip_bits(&s->gb, 2); /* chroma_format */
  1171. horiz_size_ext = get_bits(&s->gb, 2);
  1172. vert_size_ext = get_bits(&s->gb, 2);
  1173. s->width |= (horiz_size_ext << 12);
  1174. s->height |= (vert_size_ext << 12);
  1175. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1176. s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400;
  1177. skip_bits1(&s->gb); /* marker */
  1178. vbv_buf_ext = get_bits(&s->gb, 8);
  1179. low_delay = get_bits1(&s->gb);
  1180. frame_rate_ext_n = get_bits(&s->gb, 2);
  1181. frame_rate_ext_d = get_bits(&s->gb, 5);
  1182. if (frame_rate_ext_d >= 1)
  1183. s->frame_rate = (s->frame_rate * frame_rate_ext_n) / frame_rate_ext_d;
  1184. dprintf("sequence extension\n");
  1185. s->mpeg2 = 1;
  1186. s->avctx->sub_id = 2; /* indicates mpeg2 found */
  1187. }
  1188. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1189. {
  1190. int i, v, j;
  1191. dprintf("matrix extension\n");
  1192. if (get_bits1(&s->gb)) {
  1193. for(i=0;i<64;i++) {
  1194. v = get_bits(&s->gb, 8);
  1195. j = zigzag_direct[i];
  1196. s->intra_matrix[j] = v;
  1197. s->chroma_intra_matrix[j] = v;
  1198. }
  1199. }
  1200. if (get_bits1(&s->gb)) {
  1201. for(i=0;i<64;i++) {
  1202. v = get_bits(&s->gb, 8);
  1203. j = zigzag_direct[i];
  1204. s->inter_matrix[j] = v;
  1205. s->chroma_inter_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_intra_matrix[j] = v;
  1213. }
  1214. }
  1215. if (get_bits1(&s->gb)) {
  1216. for(i=0;i<64;i++) {
  1217. v = get_bits(&s->gb, 8);
  1218. j = zigzag_direct[i];
  1219. s->chroma_inter_matrix[j] = v;
  1220. }
  1221. }
  1222. }
  1223. static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
  1224. {
  1225. s->full_pel[0] = s->full_pel[1] = 0;
  1226. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1227. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1228. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1229. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1230. s->intra_dc_precision = get_bits(&s->gb, 2);
  1231. s->picture_structure = get_bits(&s->gb, 2);
  1232. s->top_field_first = get_bits1(&s->gb);
  1233. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1234. s->concealment_motion_vectors = get_bits1(&s->gb);
  1235. s->q_scale_type = get_bits1(&s->gb);
  1236. s->intra_vlc_format = get_bits1(&s->gb);
  1237. s->alternate_scan = get_bits1(&s->gb);
  1238. s->repeat_first_field = get_bits1(&s->gb);
  1239. s->chroma_420_type = get_bits1(&s->gb);
  1240. s->progressive_frame = get_bits1(&s->gb);
  1241. /* composite display not parsed */
  1242. dprintf("intra_dc_precision=%d\n", s->intra_dc_precision);
  1243. dprintf("picture_structure=%d\n", s->picture_structure);
  1244. dprintf("top field first=%d\n", s->top_field_first);
  1245. dprintf("repeat first field=%d\n", s->repeat_first_field);
  1246. dprintf("conceal=%d\n", s->concealment_motion_vectors);
  1247. dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
  1248. dprintf("alternate_scan=%d\n", s->alternate_scan);
  1249. dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1250. dprintf("progressive_frame=%d\n", s->progressive_frame);
  1251. }
  1252. static void mpeg_decode_extension(AVCodecContext *avctx,
  1253. UINT8 *buf, int buf_size)
  1254. {
  1255. Mpeg1Context *s1 = avctx->priv_data;
  1256. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1257. int ext_type;
  1258. init_get_bits(&s->gb, buf, buf_size);
  1259. ext_type = get_bits(&s->gb, 4);
  1260. switch(ext_type) {
  1261. case 0x1:
  1262. /* sequence ext */
  1263. mpeg_decode_sequence_extension(s);
  1264. break;
  1265. case 0x3:
  1266. /* quant matrix extension */
  1267. mpeg_decode_quant_matrix_extension(s);
  1268. break;
  1269. case 0x8:
  1270. /* picture extension */
  1271. mpeg_decode_picture_coding_extension(s);
  1272. break;
  1273. }
  1274. }
  1275. /* return 1 if end of frame */
  1276. static int mpeg_decode_slice(AVCodecContext *avctx,
  1277. AVPicture *pict,
  1278. int start_code,
  1279. UINT8 *buf, int buf_size)
  1280. {
  1281. Mpeg1Context *s1 = avctx->priv_data;
  1282. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1283. int ret;
  1284. start_code = (start_code - 1) & 0xff;
  1285. if (start_code >= s->mb_height)
  1286. return -1;
  1287. s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
  1288. s->last_dc[1] = s->last_dc[0];
  1289. s->last_dc[2] = s->last_dc[0];
  1290. memset(s->last_mv, 0, sizeof(s->last_mv));
  1291. s->mb_x = -1;
  1292. s->mb_y = start_code;
  1293. s->mb_incr = 0;
  1294. /* start frame decoding */
  1295. if (s->first_slice) {
  1296. s->first_slice = 0;
  1297. MPV_frame_start(s);
  1298. }
  1299. init_get_bits(&s->gb, buf, buf_size);
  1300. s->qscale = get_qscale(s);
  1301. /* extra slice info */
  1302. while (get_bits1(&s->gb) != 0) {
  1303. skip_bits(&s->gb, 8);
  1304. }
  1305. for(;;) {
  1306. clear_blocks(s->block[0]);
  1307. emms_c();
  1308. ret = mpeg_decode_mb(s, s->block);
  1309. dprintf("ret=%d\n", ret);
  1310. if (ret < 0)
  1311. return -1;
  1312. if (ret == 1)
  1313. break;
  1314. MPV_decode_mb(s, s->block);
  1315. }
  1316. emms_c();
  1317. /* end of slice reached */
  1318. if (s->mb_x == (s->mb_width - 1) &&
  1319. s->mb_y == (s->mb_height - 1)) {
  1320. /* end of image */
  1321. UINT8 **picture;
  1322. MPV_frame_end(s);
  1323. /* XXX: incorrect reported qscale for mpeg2 */
  1324. if (s->pict_type == B_TYPE) {
  1325. picture = s->current_picture;
  1326. avctx->quality = s->qscale;
  1327. } else {
  1328. /* latency of 1 frame for I and P frames */
  1329. /* XXX: use another variable than picture_number */
  1330. if (s->picture_number == 0) {
  1331. picture = NULL;
  1332. } else {
  1333. picture = s->last_picture;
  1334. avctx->quality = s->last_qscale;
  1335. }
  1336. s->last_qscale = s->qscale;
  1337. s->picture_number++;
  1338. }
  1339. if (picture) {
  1340. pict->data[0] = picture[0];
  1341. pict->data[1] = picture[1];
  1342. pict->data[2] = picture[2];
  1343. pict->linesize[0] = s->linesize;
  1344. pict->linesize[1] = s->linesize / 2;
  1345. pict->linesize[2] = s->linesize / 2;
  1346. return 1;
  1347. } else {
  1348. return 0;
  1349. }
  1350. } else {
  1351. return 0;
  1352. }
  1353. }
  1354. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1355. UINT8 *buf, int buf_size)
  1356. {
  1357. Mpeg1Context *s1 = avctx->priv_data;
  1358. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1359. int width, height, i, v, j;
  1360. init_get_bits(&s->gb, buf, buf_size);
  1361. width = get_bits(&s->gb, 12);
  1362. height = get_bits(&s->gb, 12);
  1363. skip_bits(&s->gb, 4);
  1364. s->frame_rate_index = get_bits(&s->gb, 4);
  1365. if (s->frame_rate_index == 0)
  1366. return -1;
  1367. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1368. if (get_bits1(&s->gb) == 0) /* marker */
  1369. return -1;
  1370. if (width <= 0 || height <= 0 ||
  1371. (width % 2) != 0 || (height % 2) != 0)
  1372. return -1;
  1373. if (width != s->width ||
  1374. height != s->height) {
  1375. /* start new mpeg1 context decoding */
  1376. s->out_format = FMT_MPEG1;
  1377. if (s1->mpeg_enc_ctx_allocated) {
  1378. MPV_common_end(s);
  1379. }
  1380. s->width = width;
  1381. s->height = height;
  1382. s->has_b_frames = 1;
  1383. s->avctx = avctx;
  1384. avctx->width = width;
  1385. avctx->height = height;
  1386. if (s->frame_rate_index >= 9) {
  1387. /* at least give a valid frame rate (some old mpeg1 have this) */
  1388. avctx->frame_rate = 25 * FRAME_RATE_BASE;
  1389. } else {
  1390. avctx->frame_rate = frame_rate_tab[s->frame_rate_index];
  1391. }
  1392. s->frame_rate = avctx->frame_rate;
  1393. avctx->bit_rate = s->bit_rate;
  1394. if (MPV_common_init(s) < 0)
  1395. return -1;
  1396. mpeg1_init_vlc(s);
  1397. s1->mpeg_enc_ctx_allocated = 1;
  1398. }
  1399. skip_bits(&s->gb, 10); /* vbv_buffer_size */
  1400. skip_bits(&s->gb, 1);
  1401. /* get matrix */
  1402. if (get_bits1(&s->gb)) {
  1403. for(i=0;i<64;i++) {
  1404. v = get_bits(&s->gb, 8);
  1405. j = zigzag_direct[i];
  1406. s->intra_matrix[j] = v;
  1407. s->chroma_intra_matrix[j] = v;
  1408. }
  1409. #ifdef DEBUG
  1410. dprintf("intra matrix present\n");
  1411. for(i=0;i<64;i++)
  1412. dprintf(" %d", s->intra_matrix[zigzag_direct[i]]);
  1413. printf("\n");
  1414. #endif
  1415. } else {
  1416. for(i=0;i<64;i++) {
  1417. v = ff_mpeg1_default_intra_matrix[i];
  1418. s->intra_matrix[i] = v;
  1419. s->chroma_intra_matrix[i] = v;
  1420. }
  1421. }
  1422. if (get_bits1(&s->gb)) {
  1423. for(i=0;i<64;i++) {
  1424. v = get_bits(&s->gb, 8);
  1425. j = zigzag_direct[i];
  1426. s->inter_matrix[j] = v;
  1427. s->chroma_inter_matrix[j] = v;
  1428. }
  1429. #ifdef DEBUG
  1430. dprintf("non intra matrix present\n");
  1431. for(i=0;i<64;i++)
  1432. dprintf(" %d", s->inter_matrix[zigzag_direct[i]]);
  1433. printf("\n");
  1434. #endif
  1435. } else {
  1436. for(i=0;i<64;i++) {
  1437. v = ff_mpeg1_default_non_intra_matrix[i];
  1438. s->inter_matrix[i] = v;
  1439. s->chroma_inter_matrix[i] = v;
  1440. }
  1441. }
  1442. /* we set mpeg2 parameters so that it emulates mpeg1 */
  1443. s->progressive_sequence = 1;
  1444. s->progressive_frame = 1;
  1445. s->picture_structure = PICT_FRAME;
  1446. s->frame_pred_frame_dct = 1;
  1447. s->mpeg2 = 0;
  1448. avctx->sub_id = 1; /* indicates mpeg1 */
  1449. return 0;
  1450. }
  1451. /* handle buffering and image synchronisation */
  1452. static int mpeg_decode_frame(AVCodecContext *avctx,
  1453. void *data, int *data_size,
  1454. UINT8 *buf, int buf_size)
  1455. {
  1456. Mpeg1Context *s = avctx->priv_data;
  1457. UINT8 *buf_end, *buf_ptr, *buf_start;
  1458. int len, start_code_found, ret, code, start_code, input_size;
  1459. AVPicture *picture = data;
  1460. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1461. dprintf("fill_buffer\n");
  1462. *data_size = 0;
  1463. /* special case for last picture */
  1464. if (buf_size == 0) {
  1465. if (s2->picture_number > 0) {
  1466. picture->data[0] = s2->next_picture[0];
  1467. picture->data[1] = s2->next_picture[1];
  1468. picture->data[2] = s2->next_picture[2];
  1469. picture->linesize[0] = s2->linesize;
  1470. picture->linesize[1] = s2->linesize / 2;
  1471. picture->linesize[2] = s2->linesize / 2;
  1472. *data_size = sizeof(AVPicture);
  1473. }
  1474. return 0;
  1475. }
  1476. buf_ptr = buf;
  1477. buf_end = buf + buf_size;
  1478. #if 0
  1479. if (s->repeat_field % 2 == 1) {
  1480. s->repeat_field++;
  1481. //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
  1482. // s2->picture_number, s->repeat_field);
  1483. if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
  1484. *data_size = sizeof(AVPicture);
  1485. goto the_end;
  1486. }
  1487. }
  1488. #endif
  1489. while (buf_ptr < buf_end) {
  1490. buf_start = buf_ptr;
  1491. /* find start next code */
  1492. code = find_start_code(&buf_ptr, buf_end, &s->header_state);
  1493. if (code >= 0) {
  1494. start_code_found = 1;
  1495. } else {
  1496. start_code_found = 0;
  1497. }
  1498. /* copy to buffer */
  1499. len = buf_ptr - buf_start;
  1500. if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
  1501. /* data too big : flush */
  1502. s->buf_ptr = s->buffer;
  1503. if (start_code_found)
  1504. s->start_code = code;
  1505. } else {
  1506. memcpy(s->buf_ptr, buf_start, len);
  1507. s->buf_ptr += len;
  1508. if (start_code_found) {
  1509. /* prepare data for next start code */
  1510. input_size = s->buf_ptr - s->buffer;
  1511. start_code = s->start_code;
  1512. s->buf_ptr = s->buffer;
  1513. s->start_code = code;
  1514. switch(start_code) {
  1515. case SEQ_START_CODE:
  1516. mpeg1_decode_sequence(avctx, s->buffer,
  1517. input_size);
  1518. break;
  1519. case PICTURE_START_CODE:
  1520. /* we have a complete image : we try to decompress it */
  1521. mpeg1_decode_picture(avctx,
  1522. s->buffer, input_size);
  1523. break;
  1524. case EXT_START_CODE:
  1525. mpeg_decode_extension(avctx,
  1526. s->buffer, input_size);
  1527. break;
  1528. default:
  1529. if (start_code >= SLICE_MIN_START_CODE &&
  1530. start_code <= SLICE_MAX_START_CODE) {
  1531. ret = mpeg_decode_slice(avctx, picture,
  1532. start_code, s->buffer, input_size);
  1533. if (ret == 1) {
  1534. /* got a picture: exit */
  1535. /* first check if we must repeat the frame */
  1536. avctx->repeat_pict = 0;
  1537. #if 0
  1538. if (s2->progressive_frame && s2->repeat_first_field) {
  1539. //fprintf(stderr,"\nRepeat this frame: %d! pict: %d",avctx->frame_number,s2->picture_number);
  1540. //s2->repeat_first_field = 0;
  1541. //s2->progressive_frame = 0;
  1542. if (++s->repeat_field > 2)
  1543. s->repeat_field = 0;
  1544. avctx->repeat_pict = 1;
  1545. }
  1546. #endif
  1547. if (s2->repeat_first_field) {
  1548. if (s2->progressive_sequence) {
  1549. if (s2->top_field_first)
  1550. avctx->repeat_pict = 4;
  1551. else
  1552. avctx->repeat_pict = 2;
  1553. } else if (s2->progressive_frame) {
  1554. avctx->repeat_pict = 1;
  1555. }
  1556. }
  1557. *data_size = sizeof(AVPicture);
  1558. goto the_end;
  1559. }
  1560. }
  1561. break;
  1562. }
  1563. }
  1564. }
  1565. }
  1566. the_end:
  1567. return buf_ptr - buf;
  1568. }
  1569. static int mpeg_decode_end(AVCodecContext *avctx)
  1570. {
  1571. Mpeg1Context *s = avctx->priv_data;
  1572. if (s->mpeg_enc_ctx_allocated)
  1573. MPV_common_end(&s->mpeg_enc_ctx);
  1574. return 0;
  1575. }
  1576. AVCodec mpeg_decoder = {
  1577. "mpegvideo",
  1578. CODEC_TYPE_VIDEO,
  1579. CODEC_ID_MPEG1VIDEO,
  1580. sizeof(Mpeg1Context),
  1581. mpeg_decode_init,
  1582. NULL,
  1583. mpeg_decode_end,
  1584. mpeg_decode_frame,
  1585. };