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.

1791 lines
55KB

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