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.

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