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.

1800 lines
56KB

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