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.

2007 lines
65KB

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