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.

1769 lines
55KB

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