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.

2970 lines
100KB

  1. /*
  2. * MPEG1 codec / MPEG2 decoder
  3. * Copyright (c) 2000,2001 Fabrice Bellard.
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /**
  21. * @file mpeg12.c
  22. * MPEG1/2 codec
  23. */
  24. //#define DEBUG
  25. #include "avcodec.h"
  26. #include "dsputil.h"
  27. #include "mpegvideo.h"
  28. #include "mpeg12data.h"
  29. //#undef NDEBUG
  30. //#include <assert.h>
  31. /* Start codes. */
  32. #define SEQ_END_CODE 0x000001b7
  33. #define SEQ_START_CODE 0x000001b3
  34. #define GOP_START_CODE 0x000001b8
  35. #define PICTURE_START_CODE 0x00000100
  36. #define SLICE_MIN_START_CODE 0x00000101
  37. #define SLICE_MAX_START_CODE 0x000001af
  38. #define EXT_START_CODE 0x000001b5
  39. #define USER_START_CODE 0x000001b2
  40. #define DC_VLC_BITS 9
  41. #define MV_VLC_BITS 9
  42. #define MBINCR_VLC_BITS 9
  43. #define MB_PAT_VLC_BITS 9
  44. #define MB_PTYPE_VLC_BITS 6
  45. #define MB_BTYPE_VLC_BITS 6
  46. #define TEX_VLC_BITS 9
  47. #ifdef CONFIG_ENCODERS
  48. static void mpeg1_encode_block(MpegEncContext *s,
  49. DCTELEM *block,
  50. int component);
  51. static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code); // RAL: f_code parameter added
  52. #endif //CONFIG_ENCODERS
  53. static inline int mpeg1_decode_block_inter(MpegEncContext *s,
  54. DCTELEM *block,
  55. int n);
  56. static inline int mpeg1_decode_block_intra(MpegEncContext *s,
  57. DCTELEM *block,
  58. int n);
  59. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
  60. DCTELEM *block,
  61. int n);
  62. static inline int mpeg2_decode_block_intra(MpegEncContext *s,
  63. DCTELEM *block,
  64. int n);
  65. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
  66. static void exchange_uv(MpegEncContext *s);
  67. #ifdef HAVE_XVMC
  68. extern int XVMC_field_start(MpegEncContext *s, AVCodecContext *avctx);
  69. extern int XVMC_field_end(MpegEncContext *s);
  70. extern void XVMC_pack_pblocks(MpegEncContext *s,int cbp);
  71. extern void XVMC_init_block(MpegEncContext *s);//set s->block
  72. #endif
  73. const enum PixelFormat pixfmt_yuv_420[]= {PIX_FMT_YUV420P,-1};
  74. const enum PixelFormat pixfmt_yuv_422[]= {PIX_FMT_YUV422P,-1};
  75. const enum PixelFormat pixfmt_yuv_444[]= {PIX_FMT_YUV444P,-1};
  76. const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
  77. PIX_FMT_XVMC_MPEG2_IDCT,
  78. PIX_FMT_XVMC_MPEG2_MC,
  79. -1};
  80. #ifdef CONFIG_ENCODERS
  81. static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL;
  82. static uint8_t fcode_tab[MAX_MV*2+1];
  83. static uint32_t uni_mpeg1_ac_vlc_bits[64*64*2];
  84. static uint8_t uni_mpeg1_ac_vlc_len [64*64*2];
  85. /* simple include everything table for dc, first byte is bits number next 3 are code*/
  86. static uint32_t mpeg1_lum_dc_uni[512];
  87. static uint32_t mpeg1_chr_dc_uni[512];
  88. static uint8_t mpeg1_index_run[2][64];
  89. static int8_t mpeg1_max_level[2][64];
  90. #endif //CONFIG_ENCODERS
  91. static void init_2d_vlc_rl(RLTable *rl)
  92. {
  93. int i;
  94. init_vlc(&rl->vlc, TEX_VLC_BITS, rl->n + 2,
  95. &rl->table_vlc[0][1], 4, 2,
  96. &rl->table_vlc[0][0], 4, 2);
  97. rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
  98. for(i=0; i<rl->vlc.table_size; i++){
  99. int code= rl->vlc.table[i][0];
  100. int len = rl->vlc.table[i][1];
  101. int level, run;
  102. if(len==0){ // illegal code
  103. run= 65;
  104. level= MAX_LEVEL;
  105. }else if(len<0){ //more bits needed
  106. run= 0;
  107. level= code;
  108. }else{
  109. if(code==rl->n){ //esc
  110. run= 65;
  111. level= 0;
  112. }else if(code==rl->n+1){ //eob
  113. run= 0;
  114. level= 127;
  115. }else{
  116. run= rl->table_run [code] + 1;
  117. level= rl->table_level[code];
  118. }
  119. }
  120. rl->rl_vlc[0][i].len= len;
  121. rl->rl_vlc[0][i].level= level;
  122. rl->rl_vlc[0][i].run= run;
  123. }
  124. }
  125. #ifdef CONFIG_ENCODERS
  126. static void init_uni_ac_vlc(RLTable *rl, uint32_t *uni_ac_vlc_bits, uint8_t *uni_ac_vlc_len){
  127. int i;
  128. for(i=0; i<128; i++){
  129. int level= i-64;
  130. int run;
  131. for(run=0; run<64; run++){
  132. int len, bits, code;
  133. int alevel= ABS(level);
  134. int sign= (level>>31)&1;
  135. if (alevel > rl->max_level[0][run])
  136. code= 111; /*rl->n*/
  137. else
  138. code= rl->index_run[0][run] + alevel - 1;
  139. if (code < 111 /* rl->n */) {
  140. /* store the vlc & sign at once */
  141. len= mpeg1_vlc[code][1]+1;
  142. bits= (mpeg1_vlc[code][0]<<1) + sign;
  143. } else {
  144. len= mpeg1_vlc[111/*rl->n*/][1]+6;
  145. bits= mpeg1_vlc[111/*rl->n*/][0]<<6;
  146. bits|= run;
  147. if (alevel < 128) {
  148. bits<<=8; len+=8;
  149. bits|= level & 0xff;
  150. } else {
  151. bits<<=16; len+=16;
  152. bits|= level & 0xff;
  153. if (level < 0) {
  154. bits|= 0x8001 + level + 255;
  155. } else {
  156. bits|= level & 0xffff;
  157. }
  158. }
  159. }
  160. uni_ac_vlc_bits[UNI_AC_ENC_INDEX(run, i)]= bits;
  161. uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len;
  162. }
  163. }
  164. }
  165. static int find_frame_rate_index(MpegEncContext *s){
  166. int i;
  167. int64_t dmin= INT64_MAX;
  168. int64_t d;
  169. for(i=1;i<14;i++) {
  170. int64_t n0= 1001LL/frame_rate_tab[i].den*frame_rate_tab[i].num*s->avctx->frame_rate_base;
  171. int64_t n1= 1001LL*s->avctx->frame_rate;
  172. if(s->avctx->strict_std_compliance >= 0 && i>=9) break;
  173. d = ABS(n0 - n1);
  174. if(d < dmin){
  175. dmin=d;
  176. s->frame_rate_index= i;
  177. }
  178. }
  179. if(dmin)
  180. return -1;
  181. else
  182. return 0;
  183. }
  184. static int encode_init(AVCodecContext *avctx)
  185. {
  186. MpegEncContext *s = avctx->priv_data;
  187. if(MPV_encode_init(avctx) < 0)
  188. return -1;
  189. if(find_frame_rate_index(s) < 0){
  190. if(s->strict_std_compliance >=0){
  191. av_log(avctx, AV_LOG_ERROR, "MPEG1/2 doesnt support %d/%d fps\n", avctx->frame_rate, avctx->frame_rate_base);
  192. return -1;
  193. }else{
  194. av_log(avctx, AV_LOG_INFO, "MPEG1/2 doesnt support %d/%d fps, there may be AV sync issues\n", avctx->frame_rate, avctx->frame_rate_base);
  195. }
  196. }
  197. return 0;
  198. }
  199. static void put_header(MpegEncContext *s, int header)
  200. {
  201. align_put_bits(&s->pb);
  202. put_bits(&s->pb, 16, header>>16);
  203. put_bits(&s->pb, 16, header&0xFFFF);
  204. }
  205. /* put sequence header if needed */
  206. static void mpeg1_encode_sequence_header(MpegEncContext *s)
  207. {
  208. unsigned int vbv_buffer_size;
  209. unsigned int fps, v;
  210. int n, i;
  211. uint64_t time_code;
  212. float best_aspect_error= 1E10;
  213. float aspect_ratio= av_q2d(s->avctx->sample_aspect_ratio);
  214. int constraint_parameter_flag;
  215. if(aspect_ratio==0.0) aspect_ratio= 1.0; //pixel aspect 1:1 (VGA)
  216. if (s->current_picture.key_frame) {
  217. AVRational framerate= frame_rate_tab[s->frame_rate_index];
  218. /* mpeg1 header repeated every gop */
  219. put_header(s, SEQ_START_CODE);
  220. put_bits(&s->pb, 12, s->width);
  221. put_bits(&s->pb, 12, s->height);
  222. for(i=1; i<15; i++){
  223. float error= aspect_ratio;
  224. if(s->codec_id == CODEC_ID_MPEG1VIDEO || i <=1)
  225. error-= 1.0/mpeg1_aspect[i];
  226. else
  227. error-= av_q2d(mpeg2_aspect[i])*s->height/s->width;
  228. error= ABS(error);
  229. if(error < best_aspect_error){
  230. best_aspect_error= error;
  231. s->aspect_ratio_info= i;
  232. }
  233. }
  234. put_bits(&s->pb, 4, s->aspect_ratio_info);
  235. put_bits(&s->pb, 4, s->frame_rate_index);
  236. if(s->avctx->rc_max_rate){
  237. v = (s->avctx->rc_max_rate + 399) / 400;
  238. if (v > 0x3ffff && s->codec_id == CODEC_ID_MPEG1VIDEO)
  239. v = 0x3ffff;
  240. }else{
  241. v= 0x3FFFF;
  242. }
  243. if(s->avctx->rc_buffer_size)
  244. vbv_buffer_size = s->avctx->rc_buffer_size;
  245. else
  246. /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
  247. vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
  248. vbv_buffer_size= (vbv_buffer_size + 16383) / 16384;
  249. put_bits(&s->pb, 18, v & 0x3FFFF);
  250. put_bits(&s->pb, 1, 1); /* marker */
  251. put_bits(&s->pb, 10, vbv_buffer_size & 0x3FF);
  252. constraint_parameter_flag=
  253. s->width <= 768 && s->height <= 576 &&
  254. s->mb_width * s->mb_height <= 396 &&
  255. s->mb_width * s->mb_height * framerate.num <= framerate.den*396*25 &&
  256. framerate.num <= framerate.den*30 &&
  257. vbv_buffer_size <= 20 &&
  258. v <= 1856000/400 &&
  259. s->codec_id == CODEC_ID_MPEG1VIDEO;
  260. put_bits(&s->pb, 1, constraint_parameter_flag);
  261. ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
  262. ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
  263. if(s->codec_id == CODEC_ID_MPEG2VIDEO){
  264. put_header(s, EXT_START_CODE);
  265. put_bits(&s->pb, 4, 1); //seq ext
  266. put_bits(&s->pb, 1, 0); //esc
  267. put_bits(&s->pb, 3, 4); //profile
  268. put_bits(&s->pb, 4, 8); //level
  269. put_bits(&s->pb, 1, s->progressive_sequence);
  270. put_bits(&s->pb, 2, 1); //chroma format 4:2:0
  271. put_bits(&s->pb, 2, 0); //horizontal size ext
  272. put_bits(&s->pb, 2, 0); //vertical size ext
  273. put_bits(&s->pb, 12, v>>18); //bitrate ext
  274. put_bits(&s->pb, 1, 1); //marker
  275. put_bits(&s->pb, 8, vbv_buffer_size >>10); //vbv buffer ext
  276. put_bits(&s->pb, 1, s->low_delay);
  277. put_bits(&s->pb, 2, 0); // frame_rate_ext_n
  278. put_bits(&s->pb, 5, 0); // frame_rate_ext_d
  279. }
  280. put_header(s, GOP_START_CODE);
  281. put_bits(&s->pb, 1, 0); /* do drop frame */
  282. /* time code : we must convert from the real frame rate to a
  283. fake mpeg frame rate in case of low frame rate */
  284. fps = (framerate.num + framerate.den/2)/ framerate.den;
  285. time_code = s->current_picture_ptr->coded_picture_number;
  286. s->gop_picture_number = time_code;
  287. put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
  288. put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
  289. put_bits(&s->pb, 1, 1);
  290. put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
  291. put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
  292. put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP));
  293. put_bits(&s->pb, 1, 0); /* broken link */
  294. }
  295. }
  296. static inline void encode_mb_skip_run(MpegEncContext *s, int run){
  297. while (run >= 33) {
  298. put_bits(&s->pb, 11, 0x008);
  299. run -= 33;
  300. }
  301. put_bits(&s->pb, mbAddrIncrTable[run][1],
  302. mbAddrIncrTable[run][0]);
  303. }
  304. #endif //CONFIG_ENCODERS
  305. static void common_init(MpegEncContext *s)
  306. {
  307. int i;
  308. s->y_dc_scale_table=
  309. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  310. for(i=0;i<64;i++)
  311. s->dsp.idct_permutation[i]=i;
  312. }
  313. void ff_mpeg1_clean_buffers(MpegEncContext *s){
  314. s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
  315. s->last_dc[1] = s->last_dc[0];
  316. s->last_dc[2] = s->last_dc[0];
  317. memset(s->last_mv, 0, sizeof(s->last_mv));
  318. }
  319. #ifdef CONFIG_ENCODERS
  320. void ff_mpeg1_encode_slice_header(MpegEncContext *s){
  321. put_header(s, SLICE_MIN_START_CODE + s->mb_y);
  322. put_bits(&s->pb, 5, s->qscale); /* quantizer scale */
  323. put_bits(&s->pb, 1, 0); /* slice extra information */
  324. }
  325. void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
  326. {
  327. mpeg1_encode_sequence_header(s);
  328. /* mpeg1 picture header */
  329. put_header(s, PICTURE_START_CODE);
  330. /* temporal reference */
  331. // RAL: s->picture_number instead of s->fake_picture_number
  332. put_bits(&s->pb, 10, (s->picture_number -
  333. s->gop_picture_number) & 0x3ff);
  334. put_bits(&s->pb, 3, s->pict_type);
  335. s->vbv_delay_ptr= s->pb.buf + put_bits_count(&s->pb)/8;
  336. put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
  337. // RAL: Forward f_code also needed for B frames
  338. if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
  339. put_bits(&s->pb, 1, 0); /* half pel coordinates */
  340. if(s->codec_id == CODEC_ID_MPEG1VIDEO)
  341. put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
  342. else
  343. put_bits(&s->pb, 3, 7); /* forward_f_code */
  344. }
  345. // RAL: Backward f_code necessary for B frames
  346. if (s->pict_type == B_TYPE) {
  347. put_bits(&s->pb, 1, 0); /* half pel coordinates */
  348. if(s->codec_id == CODEC_ID_MPEG1VIDEO)
  349. put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
  350. else
  351. put_bits(&s->pb, 3, 7); /* backward_f_code */
  352. }
  353. put_bits(&s->pb, 1, 0); /* extra bit picture */
  354. s->frame_pred_frame_dct = 1;
  355. if(s->codec_id == CODEC_ID_MPEG2VIDEO){
  356. put_header(s, EXT_START_CODE);
  357. put_bits(&s->pb, 4, 8); //pic ext
  358. if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
  359. put_bits(&s->pb, 4, s->f_code);
  360. put_bits(&s->pb, 4, s->f_code);
  361. }else{
  362. put_bits(&s->pb, 8, 255);
  363. }
  364. if (s->pict_type == B_TYPE) {
  365. put_bits(&s->pb, 4, s->b_code);
  366. put_bits(&s->pb, 4, s->b_code);
  367. }else{
  368. put_bits(&s->pb, 8, 255);
  369. }
  370. put_bits(&s->pb, 2, s->intra_dc_precision);
  371. assert(s->picture_structure == PICT_FRAME);
  372. put_bits(&s->pb, 2, s->picture_structure);
  373. if (s->progressive_sequence) {
  374. put_bits(&s->pb, 1, 0); /* no repeat */
  375. } else {
  376. put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
  377. }
  378. /* XXX: optimize the generation of this flag with entropy
  379. measures */
  380. s->frame_pred_frame_dct = s->progressive_sequence;
  381. put_bits(&s->pb, 1, s->frame_pred_frame_dct);
  382. put_bits(&s->pb, 1, s->concealment_motion_vectors);
  383. put_bits(&s->pb, 1, s->q_scale_type);
  384. put_bits(&s->pb, 1, s->intra_vlc_format);
  385. put_bits(&s->pb, 1, s->alternate_scan);
  386. put_bits(&s->pb, 1, s->repeat_first_field);
  387. put_bits(&s->pb, 1, s->chroma_420_type=1);
  388. s->progressive_frame = s->progressive_sequence;
  389. put_bits(&s->pb, 1, s->progressive_frame);
  390. put_bits(&s->pb, 1, 0); //composite_display_flag
  391. }
  392. if(s->flags & CODEC_FLAG_SVCD_SCAN_OFFSET){
  393. int i;
  394. put_header(s, USER_START_CODE);
  395. for(i=0; i<sizeof(svcd_scan_offset_placeholder); i++){
  396. put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
  397. }
  398. }
  399. s->mb_y=0;
  400. ff_mpeg1_encode_slice_header(s);
  401. }
  402. static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
  403. int has_mv, int field_motion)
  404. {
  405. put_bits(&s->pb, n, bits);
  406. if (!s->frame_pred_frame_dct) {
  407. if (has_mv)
  408. put_bits(&s->pb, 2, 2 - field_motion); /* motion_type: frame/field */
  409. put_bits(&s->pb, 1, s->interlaced_dct);
  410. }
  411. }
  412. void mpeg1_encode_mb(MpegEncContext *s,
  413. DCTELEM block[6][64],
  414. int motion_x, int motion_y)
  415. {
  416. int i, cbp;
  417. const int mb_x = s->mb_x;
  418. const int mb_y = s->mb_y;
  419. const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
  420. /* compute cbp */
  421. cbp = 0;
  422. for(i=0;i<6;i++) {
  423. if (s->block_last_index[i] >= 0)
  424. cbp |= 1 << (5 - i);
  425. }
  426. if (cbp == 0 && !first_mb && (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) &&
  427. ((s->pict_type == P_TYPE && s->mv_type == MV_TYPE_16X16 && (motion_x | motion_y) == 0) ||
  428. (s->pict_type == B_TYPE && s->mv_dir == s->last_mv_dir && (((s->mv_dir & MV_DIR_FORWARD) ? ((s->mv[0][0][0] - s->last_mv[0][0][0])|(s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
  429. ((s->mv_dir & MV_DIR_BACKWARD) ? ((s->mv[1][0][0] - s->last_mv[1][0][0])|(s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
  430. s->mb_skip_run++;
  431. s->qscale -= s->dquant;
  432. s->skip_count++;
  433. s->misc_bits++;
  434. s->last_bits++;
  435. if(s->pict_type == P_TYPE){
  436. s->last_mv[0][1][0]= s->last_mv[0][0][0]=
  437. s->last_mv[0][1][1]= s->last_mv[0][0][1]= 0;
  438. }
  439. } else {
  440. if(first_mb){
  441. assert(s->mb_skip_run == 0);
  442. encode_mb_skip_run(s, s->mb_x);
  443. }else{
  444. encode_mb_skip_run(s, s->mb_skip_run);
  445. }
  446. if (s->pict_type == I_TYPE) {
  447. if(s->dquant && cbp){
  448. put_mb_modes(s, 2, 1, 0, 0); /* macroblock_type : macroblock_quant = 1 */
  449. put_bits(&s->pb, 5, s->qscale);
  450. }else{
  451. put_mb_modes(s, 1, 1, 0, 0); /* macroblock_type : macroblock_quant = 0 */
  452. s->qscale -= s->dquant;
  453. }
  454. s->misc_bits+= get_bits_diff(s);
  455. s->i_count++;
  456. } else if (s->mb_intra) {
  457. if(s->dquant && cbp){
  458. put_mb_modes(s, 6, 0x01, 0, 0);
  459. put_bits(&s->pb, 5, s->qscale);
  460. }else{
  461. put_mb_modes(s, 5, 0x03, 0, 0);
  462. s->qscale -= s->dquant;
  463. }
  464. s->misc_bits+= get_bits_diff(s);
  465. s->i_count++;
  466. memset(s->last_mv, 0, sizeof(s->last_mv));
  467. } else if (s->pict_type == P_TYPE) {
  468. if(s->mv_type == MV_TYPE_16X16){
  469. if (cbp != 0) {
  470. if ((motion_x|motion_y) == 0) {
  471. if(s->dquant){
  472. put_mb_modes(s, 5, 1, 0, 0); /* macroblock_pattern & quant */
  473. put_bits(&s->pb, 5, s->qscale);
  474. }else{
  475. put_mb_modes(s, 2, 1, 0, 0); /* macroblock_pattern only */
  476. }
  477. s->misc_bits+= get_bits_diff(s);
  478. } else {
  479. if(s->dquant){
  480. put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
  481. put_bits(&s->pb, 5, s->qscale);
  482. }else{
  483. put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
  484. }
  485. s->misc_bits+= get_bits_diff(s);
  486. mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
  487. mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
  488. s->mv_bits+= get_bits_diff(s);
  489. }
  490. } else {
  491. put_bits(&s->pb, 3, 1); /* motion only */
  492. if (!s->frame_pred_frame_dct)
  493. put_bits(&s->pb, 2, 2); /* motion_type: frame */
  494. s->misc_bits+= get_bits_diff(s);
  495. mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
  496. mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
  497. s->qscale -= s->dquant;
  498. s->mv_bits+= get_bits_diff(s);
  499. }
  500. s->last_mv[0][1][0]= s->last_mv[0][0][0]= motion_x;
  501. s->last_mv[0][1][1]= s->last_mv[0][0][1]= motion_y;
  502. }else{
  503. assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
  504. if (cbp) {
  505. if(s->dquant){
  506. put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
  507. put_bits(&s->pb, 5, s->qscale);
  508. }else{
  509. put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
  510. }
  511. } else {
  512. put_bits(&s->pb, 3, 1); /* motion only */
  513. put_bits(&s->pb, 2, 1); /* motion_type: field */
  514. s->qscale -= s->dquant;
  515. }
  516. s->misc_bits+= get_bits_diff(s);
  517. for(i=0; i<2; i++){
  518. put_bits(&s->pb, 1, s->field_select[0][i]);
  519. mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
  520. mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
  521. s->last_mv[0][i][0]= s->mv[0][i][0];
  522. s->last_mv[0][i][1]= 2*s->mv[0][i][1];
  523. }
  524. s->mv_bits+= get_bits_diff(s);
  525. }
  526. if(cbp)
  527. put_bits(&s->pb, mbPatTable[cbp][1], mbPatTable[cbp][0]);
  528. s->f_count++;
  529. } else{
  530. static const int mb_type_len[4]={0,3,4,2}; //bak,for,bi
  531. if(s->mv_type == MV_TYPE_16X16){
  532. if (cbp){ // With coded bloc pattern
  533. if (s->dquant) {
  534. if(s->mv_dir == MV_DIR_FORWARD)
  535. put_mb_modes(s, 6, 3, 1, 0);
  536. else
  537. put_mb_modes(s, mb_type_len[s->mv_dir]+3, 2, 1, 0);
  538. put_bits(&s->pb, 5, s->qscale);
  539. } else {
  540. put_mb_modes(s, mb_type_len[s->mv_dir], 3, 1, 0);
  541. }
  542. }else{ // No coded bloc pattern
  543. put_bits(&s->pb, mb_type_len[s->mv_dir], 2);
  544. if (!s->frame_pred_frame_dct)
  545. put_bits(&s->pb, 2, 2); /* motion_type: frame */
  546. s->qscale -= s->dquant;
  547. }
  548. s->misc_bits += get_bits_diff(s);
  549. if (s->mv_dir&MV_DIR_FORWARD){
  550. mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
  551. mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
  552. s->last_mv[0][0][0]=s->last_mv[0][1][0]= s->mv[0][0][0];
  553. s->last_mv[0][0][1]=s->last_mv[0][1][1]= s->mv[0][0][1];
  554. s->f_count++;
  555. }
  556. if (s->mv_dir&MV_DIR_BACKWARD){
  557. mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
  558. mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
  559. s->last_mv[1][0][0]=s->last_mv[1][1][0]= s->mv[1][0][0];
  560. s->last_mv[1][0][1]=s->last_mv[1][1][1]= s->mv[1][0][1];
  561. s->b_count++;
  562. }
  563. }else{
  564. assert(s->mv_type == MV_TYPE_FIELD);
  565. assert(!s->frame_pred_frame_dct);
  566. if (cbp){ // With coded bloc pattern
  567. if (s->dquant) {
  568. if(s->mv_dir == MV_DIR_FORWARD)
  569. put_mb_modes(s, 6, 3, 1, 1);
  570. else
  571. put_mb_modes(s, mb_type_len[s->mv_dir]+3, 2, 1, 1);
  572. put_bits(&s->pb, 5, s->qscale);
  573. } else {
  574. put_mb_modes(s, mb_type_len[s->mv_dir], 3, 1, 1);
  575. }
  576. }else{ // No coded bloc pattern
  577. put_bits(&s->pb, mb_type_len[s->mv_dir], 2);
  578. put_bits(&s->pb, 2, 1); /* motion_type: field */
  579. s->qscale -= s->dquant;
  580. }
  581. s->misc_bits += get_bits_diff(s);
  582. if (s->mv_dir&MV_DIR_FORWARD){
  583. for(i=0; i<2; i++){
  584. put_bits(&s->pb, 1, s->field_select[0][i]);
  585. mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
  586. mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
  587. s->last_mv[0][i][0]= s->mv[0][i][0];
  588. s->last_mv[0][i][1]= 2*s->mv[0][i][1];
  589. }
  590. s->f_count++;
  591. }
  592. if (s->mv_dir&MV_DIR_BACKWARD){
  593. for(i=0; i<2; i++){
  594. put_bits(&s->pb, 1, s->field_select[1][i]);
  595. mpeg1_encode_motion(s, s->mv[1][i][0] - s->last_mv[1][i][0] , s->b_code);
  596. mpeg1_encode_motion(s, s->mv[1][i][1] - (s->last_mv[1][i][1]>>1), s->b_code);
  597. s->last_mv[1][i][0]= s->mv[1][i][0];
  598. s->last_mv[1][i][1]= 2*s->mv[1][i][1];
  599. }
  600. s->b_count++;
  601. }
  602. }
  603. s->mv_bits += get_bits_diff(s);
  604. if(cbp)
  605. put_bits(&s->pb, mbPatTable[cbp][1], mbPatTable[cbp][0]);
  606. }
  607. for(i=0;i<6;i++) {
  608. if (cbp & (1 << (5 - i))) {
  609. mpeg1_encode_block(s, block[i], i);
  610. }
  611. }
  612. s->mb_skip_run = 0;
  613. if(s->mb_intra)
  614. s->i_tex_bits+= get_bits_diff(s);
  615. else
  616. s->p_tex_bits+= get_bits_diff(s);
  617. }
  618. }
  619. // RAL: Parameter added: f_or_b_code
  620. static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
  621. {
  622. int code, bit_size, l, m, bits, range, sign;
  623. if (val == 0) {
  624. /* zero vector */
  625. code = 0;
  626. put_bits(&s->pb,
  627. mbMotionVectorTable[0][1],
  628. mbMotionVectorTable[0][0]);
  629. } else {
  630. bit_size = f_or_b_code - 1;
  631. range = 1 << bit_size;
  632. /* modulo encoding */
  633. l = 16 * range;
  634. m = 2 * l;
  635. if (val < -l) {
  636. val += m;
  637. } else if (val >= l) {
  638. val -= m;
  639. }
  640. if (val >= 0) {
  641. val--;
  642. code = (val >> bit_size) + 1;
  643. bits = val & (range - 1);
  644. sign = 0;
  645. } else {
  646. val = -val;
  647. val--;
  648. code = (val >> bit_size) + 1;
  649. bits = val & (range - 1);
  650. sign = 1;
  651. }
  652. assert(code > 0 && code <= 16);
  653. put_bits(&s->pb,
  654. mbMotionVectorTable[code][1],
  655. mbMotionVectorTable[code][0]);
  656. put_bits(&s->pb, 1, sign);
  657. if (bit_size > 0) {
  658. put_bits(&s->pb, bit_size, bits);
  659. }
  660. }
  661. }
  662. void ff_mpeg1_encode_init(MpegEncContext *s)
  663. {
  664. static int done=0;
  665. common_init(s);
  666. if(!done){
  667. int f_code;
  668. int mv;
  669. int i;
  670. done=1;
  671. init_rl(&rl_mpeg1);
  672. for(i=0; i<64; i++)
  673. {
  674. mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
  675. mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
  676. }
  677. init_uni_ac_vlc(&rl_mpeg1, uni_mpeg1_ac_vlc_bits, uni_mpeg1_ac_vlc_len);
  678. /* build unified dc encoding tables */
  679. for(i=-255; i<256; i++)
  680. {
  681. int adiff, index;
  682. int bits, code;
  683. int diff=i;
  684. adiff = ABS(diff);
  685. if(diff<0) diff--;
  686. index = vlc_dc_table[adiff];
  687. bits= vlc_dc_lum_bits[index] + index;
  688. code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
  689. mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
  690. bits= vlc_dc_chroma_bits[index] + index;
  691. code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
  692. mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
  693. }
  694. mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
  695. for(f_code=1; f_code<=MAX_FCODE; f_code++){
  696. for(mv=-MAX_MV; mv<=MAX_MV; mv++){
  697. int len;
  698. if(mv==0) len= mbMotionVectorTable[0][1];
  699. else{
  700. int val, bit_size, range, code;
  701. bit_size = s->f_code - 1;
  702. range = 1 << bit_size;
  703. val=mv;
  704. if (val < 0)
  705. val = -val;
  706. val--;
  707. code = (val >> bit_size) + 1;
  708. if(code<17){
  709. len= mbMotionVectorTable[code][1] + 1 + bit_size;
  710. }else{
  711. len= mbMotionVectorTable[16][1] + 2 + bit_size;
  712. }
  713. }
  714. mv_penalty[f_code][mv+MAX_MV]= len;
  715. }
  716. }
  717. for(f_code=MAX_FCODE; f_code>0; f_code--){
  718. for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
  719. fcode_tab[mv+MAX_MV]= f_code;
  720. }
  721. }
  722. }
  723. s->me.mv_penalty= mv_penalty;
  724. s->fcode_tab= fcode_tab;
  725. if(s->codec_id == CODEC_ID_MPEG1VIDEO){
  726. s->min_qcoeff=-255;
  727. s->max_qcoeff= 255;
  728. }else{
  729. s->min_qcoeff=-2047;
  730. s->max_qcoeff= 2047;
  731. }
  732. s->intra_ac_vlc_length=
  733. s->inter_ac_vlc_length=
  734. s->intra_ac_vlc_last_length=
  735. s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
  736. }
  737. static inline void encode_dc(MpegEncContext *s, int diff, int component)
  738. {
  739. if (component == 0) {
  740. put_bits(
  741. &s->pb,
  742. mpeg1_lum_dc_uni[diff+255]&0xFF,
  743. mpeg1_lum_dc_uni[diff+255]>>8);
  744. } else {
  745. put_bits(
  746. &s->pb,
  747. mpeg1_chr_dc_uni[diff+255]&0xFF,
  748. mpeg1_chr_dc_uni[diff+255]>>8);
  749. }
  750. }
  751. static void mpeg1_encode_block(MpegEncContext *s,
  752. DCTELEM *block,
  753. int n)
  754. {
  755. int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
  756. int code, component;
  757. // RLTable *rl = &rl_mpeg1;
  758. last_index = s->block_last_index[n];
  759. /* DC coef */
  760. if (s->mb_intra) {
  761. component = (n <= 3 ? 0 : n - 4 + 1);
  762. dc = block[0]; /* overflow is impossible */
  763. diff = dc - s->last_dc[component];
  764. encode_dc(s, diff, component);
  765. s->last_dc[component] = dc;
  766. i = 1;
  767. /*
  768. if (s->intra_vlc_format)
  769. rl = &rl_mpeg2;
  770. else
  771. rl = &rl_mpeg1;
  772. */
  773. } else {
  774. /* encode the first coefficient : needs to be done here because
  775. it is handled slightly differently */
  776. level = block[0];
  777. if (abs(level) == 1) {
  778. code = ((uint32_t)level >> 31); /* the sign bit */
  779. put_bits(&s->pb, 2, code | 0x02);
  780. i = 1;
  781. } else {
  782. i = 0;
  783. last_non_zero = -1;
  784. goto next_coef;
  785. }
  786. }
  787. /* now quantify & encode AC coefs */
  788. last_non_zero = i - 1;
  789. for(;i<=last_index;i++) {
  790. j = s->intra_scantable.permutated[i];
  791. level = block[j];
  792. next_coef:
  793. #if 0
  794. if (level != 0)
  795. dprintf("level[%d]=%d\n", i, level);
  796. #endif
  797. /* encode using VLC */
  798. if (level != 0) {
  799. run = i - last_non_zero - 1;
  800. alevel= level;
  801. MASK_ABS(sign, alevel)
  802. sign&=1;
  803. // code = get_rl_index(rl, 0, run, alevel);
  804. if (alevel <= mpeg1_max_level[0][run]){
  805. code= mpeg1_index_run[0][run] + alevel - 1;
  806. /* store the vlc & sign at once */
  807. put_bits(&s->pb, mpeg1_vlc[code][1]+1, (mpeg1_vlc[code][0]<<1) + sign);
  808. } else {
  809. /* escape seems to be pretty rare <5% so i dont optimize it */
  810. put_bits(&s->pb, mpeg1_vlc[111/*rl->n*/][1], mpeg1_vlc[111/*rl->n*/][0]);
  811. /* escape: only clip in this case */
  812. put_bits(&s->pb, 6, run);
  813. if(s->codec_id == CODEC_ID_MPEG1VIDEO){
  814. if (alevel < 128) {
  815. put_bits(&s->pb, 8, level & 0xff);
  816. } else {
  817. if (level < 0) {
  818. put_bits(&s->pb, 16, 0x8001 + level + 255);
  819. } else {
  820. put_bits(&s->pb, 16, level & 0xffff);
  821. }
  822. }
  823. }else{
  824. put_bits(&s->pb, 12, level & 0xfff);
  825. }
  826. }
  827. last_non_zero = i;
  828. }
  829. }
  830. /* end of block */
  831. put_bits(&s->pb, 2, 0x2);
  832. }
  833. #endif //CONFIG_ENCODERS
  834. /******************************************/
  835. /* decoding */
  836. static VLC dc_lum_vlc;
  837. static VLC dc_chroma_vlc;
  838. static VLC mv_vlc;
  839. static VLC mbincr_vlc;
  840. static VLC mb_ptype_vlc;
  841. static VLC mb_btype_vlc;
  842. static VLC mb_pat_vlc;
  843. static void init_vlcs()
  844. {
  845. static int done = 0;
  846. if (!done) {
  847. done = 1;
  848. init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12,
  849. vlc_dc_lum_bits, 1, 1,
  850. vlc_dc_lum_code, 2, 2);
  851. init_vlc(&dc_chroma_vlc, DC_VLC_BITS, 12,
  852. vlc_dc_chroma_bits, 1, 1,
  853. vlc_dc_chroma_code, 2, 2);
  854. init_vlc(&mv_vlc, MV_VLC_BITS, 17,
  855. &mbMotionVectorTable[0][1], 2, 1,
  856. &mbMotionVectorTable[0][0], 2, 1);
  857. init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 36,
  858. &mbAddrIncrTable[0][1], 2, 1,
  859. &mbAddrIncrTable[0][0], 2, 1);
  860. init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
  861. &mbPatTable[0][1], 2, 1,
  862. &mbPatTable[0][0], 2, 1);
  863. init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
  864. &table_mb_ptype[0][1], 2, 1,
  865. &table_mb_ptype[0][0], 2, 1);
  866. init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
  867. &table_mb_btype[0][1], 2, 1,
  868. &table_mb_btype[0][0], 2, 1);
  869. init_rl(&rl_mpeg1);
  870. init_rl(&rl_mpeg2);
  871. init_2d_vlc_rl(&rl_mpeg1);
  872. init_2d_vlc_rl(&rl_mpeg2);
  873. }
  874. }
  875. static inline int get_dmv(MpegEncContext *s)
  876. {
  877. if(get_bits1(&s->gb))
  878. return 1 - (get_bits1(&s->gb) << 1);
  879. else
  880. return 0;
  881. }
  882. static inline int get_qscale(MpegEncContext *s)
  883. {
  884. int qscale = get_bits(&s->gb, 5);
  885. if (s->q_scale_type) {
  886. return non_linear_qscale[qscale];
  887. } else {
  888. return qscale << 1;
  889. }
  890. }
  891. /* motion type (for mpeg2) */
  892. #define MT_FIELD 1
  893. #define MT_FRAME 2
  894. #define MT_16X8 2
  895. #define MT_DMV 3
  896. static int mpeg_decode_mb(MpegEncContext *s,
  897. DCTELEM block[12][64])
  898. {
  899. int i, j, k, cbp, val, mb_type, motion_type;
  900. dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
  901. assert(s->mb_skiped==0);
  902. if (s->mb_skip_run-- != 0) {
  903. if(s->pict_type == I_TYPE){
  904. av_log(s->avctx, AV_LOG_ERROR, "skiped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
  905. return -1;
  906. }
  907. /* skip mb */
  908. s->mb_intra = 0;
  909. for(i=0;i<12;i++)
  910. s->block_last_index[i] = -1;
  911. if(s->picture_structure == PICT_FRAME)
  912. s->mv_type = MV_TYPE_16X16;
  913. else
  914. s->mv_type = MV_TYPE_FIELD;
  915. if (s->pict_type == P_TYPE) {
  916. /* if P type, zero motion vector is implied */
  917. s->mv_dir = MV_DIR_FORWARD;
  918. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  919. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  920. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  921. s->field_select[0][0]= s->picture_structure - 1;
  922. s->mb_skiped = 1;
  923. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
  924. } else {
  925. /* if B type, reuse previous vectors and directions */
  926. s->mv[0][0][0] = s->last_mv[0][0][0];
  927. s->mv[0][0][1] = s->last_mv[0][0][1];
  928. s->mv[1][0][0] = s->last_mv[1][0][0];
  929. s->mv[1][0][1] = s->last_mv[1][0][1];
  930. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
  931. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1] | MB_TYPE_SKIP;
  932. // assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
  933. if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
  934. s->mb_skiped = 1;
  935. }
  936. return 0;
  937. }
  938. switch(s->pict_type) {
  939. default:
  940. case I_TYPE:
  941. if (get_bits1(&s->gb) == 0) {
  942. if (get_bits1(&s->gb) == 0){
  943. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
  944. return -1;
  945. }
  946. mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
  947. } else {
  948. mb_type = MB_TYPE_INTRA;
  949. }
  950. break;
  951. case P_TYPE:
  952. mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
  953. if (mb_type < 0){
  954. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
  955. return -1;
  956. }
  957. mb_type = ptype2mb_type[ mb_type ];
  958. break;
  959. case B_TYPE:
  960. mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
  961. if (mb_type < 0){
  962. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
  963. return -1;
  964. }
  965. mb_type = btype2mb_type[ mb_type ];
  966. break;
  967. }
  968. dprintf("mb_type=%x\n", mb_type);
  969. // motion_type = 0; /* avoid warning */
  970. if (IS_INTRA(mb_type)) {
  971. /* compute dct type */
  972. if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
  973. !s->frame_pred_frame_dct) {
  974. s->interlaced_dct = get_bits1(&s->gb);
  975. }
  976. if (IS_QUANT(mb_type))
  977. s->qscale = get_qscale(s);
  978. if (s->concealment_motion_vectors) {
  979. /* just parse them */
  980. if (s->picture_structure != PICT_FRAME)
  981. skip_bits1(&s->gb); /* field select */
  982. s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
  983. mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
  984. s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
  985. mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
  986. skip_bits1(&s->gb); /* marker */
  987. }else
  988. memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
  989. s->mb_intra = 1;
  990. #ifdef HAVE_XVMC
  991. //one 1 we memcpy blocks in xvmcvideo
  992. if(s->avctx->xvmc_acceleration > 1){
  993. XVMC_pack_pblocks(s,-1);//inter are always full blocks
  994. if(s->swap_uv){
  995. exchange_uv(s);
  996. }
  997. }
  998. #endif
  999. if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
  1000. for(i=0;i<4+(1<<s->chroma_format);i++) {
  1001. if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
  1002. return -1;
  1003. }
  1004. } else {
  1005. for(i=0;i<6;i++) {
  1006. if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0)
  1007. return -1;
  1008. }
  1009. }
  1010. } else {
  1011. if (mb_type & MB_TYPE_ZERO_MV){
  1012. assert(mb_type & MB_TYPE_CBP);
  1013. /* compute dct type */
  1014. if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
  1015. !s->frame_pred_frame_dct) {
  1016. s->interlaced_dct = get_bits1(&s->gb);
  1017. }
  1018. if (IS_QUANT(mb_type))
  1019. s->qscale = get_qscale(s);
  1020. s->mv_dir = MV_DIR_FORWARD;
  1021. if(s->picture_structure == PICT_FRAME)
  1022. s->mv_type = MV_TYPE_16X16;
  1023. else{
  1024. s->mv_type = MV_TYPE_FIELD;
  1025. mb_type |= MB_TYPE_INTERLACED;
  1026. s->field_select[0][0]= s->picture_structure - 1;
  1027. }
  1028. s->last_mv[0][0][0] = 0;
  1029. s->last_mv[0][0][1] = 0;
  1030. s->last_mv[0][1][0] = 0;
  1031. s->last_mv[0][1][1] = 0;
  1032. s->mv[0][0][0] = 0;
  1033. s->mv[0][0][1] = 0;
  1034. }else{
  1035. assert(mb_type & MB_TYPE_L0L1);
  1036. //FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
  1037. /* get additionnal motion vector type */
  1038. if (s->frame_pred_frame_dct)
  1039. motion_type = MT_FRAME;
  1040. else{
  1041. motion_type = get_bits(&s->gb, 2);
  1042. }
  1043. /* compute dct type */
  1044. if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
  1045. !s->frame_pred_frame_dct && HAS_CBP(mb_type)) {
  1046. s->interlaced_dct = get_bits1(&s->gb);
  1047. }
  1048. if (IS_QUANT(mb_type))
  1049. s->qscale = get_qscale(s);
  1050. /* motion vectors */
  1051. s->mv_dir = 0;
  1052. for(i=0;i<2;i++) {
  1053. if (USES_LIST(mb_type, i)) {
  1054. s->mv_dir |= (MV_DIR_FORWARD >> i);
  1055. dprintf("motion_type=%d\n", motion_type);
  1056. switch(motion_type) {
  1057. case MT_FRAME: /* or MT_16X8 */
  1058. if (s->picture_structure == PICT_FRAME) {
  1059. /* MT_FRAME */
  1060. mb_type |= MB_TYPE_16x16;
  1061. s->mv_type = MV_TYPE_16X16;
  1062. s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
  1063. mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
  1064. s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
  1065. mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
  1066. /* full_pel: only for mpeg1 */
  1067. if (s->full_pel[i]){
  1068. s->mv[i][0][0] <<= 1;
  1069. s->mv[i][0][1] <<= 1;
  1070. }
  1071. } else {
  1072. /* MT_16X8 */
  1073. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  1074. s->mv_type = MV_TYPE_16X8;
  1075. for(j=0;j<2;j++) {
  1076. s->field_select[i][j] = get_bits1(&s->gb);
  1077. for(k=0;k<2;k++) {
  1078. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  1079. s->last_mv[i][j][k]);
  1080. s->last_mv[i][j][k] = val;
  1081. s->mv[i][j][k] = val;
  1082. }
  1083. }
  1084. }
  1085. break;
  1086. case MT_FIELD:
  1087. s->mv_type = MV_TYPE_FIELD;
  1088. if (s->picture_structure == PICT_FRAME) {
  1089. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  1090. for(j=0;j<2;j++) {
  1091. s->field_select[i][j] = get_bits1(&s->gb);
  1092. val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  1093. s->last_mv[i][j][0]);
  1094. s->last_mv[i][j][0] = val;
  1095. s->mv[i][j][0] = val;
  1096. dprintf("fmx=%d\n", val);
  1097. val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  1098. s->last_mv[i][j][1] >> 1);
  1099. s->last_mv[i][j][1] = val << 1;
  1100. s->mv[i][j][1] = val;
  1101. dprintf("fmy=%d\n", val);
  1102. }
  1103. } else {
  1104. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  1105. s->field_select[i][0] = get_bits1(&s->gb);
  1106. for(k=0;k<2;k++) {
  1107. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  1108. s->last_mv[i][0][k]);
  1109. s->last_mv[i][0][k] = val;
  1110. s->last_mv[i][1][k] = val;
  1111. s->mv[i][0][k] = val;
  1112. }
  1113. }
  1114. break;
  1115. case MT_DMV:
  1116. {
  1117. int dmx, dmy, mx, my, m;
  1118. mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  1119. s->last_mv[i][0][0]);
  1120. s->last_mv[i][0][0] = mx;
  1121. s->last_mv[i][1][0] = mx;
  1122. dmx = get_dmv(s);
  1123. my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  1124. s->last_mv[i][0][1] >> 1);
  1125. dmy = get_dmv(s);
  1126. s->mv_type = MV_TYPE_DMV;
  1127. s->last_mv[i][0][1] = my<<1;
  1128. s->last_mv[i][1][1] = my<<1;
  1129. s->mv[i][0][0] = mx;
  1130. s->mv[i][0][1] = my;
  1131. s->mv[i][1][0] = mx;//not used
  1132. s->mv[i][1][1] = my;//not used
  1133. if (s->picture_structure == PICT_FRAME) {
  1134. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  1135. //m = 1 + 2 * s->top_field_first;
  1136. m = s->top_field_first ? 1 : 3;
  1137. /* top -> top pred */
  1138. s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  1139. s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
  1140. m = 4 - m;
  1141. s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  1142. s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
  1143. } else {
  1144. mb_type |= MB_TYPE_16x16;
  1145. s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
  1146. s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
  1147. if(s->picture_structure == PICT_TOP_FIELD)
  1148. s->mv[i][2][1]--;
  1149. else
  1150. s->mv[i][2][1]++;
  1151. }
  1152. }
  1153. break;
  1154. default:
  1155. av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
  1156. return -1;
  1157. }
  1158. }
  1159. }
  1160. }
  1161. s->mb_intra = 0;
  1162. if (HAS_CBP(mb_type)) {
  1163. cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
  1164. if (cbp < 0 || ((cbp == 0) && (s->chroma_format < 2)) ){
  1165. av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
  1166. return -1;
  1167. }
  1168. if(s->chroma_format == 2){//CHROMA422
  1169. cbp|= ( get_bits(&s->gb,2) ) << 6;
  1170. }else
  1171. if(s->chroma_format > 2){//CHROMA444
  1172. cbp|= ( get_bits(&s->gb,6) ) << 6;
  1173. }
  1174. #ifdef HAVE_XVMC
  1175. //on 1 we memcpy blocks in xvmcvideo
  1176. if(s->avctx->xvmc_acceleration > 1){
  1177. XVMC_pack_pblocks(s,cbp);
  1178. if(s->swap_uv){
  1179. exchange_uv(s);
  1180. }
  1181. }
  1182. #endif
  1183. if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
  1184. for(i=0;i<6;i++) {
  1185. if (cbp & (1<<(5-i)) ) {
  1186. if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
  1187. return -1;
  1188. } else {
  1189. s->block_last_index[i] = -1;
  1190. }
  1191. }
  1192. if (s->chroma_format >= 2) {
  1193. if (s->chroma_format == 2) {//CHROMA_422)
  1194. for(i=6;i<8;i++) {
  1195. if (cbp & (1<<(6+7-i)) ) {
  1196. if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
  1197. return -1;
  1198. } else {
  1199. s->block_last_index[i] = -1;
  1200. }
  1201. }
  1202. }else{ /*CHROMA_444*/
  1203. for(i=6;i<12;i++) {
  1204. if (cbp & (1<<(6+11-i)) ) {
  1205. if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
  1206. return -1;
  1207. } else {
  1208. s->block_last_index[i] = -1;
  1209. }
  1210. }
  1211. }
  1212. }
  1213. } else {
  1214. for(i=0;i<6;i++) {
  1215. if (cbp & 32) {
  1216. if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
  1217. return -1;
  1218. } else {
  1219. s->block_last_index[i] = -1;
  1220. }
  1221. cbp+=cbp;
  1222. }
  1223. }
  1224. }else{
  1225. for(i=0;i<6;i++)
  1226. s->block_last_index[i] = -1;
  1227. }
  1228. }
  1229. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
  1230. return 0;
  1231. }
  1232. /* as h263, but only 17 codes */
  1233. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
  1234. {
  1235. int code, sign, val, l, shift;
  1236. code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
  1237. if (code == 0) {
  1238. return pred;
  1239. }
  1240. if (code < 0) {
  1241. return 0xffff;
  1242. }
  1243. sign = get_bits1(&s->gb);
  1244. shift = fcode - 1;
  1245. val = code;
  1246. if (shift) {
  1247. val = (val - 1) << shift;
  1248. val |= get_bits(&s->gb, shift);
  1249. val++;
  1250. }
  1251. if (sign)
  1252. val = -val;
  1253. val += pred;
  1254. /* modulo decoding */
  1255. l = 1 << (shift+4);
  1256. val = ((val + l)&(l*2-1)) - l;
  1257. return val;
  1258. }
  1259. static inline int decode_dc(GetBitContext *gb, int component)
  1260. {
  1261. int code, diff;
  1262. if (component == 0) {
  1263. code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
  1264. } else {
  1265. code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
  1266. }
  1267. if (code < 0){
  1268. av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
  1269. return 0xffff;
  1270. }
  1271. if (code == 0) {
  1272. diff = 0;
  1273. } else {
  1274. diff = get_xbits(gb, code);
  1275. }
  1276. return diff;
  1277. }
  1278. static inline int mpeg1_decode_block_intra(MpegEncContext *s,
  1279. DCTELEM *block,
  1280. int n)
  1281. {
  1282. int level, dc, diff, i, j, run;
  1283. int component;
  1284. RLTable *rl = &rl_mpeg1;
  1285. uint8_t * const scantable= s->intra_scantable.permutated;
  1286. const uint16_t *quant_matrix= s->intra_matrix;
  1287. const int qscale= s->qscale;
  1288. /* DC coef */
  1289. component = (n <= 3 ? 0 : n - 4 + 1);
  1290. diff = decode_dc(&s->gb, component);
  1291. if (diff >= 0xffff)
  1292. return -1;
  1293. dc = s->last_dc[component];
  1294. dc += diff;
  1295. s->last_dc[component] = dc;
  1296. block[0] = dc<<3;
  1297. dprintf("dc=%d diff=%d\n", dc, diff);
  1298. i = 0;
  1299. {
  1300. OPEN_READER(re, &s->gb);
  1301. /* now quantify & encode AC coefs */
  1302. for(;;) {
  1303. UPDATE_CACHE(re, &s->gb);
  1304. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  1305. if(level == 127){
  1306. break;
  1307. } else if(level != 0) {
  1308. i += run;
  1309. j = scantable[i];
  1310. level= (level*qscale*quant_matrix[j])>>4;
  1311. level= (level-1)|1;
  1312. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1313. LAST_SKIP_BITS(re, &s->gb, 1);
  1314. } else {
  1315. /* escape */
  1316. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1317. UPDATE_CACHE(re, &s->gb);
  1318. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  1319. if (level == -128) {
  1320. level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
  1321. } else if (level == 0) {
  1322. level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
  1323. }
  1324. i += run;
  1325. j = scantable[i];
  1326. if(level<0){
  1327. level= -level;
  1328. level= (level*qscale*quant_matrix[j])>>4;
  1329. level= (level-1)|1;
  1330. level= -level;
  1331. }else{
  1332. level= (level*qscale*quant_matrix[j])>>4;
  1333. level= (level-1)|1;
  1334. }
  1335. }
  1336. if (i > 63){
  1337. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1338. return -1;
  1339. }
  1340. block[j] = level;
  1341. }
  1342. CLOSE_READER(re, &s->gb);
  1343. }
  1344. s->block_last_index[n] = i;
  1345. return 0;
  1346. }
  1347. static inline int mpeg1_decode_block_inter(MpegEncContext *s,
  1348. DCTELEM *block,
  1349. int n)
  1350. {
  1351. int level, i, j, run;
  1352. RLTable *rl = &rl_mpeg1;
  1353. uint8_t * const scantable= s->intra_scantable.permutated;
  1354. const uint16_t *quant_matrix= s->inter_matrix;
  1355. const int qscale= s->qscale;
  1356. {
  1357. int v;
  1358. OPEN_READER(re, &s->gb);
  1359. i = -1;
  1360. /* special case for the first coef. no need to add a second vlc table */
  1361. UPDATE_CACHE(re, &s->gb);
  1362. v= SHOW_UBITS(re, &s->gb, 2);
  1363. if (v & 2) {
  1364. LAST_SKIP_BITS(re, &s->gb, 2);
  1365. level= (3*qscale*quant_matrix[0])>>5;
  1366. level= (level-1)|1;
  1367. if(v&1)
  1368. level= -level;
  1369. block[0] = level;
  1370. i++;
  1371. }
  1372. /* now quantify & encode AC coefs */
  1373. for(;;) {
  1374. UPDATE_CACHE(re, &s->gb);
  1375. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  1376. if(level == 127){
  1377. break;
  1378. } else if(level != 0) {
  1379. i += run;
  1380. j = scantable[i];
  1381. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1382. level= (level-1)|1;
  1383. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1384. LAST_SKIP_BITS(re, &s->gb, 1);
  1385. } else {
  1386. /* escape */
  1387. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1388. UPDATE_CACHE(re, &s->gb);
  1389. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  1390. if (level == -128) {
  1391. level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
  1392. } else if (level == 0) {
  1393. level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
  1394. }
  1395. i += run;
  1396. j = scantable[i];
  1397. if(level<0){
  1398. level= -level;
  1399. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1400. level= (level-1)|1;
  1401. level= -level;
  1402. }else{
  1403. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1404. level= (level-1)|1;
  1405. }
  1406. }
  1407. if (i > 63){
  1408. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1409. return -1;
  1410. }
  1411. block[j] = level;
  1412. }
  1413. CLOSE_READER(re, &s->gb);
  1414. }
  1415. s->block_last_index[n] = i;
  1416. return 0;
  1417. }
  1418. /* Also does unquantization here, since I will never support mpeg2
  1419. encoding */
  1420. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
  1421. DCTELEM *block,
  1422. int n)
  1423. {
  1424. int level, i, j, run;
  1425. RLTable *rl = &rl_mpeg1;
  1426. uint8_t * const scantable= s->intra_scantable.permutated;
  1427. const uint16_t *quant_matrix;
  1428. const int qscale= s->qscale;
  1429. int mismatch;
  1430. mismatch = 1;
  1431. {
  1432. int v;
  1433. OPEN_READER(re, &s->gb);
  1434. i = -1;
  1435. if (n < 4)
  1436. quant_matrix = s->inter_matrix;
  1437. else
  1438. quant_matrix = s->chroma_inter_matrix;
  1439. /* special case for the first coef. no need to add a second vlc table */
  1440. UPDATE_CACHE(re, &s->gb);
  1441. v= SHOW_UBITS(re, &s->gb, 2);
  1442. if (v & 2) {
  1443. LAST_SKIP_BITS(re, &s->gb, 2);
  1444. level= (3*qscale*quant_matrix[0])>>5;
  1445. if(v&1)
  1446. level= -level;
  1447. block[0] = level;
  1448. mismatch ^= level;
  1449. i++;
  1450. }
  1451. /* now quantify & encode AC coefs */
  1452. for(;;) {
  1453. UPDATE_CACHE(re, &s->gb);
  1454. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  1455. if(level == 127){
  1456. break;
  1457. } else if(level != 0) {
  1458. i += run;
  1459. j = scantable[i];
  1460. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1461. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1462. LAST_SKIP_BITS(re, &s->gb, 1);
  1463. } else {
  1464. /* escape */
  1465. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1466. UPDATE_CACHE(re, &s->gb);
  1467. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1468. i += run;
  1469. j = scantable[i];
  1470. if(level<0){
  1471. level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
  1472. level= -level;
  1473. }else{
  1474. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1475. }
  1476. }
  1477. if (i > 63){
  1478. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1479. return -1;
  1480. }
  1481. mismatch ^= level;
  1482. block[j] = level;
  1483. }
  1484. CLOSE_READER(re, &s->gb);
  1485. }
  1486. block[63] ^= (mismatch & 1);
  1487. s->block_last_index[n] = i;
  1488. return 0;
  1489. }
  1490. static inline int mpeg2_decode_block_intra(MpegEncContext *s,
  1491. DCTELEM *block,
  1492. int n)
  1493. {
  1494. int level, dc, diff, i, j, run;
  1495. int component;
  1496. RLTable *rl;
  1497. uint8_t * const scantable= s->intra_scantable.permutated;
  1498. const uint16_t *quant_matrix;
  1499. const int qscale= s->qscale;
  1500. int mismatch;
  1501. /* DC coef */
  1502. if (n < 4){
  1503. quant_matrix = s->intra_matrix;
  1504. component = 0;
  1505. }else{
  1506. quant_matrix = s->chroma_intra_matrix;
  1507. component = (n&1) + 1;
  1508. }
  1509. diff = decode_dc(&s->gb, component);
  1510. if (diff >= 0xffff)
  1511. return -1;
  1512. dc = s->last_dc[component];
  1513. dc += diff;
  1514. s->last_dc[component] = dc;
  1515. block[0] = dc << (3 - s->intra_dc_precision);
  1516. dprintf("dc=%d\n", block[0]);
  1517. mismatch = block[0] ^ 1;
  1518. i = 0;
  1519. if (s->intra_vlc_format)
  1520. rl = &rl_mpeg2;
  1521. else
  1522. rl = &rl_mpeg1;
  1523. {
  1524. OPEN_READER(re, &s->gb);
  1525. /* now quantify & encode AC coefs */
  1526. for(;;) {
  1527. UPDATE_CACHE(re, &s->gb);
  1528. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  1529. if(level == 127){
  1530. break;
  1531. } else if(level != 0) {
  1532. i += run;
  1533. j = scantable[i];
  1534. level= (level*qscale*quant_matrix[j])>>4;
  1535. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1536. LAST_SKIP_BITS(re, &s->gb, 1);
  1537. } else {
  1538. /* escape */
  1539. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1540. UPDATE_CACHE(re, &s->gb);
  1541. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1542. i += run;
  1543. j = scantable[i];
  1544. if(level<0){
  1545. level= (-level*qscale*quant_matrix[j])>>4;
  1546. level= -level;
  1547. }else{
  1548. level= (level*qscale*quant_matrix[j])>>4;
  1549. }
  1550. }
  1551. if (i > 63){
  1552. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1553. return -1;
  1554. }
  1555. mismatch^= level;
  1556. block[j] = level;
  1557. }
  1558. CLOSE_READER(re, &s->gb);
  1559. }
  1560. block[63]^= mismatch&1;
  1561. s->block_last_index[n] = i;
  1562. return 0;
  1563. }
  1564. typedef struct Mpeg1Context {
  1565. MpegEncContext mpeg_enc_ctx;
  1566. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  1567. int repeat_field; /* true if we must repeat the field */
  1568. int display_weight;
  1569. int display_height;
  1570. AVPanScan pan_scan; /** some temporary storage for the panscan */
  1571. int slice_count;
  1572. int swap_uv;//indicate VCR2
  1573. int save_aspect_info;
  1574. } Mpeg1Context;
  1575. static int mpeg_decode_init(AVCodecContext *avctx)
  1576. {
  1577. Mpeg1Context *s = avctx->priv_data;
  1578. s->mpeg_enc_ctx.avctx= avctx;
  1579. s->mpeg_enc_ctx.flags= avctx->flags;
  1580. s->mpeg_enc_ctx.flags2= avctx->flags2;
  1581. common_init(&s->mpeg_enc_ctx);
  1582. init_vlcs();
  1583. s->mpeg_enc_ctx_allocated = 0;
  1584. s->mpeg_enc_ctx.picture_number = 0;
  1585. s->repeat_field = 0;
  1586. s->mpeg_enc_ctx.codec_id= avctx->codec->id;
  1587. return 0;
  1588. }
  1589. static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
  1590. const uint8_t *new_perm){
  1591. uint16_t temp_matrix[64];
  1592. int i;
  1593. memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
  1594. for(i=0;i<64;i++){
  1595. matrix[new_perm[i]] = temp_matrix[old_perm[i]];
  1596. }
  1597. }
  1598. //Call this function when we know all parameters
  1599. //it may be called in different places for mpeg1 and mpeg2
  1600. static int mpeg_decode_postinit(AVCodecContext *avctx){
  1601. Mpeg1Context *s1 = avctx->priv_data;
  1602. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1603. uint8_t old_permutation[64];
  1604. if (
  1605. (s1->mpeg_enc_ctx_allocated == 0)||
  1606. avctx->width != s->width ||
  1607. avctx->height != s->height||
  1608. // s1->save_aspect_info != avctx->aspect_ratio_info||
  1609. 0)
  1610. {
  1611. if (s1->mpeg_enc_ctx_allocated) {
  1612. MPV_common_end(s);
  1613. }
  1614. if( (s->width == 0 )||(s->height == 0))
  1615. return -2;
  1616. avctx->width = s->width;
  1617. avctx->height = s->height;
  1618. avctx->bit_rate = s->bit_rate;
  1619. s1->save_aspect_info = s->aspect_ratio_info;
  1620. //low_delay may be forced, in this case we will have B frames
  1621. //that behave like P frames
  1622. avctx->has_b_frames = !(s->low_delay);
  1623. if(avctx->sub_id==1){//s->codec_id==avctx->codec_id==CODEC_ID
  1624. //mpeg1 fps
  1625. avctx->frame_rate = frame_rate_tab[s->frame_rate_index].num;
  1626. avctx->frame_rate_base= frame_rate_tab[s->frame_rate_index].den;
  1627. //mpeg1 aspect
  1628. avctx->sample_aspect_ratio= av_d2q(
  1629. 1.0/mpeg1_aspect[s->aspect_ratio_info], 255);
  1630. }else{//mpeg2
  1631. //mpeg2 fps
  1632. av_reduce(
  1633. &s->avctx->frame_rate,
  1634. &s->avctx->frame_rate_base,
  1635. frame_rate_tab[s->frame_rate_index].num * (s->frame_rate_ext_n+1),
  1636. frame_rate_tab[s->frame_rate_index].den * (s->frame_rate_ext_d+1),
  1637. 1<<30);
  1638. //mpeg2 aspect
  1639. if(s->aspect_ratio_info > 1){
  1640. if( (s1->display_weight == 0 )||(s1->display_height == 0) ){
  1641. s->avctx->sample_aspect_ratio=
  1642. mpeg2_aspect[s->aspect_ratio_info];
  1643. }else{
  1644. s->avctx->sample_aspect_ratio=
  1645. av_div_q(
  1646. mpeg2_aspect[s->aspect_ratio_info],
  1647. (AVRational){s1->display_weight, s1->display_height}
  1648. );
  1649. }
  1650. }else{
  1651. s->avctx->sample_aspect_ratio=
  1652. av_div_q(
  1653. mpeg2_aspect[s->aspect_ratio_info],
  1654. (AVRational){s->width, s->height}
  1655. );
  1656. }
  1657. }//mpeg2
  1658. if(avctx->xvmc_acceleration){
  1659. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
  1660. }else{
  1661. if(s->chroma_format < 2){
  1662. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
  1663. }else
  1664. if(s->chroma_format == 2){
  1665. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_422);
  1666. }else
  1667. if(s->chroma_format > 2){
  1668. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_444);
  1669. }
  1670. }
  1671. //until then pix_fmt may be changed right after codec init
  1672. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
  1673. if( avctx->idct_algo == FF_IDCT_AUTO )
  1674. avctx->idct_algo = FF_IDCT_SIMPLE;
  1675. //quantization matrixes may need reordering
  1676. //if dct permutation is changed
  1677. memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
  1678. if (MPV_common_init(s) < 0)
  1679. return -2;
  1680. quant_matrix_rebuild(s->intra_matrix, old_permutation,s->dsp.idct_permutation);
  1681. quant_matrix_rebuild(s->inter_matrix, old_permutation,s->dsp.idct_permutation);
  1682. quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
  1683. quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
  1684. s1->mpeg_enc_ctx_allocated = 1;
  1685. }
  1686. return 0;
  1687. }
  1688. /* return the 8 bit start code value and update the search
  1689. state. Return -1 if no start code found */
  1690. static int find_start_code(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
  1691. {
  1692. const uint8_t *buf_ptr= *pbuf_ptr;
  1693. buf_ptr++; //gurantees that -1 is within the array
  1694. buf_end -= 2; // gurantees that +2 is within the array
  1695. while (buf_ptr < buf_end) {
  1696. if(*buf_ptr==0){
  1697. while(buf_ptr < buf_end && buf_ptr[1]==0)
  1698. buf_ptr++;
  1699. if(buf_ptr[-1] == 0 && buf_ptr[1] == 1){
  1700. *pbuf_ptr = buf_ptr+3;
  1701. return buf_ptr[2] + 0x100;
  1702. }
  1703. }
  1704. buf_ptr += 2;
  1705. }
  1706. buf_end += 2; //undo the hack above
  1707. *pbuf_ptr = buf_end;
  1708. return -1;
  1709. }
  1710. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1711. const uint8_t *buf, int buf_size)
  1712. {
  1713. Mpeg1Context *s1 = avctx->priv_data;
  1714. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1715. int ref, f_code, vbv_delay;
  1716. if(mpeg_decode_postinit(s->avctx) < 0)
  1717. return -2;
  1718. init_get_bits(&s->gb, buf, buf_size*8);
  1719. ref = get_bits(&s->gb, 10); /* temporal ref */
  1720. s->pict_type = get_bits(&s->gb, 3);
  1721. vbv_delay= get_bits(&s->gb, 16);
  1722. if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
  1723. s->full_pel[0] = get_bits1(&s->gb);
  1724. f_code = get_bits(&s->gb, 3);
  1725. if (f_code == 0)
  1726. return -1;
  1727. s->mpeg_f_code[0][0] = f_code;
  1728. s->mpeg_f_code[0][1] = f_code;
  1729. }
  1730. if (s->pict_type == B_TYPE) {
  1731. s->full_pel[1] = get_bits1(&s->gb);
  1732. f_code = get_bits(&s->gb, 3);
  1733. if (f_code == 0)
  1734. return -1;
  1735. s->mpeg_f_code[1][0] = f_code;
  1736. s->mpeg_f_code[1][1] = f_code;
  1737. }
  1738. s->current_picture.pict_type= s->pict_type;
  1739. s->current_picture.key_frame= s->pict_type == I_TYPE;
  1740. // if(avctx->debug & FF_DEBUG_PICT_INFO)
  1741. // av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d\n", vbv_delay, ref);
  1742. s->y_dc_scale = 8;
  1743. s->c_dc_scale = 8;
  1744. s->first_slice = 1;
  1745. return 0;
  1746. }
  1747. static void mpeg_decode_sequence_extension(MpegEncContext *s)
  1748. {
  1749. int horiz_size_ext, vert_size_ext;
  1750. int bit_rate_ext;
  1751. int level, profile;
  1752. skip_bits(&s->gb, 1); /* profil and level esc*/
  1753. profile= get_bits(&s->gb, 3);
  1754. level= get_bits(&s->gb, 4);
  1755. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1756. s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
  1757. horiz_size_ext = get_bits(&s->gb, 2);
  1758. vert_size_ext = get_bits(&s->gb, 2);
  1759. s->width |= (horiz_size_ext << 12);
  1760. s->height |= (vert_size_ext << 12);
  1761. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1762. s->bit_rate += (bit_rate_ext << 12) * 400;
  1763. skip_bits1(&s->gb); /* marker */
  1764. s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
  1765. s->low_delay = get_bits1(&s->gb);
  1766. if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
  1767. s->frame_rate_ext_n = get_bits(&s->gb, 2);
  1768. s->frame_rate_ext_d = get_bits(&s->gb, 5);
  1769. dprintf("sequence extension\n");
  1770. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
  1771. s->avctx->sub_id = 2; /* indicates mpeg2 found */
  1772. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1773. av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
  1774. profile, level, s->avctx->rc_buffer_size, s->bit_rate);
  1775. }
  1776. static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
  1777. {
  1778. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1779. int color_description, w, h;
  1780. skip_bits(&s->gb, 3); /* video format */
  1781. color_description= get_bits1(&s->gb);
  1782. if(color_description){
  1783. skip_bits(&s->gb, 8); /* color primaries */
  1784. skip_bits(&s->gb, 8); /* transfer_characteristics */
  1785. skip_bits(&s->gb, 8); /* matrix_coefficients */
  1786. }
  1787. w= get_bits(&s->gb, 14);
  1788. skip_bits(&s->gb, 1); //marker
  1789. h= get_bits(&s->gb, 14);
  1790. skip_bits(&s->gb, 1); //marker
  1791. s1->pan_scan.width= 16*w;
  1792. s1->pan_scan.height=16*h;
  1793. s1->display_weight = w;
  1794. s1->display_height = h;
  1795. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1796. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  1797. }
  1798. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  1799. {
  1800. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1801. int i;
  1802. for(i=0; i<1; i++){ //FIXME count
  1803. s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
  1804. skip_bits(&s->gb, 1); //marker
  1805. s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
  1806. skip_bits(&s->gb, 1); //marker
  1807. }
  1808. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1809. av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
  1810. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  1811. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  1812. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
  1813. );
  1814. }
  1815. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1816. {
  1817. int i, v, j;
  1818. dprintf("matrix extension\n");
  1819. if (get_bits1(&s->gb)) {
  1820. for(i=0;i<64;i++) {
  1821. v = get_bits(&s->gb, 8);
  1822. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1823. s->intra_matrix[j] = v;
  1824. s->chroma_intra_matrix[j] = v;
  1825. }
  1826. }
  1827. if (get_bits1(&s->gb)) {
  1828. for(i=0;i<64;i++) {
  1829. v = get_bits(&s->gb, 8);
  1830. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1831. s->inter_matrix[j] = v;
  1832. s->chroma_inter_matrix[j] = v;
  1833. }
  1834. }
  1835. if (get_bits1(&s->gb)) {
  1836. for(i=0;i<64;i++) {
  1837. v = get_bits(&s->gb, 8);
  1838. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1839. s->chroma_intra_matrix[j] = v;
  1840. }
  1841. }
  1842. if (get_bits1(&s->gb)) {
  1843. for(i=0;i<64;i++) {
  1844. v = get_bits(&s->gb, 8);
  1845. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1846. s->chroma_inter_matrix[j] = v;
  1847. }
  1848. }
  1849. }
  1850. static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
  1851. {
  1852. s->full_pel[0] = s->full_pel[1] = 0;
  1853. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1854. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1855. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1856. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1857. s->intra_dc_precision = get_bits(&s->gb, 2);
  1858. s->picture_structure = get_bits(&s->gb, 2);
  1859. s->top_field_first = get_bits1(&s->gb);
  1860. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1861. s->concealment_motion_vectors = get_bits1(&s->gb);
  1862. s->q_scale_type = get_bits1(&s->gb);
  1863. s->intra_vlc_format = get_bits1(&s->gb);
  1864. s->alternate_scan = get_bits1(&s->gb);
  1865. s->repeat_first_field = get_bits1(&s->gb);
  1866. s->chroma_420_type = get_bits1(&s->gb);
  1867. s->progressive_frame = get_bits1(&s->gb);
  1868. if(s->picture_structure == PICT_FRAME)
  1869. s->first_field=0;
  1870. else{
  1871. s->first_field ^= 1;
  1872. memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
  1873. }
  1874. if(s->alternate_scan){
  1875. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  1876. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  1877. }else{
  1878. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  1879. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  1880. }
  1881. /* composite display not parsed */
  1882. dprintf("intra_dc_precision=%d\n", s->intra_dc_precision);
  1883. dprintf("picture_structure=%d\n", s->picture_structure);
  1884. dprintf("top field first=%d\n", s->top_field_first);
  1885. dprintf("repeat first field=%d\n", s->repeat_first_field);
  1886. dprintf("conceal=%d\n", s->concealment_motion_vectors);
  1887. dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
  1888. dprintf("alternate_scan=%d\n", s->alternate_scan);
  1889. dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1890. dprintf("progressive_frame=%d\n", s->progressive_frame);
  1891. }
  1892. static void mpeg_decode_extension(AVCodecContext *avctx,
  1893. const uint8_t *buf, int buf_size)
  1894. {
  1895. Mpeg1Context *s1 = avctx->priv_data;
  1896. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1897. int ext_type;
  1898. init_get_bits(&s->gb, buf, buf_size*8);
  1899. ext_type = get_bits(&s->gb, 4);
  1900. switch(ext_type) {
  1901. case 0x1:
  1902. mpeg_decode_sequence_extension(s);
  1903. break;
  1904. case 0x2:
  1905. mpeg_decode_sequence_display_extension(s1);
  1906. break;
  1907. case 0x3:
  1908. mpeg_decode_quant_matrix_extension(s);
  1909. break;
  1910. case 0x7:
  1911. mpeg_decode_picture_display_extension(s1);
  1912. break;
  1913. case 0x8:
  1914. mpeg_decode_picture_coding_extension(s);
  1915. break;
  1916. }
  1917. }
  1918. static void exchange_uv(MpegEncContext *s){
  1919. short * tmp;
  1920. tmp = s->pblocks[4];
  1921. s->pblocks[4] = s->pblocks[5];
  1922. s->pblocks[5] = tmp;
  1923. }
  1924. static int mpeg_field_start(MpegEncContext *s){
  1925. AVCodecContext *avctx= s->avctx;
  1926. Mpeg1Context *s1 = (Mpeg1Context*)s;
  1927. /* start frame decoding */
  1928. if(s->first_field || s->picture_structure==PICT_FRAME){
  1929. if(MPV_frame_start(s, avctx) < 0)
  1930. return -1;
  1931. ff_er_frame_start(s);
  1932. /* first check if we must repeat the frame */
  1933. s->current_picture_ptr->repeat_pict = 0;
  1934. if (s->repeat_first_field) {
  1935. if (s->progressive_sequence) {
  1936. if (s->top_field_first)
  1937. s->current_picture_ptr->repeat_pict = 4;
  1938. else
  1939. s->current_picture_ptr->repeat_pict = 2;
  1940. } else if (s->progressive_frame) {
  1941. s->current_picture_ptr->repeat_pict = 1;
  1942. }
  1943. }
  1944. *s->current_picture_ptr->pan_scan= s1->pan_scan;
  1945. }else{ //second field
  1946. int i;
  1947. if(!s->current_picture_ptr){
  1948. av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
  1949. return -1;
  1950. }
  1951. for(i=0; i<4; i++){
  1952. s->current_picture.data[i] = s->current_picture_ptr->data[i];
  1953. if(s->picture_structure == PICT_BOTTOM_FIELD){
  1954. s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
  1955. }
  1956. }
  1957. }
  1958. #ifdef HAVE_XVMC
  1959. // MPV_frame_start will call this function too,
  1960. // but we need to call it on every field
  1961. if(s->avctx->xvmc_acceleration)
  1962. XVMC_field_start(s,avctx);
  1963. #endif
  1964. return 0;
  1965. }
  1966. #define DECODE_SLICE_ERROR -1
  1967. #define DECODE_SLICE_OK 0
  1968. /**
  1969. * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
  1970. * @return DECODE_SLICE_ERROR if the slice is damaged<br>
  1971. * DECODE_SLICE_OK if this slice is ok<br>
  1972. */
  1973. static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
  1974. const uint8_t **buf, int buf_size)
  1975. {
  1976. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1977. AVCodecContext *avctx= s->avctx;
  1978. int ret;
  1979. const int field_pic= s->picture_structure != PICT_FRAME;
  1980. s->resync_mb_x=
  1981. s->resync_mb_y= -1;
  1982. if (mb_y >= s->mb_height){
  1983. av_log(s->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", s->mb_y, s->mb_height);
  1984. return -1;
  1985. }
  1986. init_get_bits(&s->gb, *buf, buf_size*8);
  1987. ff_mpeg1_clean_buffers(s);
  1988. s->interlaced_dct = 0;
  1989. s->qscale = get_qscale(s);
  1990. if(s->qscale == 0){
  1991. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  1992. return -1;
  1993. }
  1994. /* extra slice info */
  1995. while (get_bits1(&s->gb) != 0) {
  1996. skip_bits(&s->gb, 8);
  1997. }
  1998. s->mb_x=0;
  1999. for(;;) {
  2000. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  2001. if (code < 0){
  2002. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  2003. return -1;
  2004. }
  2005. if (code >= 33) {
  2006. if (code == 33) {
  2007. s->mb_x += 33;
  2008. }
  2009. /* otherwise, stuffing, nothing to do */
  2010. } else {
  2011. s->mb_x += code;
  2012. break;
  2013. }
  2014. }
  2015. s->resync_mb_x= s->mb_x;
  2016. s->resync_mb_y= s->mb_y= mb_y;
  2017. s->mb_skip_run= 0;
  2018. ff_init_block_index(s);
  2019. if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
  2020. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  2021. av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%2d%2d%2d%2d %s %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
  2022. 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],
  2023. s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")),
  2024. s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
  2025. s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
  2026. s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
  2027. }
  2028. }
  2029. for(;;) {
  2030. #ifdef HAVE_XVMC
  2031. //one 1 we memcpy blocks in xvmcvideo
  2032. if(s->avctx->xvmc_acceleration > 1)
  2033. XVMC_init_block(s);//set s->block
  2034. #endif
  2035. s->dsp.clear_blocks(s->block[0]);
  2036. ret = mpeg_decode_mb(s, s->block);
  2037. s->chroma_qscale= s->qscale;
  2038. dprintf("ret=%d\n", ret);
  2039. if (ret < 0)
  2040. return -1;
  2041. if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
  2042. const int wrap = field_pic ? 2*s->block_wrap[0] : s->block_wrap[0];
  2043. int xy = s->mb_x*2 + 1 + (s->mb_y*2 +1)*wrap;
  2044. int motion_x, motion_y, dir, i;
  2045. if(field_pic && !s->first_field)
  2046. xy += wrap/2;
  2047. for(i=0; i<2; i++){
  2048. for(dir=0; dir<2; dir++){
  2049. if (s->mb_intra || (dir==1 && s->pict_type != B_TYPE)) {
  2050. motion_x = motion_y = 0;
  2051. }else if (s->mv_type == MV_TYPE_16X16){
  2052. motion_x = s->mv[dir][0][0];
  2053. motion_y = s->mv[dir][0][1];
  2054. } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
  2055. motion_x = s->mv[dir][i][0];
  2056. motion_y = s->mv[dir][i][1];
  2057. }
  2058. s->current_picture.motion_val[dir][xy ][0] = motion_x;
  2059. s->current_picture.motion_val[dir][xy ][1] = motion_y;
  2060. s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
  2061. s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
  2062. }
  2063. xy += wrap;
  2064. }
  2065. }
  2066. s->dest[0] += 16;
  2067. s->dest[1] += 8;
  2068. s->dest[2] += 8;
  2069. MPV_decode_mb(s, s->block);
  2070. if (++s->mb_x >= s->mb_width) {
  2071. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  2072. s->mb_x = 0;
  2073. s->mb_y++;
  2074. if(s->mb_y<<field_pic >= s->mb_height){
  2075. int left= s->gb.size_in_bits - get_bits_count(&s->gb);
  2076. if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)))
  2077. || (avctx->error_resilience >= FF_ER_AGGRESSIVE && left>8)){
  2078. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d\n", left);
  2079. return -1;
  2080. }else
  2081. goto eos;
  2082. }
  2083. ff_init_block_index(s);
  2084. }
  2085. /* skip mb handling */
  2086. if (s->mb_skip_run == -1) {
  2087. /* read again increment */
  2088. s->mb_skip_run = 0;
  2089. for(;;) {
  2090. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  2091. if (code < 0){
  2092. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  2093. return -1;
  2094. }
  2095. if (code >= 33) {
  2096. if (code == 33) {
  2097. s->mb_skip_run += 33;
  2098. }else if(code == 35){
  2099. if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
  2100. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  2101. return -1;
  2102. }
  2103. goto eos; /* end of slice */
  2104. }
  2105. /* otherwise, stuffing, nothing to do */
  2106. } else {
  2107. s->mb_skip_run += code;
  2108. break;
  2109. }
  2110. }
  2111. }
  2112. }
  2113. eos: // end of slice
  2114. *buf += get_bits_count(&s->gb)/8 - 1;
  2115. //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  2116. return 0;
  2117. }
  2118. static int slice_decode_thread(AVCodecContext *c, void *arg){
  2119. MpegEncContext *s= arg;
  2120. const uint8_t *buf= s->gb.buffer;
  2121. int mb_y= s->start_mb_y;
  2122. s->error_count= 3*(s->end_mb_y - s->start_mb_y)*s->mb_width;
  2123. for(;;){
  2124. int start_code, ret;
  2125. ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
  2126. emms_c();
  2127. //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
  2128. //ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, s->start_mb_y, s->end_mb_y, s->error_count);
  2129. if(ret < 0){
  2130. if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
  2131. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
  2132. }else{
  2133. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
  2134. }
  2135. if(s->mb_y == s->end_mb_y)
  2136. return 0;
  2137. start_code = find_start_code(&buf, s->gb.buffer_end);
  2138. mb_y= start_code - SLICE_MIN_START_CODE;
  2139. if(mb_y < 0 || mb_y >= s->end_mb_y)
  2140. return -1;
  2141. }
  2142. return 0; //not reached
  2143. }
  2144. /**
  2145. * handles slice ends.
  2146. * @return 1 if it seems to be the last slice of
  2147. */
  2148. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  2149. {
  2150. Mpeg1Context *s1 = avctx->priv_data;
  2151. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2152. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  2153. return 0;
  2154. #ifdef HAVE_XVMC
  2155. if(s->avctx->xvmc_acceleration)
  2156. XVMC_field_end(s);
  2157. #endif
  2158. /* end of slice reached */
  2159. if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
  2160. /* end of image */
  2161. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
  2162. ff_er_frame_end(s);
  2163. MPV_frame_end(s);
  2164. if (s->pict_type == B_TYPE || s->low_delay) {
  2165. *pict= *(AVFrame*)s->current_picture_ptr;
  2166. ff_print_debug_info(s, pict);
  2167. } else {
  2168. s->picture_number++;
  2169. /* latency of 1 frame for I and P frames */
  2170. /* XXX: use another variable than picture_number */
  2171. if (s->last_picture_ptr != NULL) {
  2172. *pict= *(AVFrame*)s->last_picture_ptr;
  2173. ff_print_debug_info(s, pict);
  2174. }
  2175. }
  2176. return 1;
  2177. } else {
  2178. return 0;
  2179. }
  2180. }
  2181. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  2182. const uint8_t *buf, int buf_size)
  2183. {
  2184. Mpeg1Context *s1 = avctx->priv_data;
  2185. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2186. int i, v, j;
  2187. init_get_bits(&s->gb, buf, buf_size*8);
  2188. s->width = get_bits(&s->gb, 12);
  2189. s->height = get_bits(&s->gb, 12);
  2190. s->aspect_ratio_info= get_bits(&s->gb, 4);
  2191. if (s->aspect_ratio_info == 0)
  2192. return -1;
  2193. s->frame_rate_index = get_bits(&s->gb, 4);
  2194. if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
  2195. return -1;
  2196. s->bit_rate = get_bits(&s->gb, 18) * 400;
  2197. if (get_bits1(&s->gb) == 0) /* marker */
  2198. return -1;
  2199. if (s->width <= 0 || s->height <= 0)
  2200. return -1;
  2201. s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
  2202. skip_bits(&s->gb, 1);
  2203. /* get matrix */
  2204. if (get_bits1(&s->gb)) {
  2205. for(i=0;i<64;i++) {
  2206. v = get_bits(&s->gb, 8);
  2207. if(v==0){
  2208. av_log(s->avctx, AV_LOG_ERROR, "intra matrix damaged\n");
  2209. return -1;
  2210. }
  2211. j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2212. s->intra_matrix[j] = v;
  2213. s->chroma_intra_matrix[j] = v;
  2214. }
  2215. #ifdef DEBUG
  2216. dprintf("intra matrix present\n");
  2217. for(i=0;i<64;i++)
  2218. dprintf(" %d", s->intra_matrix[s->dsp.idct_permutation[i]);
  2219. printf("\n");
  2220. #endif
  2221. } else {
  2222. for(i=0;i<64;i++) {
  2223. j = s->dsp.idct_permutation[i];
  2224. v = ff_mpeg1_default_intra_matrix[i];
  2225. s->intra_matrix[j] = v;
  2226. s->chroma_intra_matrix[j] = v;
  2227. }
  2228. }
  2229. if (get_bits1(&s->gb)) {
  2230. for(i=0;i<64;i++) {
  2231. v = get_bits(&s->gb, 8);
  2232. if(v==0){
  2233. av_log(s->avctx, AV_LOG_ERROR, "inter matrix damaged\n");
  2234. return -1;
  2235. }
  2236. j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2237. s->inter_matrix[j] = v;
  2238. s->chroma_inter_matrix[j] = v;
  2239. }
  2240. #ifdef DEBUG
  2241. dprintf("non intra matrix present\n");
  2242. for(i=0;i<64;i++)
  2243. dprintf(" %d", s->inter_matrix[s->dsp.idct_permutation[i]);
  2244. printf("\n");
  2245. #endif
  2246. } else {
  2247. for(i=0;i<64;i++) {
  2248. int j= s->dsp.idct_permutation[i];
  2249. v = ff_mpeg1_default_non_intra_matrix[i];
  2250. s->inter_matrix[j] = v;
  2251. s->chroma_inter_matrix[j] = v;
  2252. }
  2253. }
  2254. if(show_bits(&s->gb, 23) != 0){
  2255. av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
  2256. return -1;
  2257. }
  2258. /* we set mpeg2 parameters so that it emulates mpeg1 */
  2259. s->progressive_sequence = 1;
  2260. s->progressive_frame = 1;
  2261. s->picture_structure = PICT_FRAME;
  2262. s->frame_pred_frame_dct = 1;
  2263. s->chroma_format = 1;
  2264. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
  2265. avctx->sub_id = 1; /* indicates mpeg1 */
  2266. s->out_format = FMT_MPEG1;
  2267. s->swap_uv = 0;//AFAIK VCR2 don't have SEQ_HEADER
  2268. if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
  2269. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  2270. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
  2271. s->avctx->rc_buffer_size, s->bit_rate);
  2272. return 0;
  2273. }
  2274. static int vcr2_init_sequence(AVCodecContext *avctx)
  2275. {
  2276. Mpeg1Context *s1 = avctx->priv_data;
  2277. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2278. int i, v;
  2279. /* start new mpeg1 context decoding */
  2280. s->out_format = FMT_MPEG1;
  2281. if (s1->mpeg_enc_ctx_allocated) {
  2282. MPV_common_end(s);
  2283. }
  2284. s->width = avctx->width;
  2285. s->height = avctx->height;
  2286. avctx->has_b_frames= 0; //true?
  2287. s->low_delay= 1;
  2288. if(avctx->xvmc_acceleration){
  2289. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
  2290. }else{
  2291. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
  2292. }
  2293. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
  2294. if( avctx->idct_algo == FF_IDCT_AUTO )
  2295. avctx->idct_algo = FF_IDCT_SIMPLE;
  2296. if (MPV_common_init(s) < 0)
  2297. return -1;
  2298. exchange_uv(s);//common init reset pblocks, so we swap them here
  2299. s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
  2300. s1->mpeg_enc_ctx_allocated = 1;
  2301. for(i=0;i<64;i++) {
  2302. int j= s->dsp.idct_permutation[i];
  2303. v = ff_mpeg1_default_intra_matrix[i];
  2304. s->intra_matrix[j] = v;
  2305. s->chroma_intra_matrix[j] = v;
  2306. v = ff_mpeg1_default_non_intra_matrix[i];
  2307. s->inter_matrix[j] = v;
  2308. s->chroma_inter_matrix[j] = v;
  2309. }
  2310. s->progressive_sequence = 1;
  2311. s->progressive_frame = 1;
  2312. s->picture_structure = PICT_FRAME;
  2313. s->frame_pred_frame_dct = 1;
  2314. s->chroma_format = 1;
  2315. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
  2316. avctx->sub_id = 2; /* indicates mpeg2 */
  2317. return 0;
  2318. }
  2319. static void mpeg_decode_user_data(AVCodecContext *avctx,
  2320. const uint8_t *buf, int buf_size)
  2321. {
  2322. const uint8_t *p;
  2323. int len, flags;
  2324. p = buf;
  2325. len = buf_size;
  2326. /* we parse the DTG active format information */
  2327. if (len >= 5 &&
  2328. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  2329. flags = p[4];
  2330. p += 5;
  2331. len -= 5;
  2332. if (flags & 0x80) {
  2333. /* skip event id */
  2334. if (len < 2)
  2335. return;
  2336. p += 2;
  2337. len -= 2;
  2338. }
  2339. if (flags & 0x40) {
  2340. if (len < 1)
  2341. return;
  2342. avctx->dtg_active_format = p[0] & 0x0f;
  2343. }
  2344. }
  2345. }
  2346. /**
  2347. * finds the end of the current frame in the bitstream.
  2348. * @return the position of the first byte of the next frame, or -1
  2349. */
  2350. static int mpeg1_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
  2351. ParseContext *pc= &s->parse_context;
  2352. int i;
  2353. uint32_t state;
  2354. state= pc->state;
  2355. i=0;
  2356. if(!pc->frame_start_found){
  2357. for(i=0; i<buf_size; i++){
  2358. state= (state<<8) | buf[i];
  2359. if(state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
  2360. i++;
  2361. pc->frame_start_found=1;
  2362. break;
  2363. }
  2364. }
  2365. }
  2366. if(pc->frame_start_found){
  2367. for(; i<buf_size; i++){
  2368. state= (state<<8) | buf[i];
  2369. if((state&0xFFFFFF00) == 0x100){
  2370. if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
  2371. pc->frame_start_found=0;
  2372. pc->state=-1;
  2373. return i-3;
  2374. }
  2375. }
  2376. }
  2377. }
  2378. pc->state= state;
  2379. return END_NOT_FOUND;
  2380. }
  2381. /* handle buffering and image synchronisation */
  2382. static int mpeg_decode_frame(AVCodecContext *avctx,
  2383. void *data, int *data_size,
  2384. uint8_t *buf, int buf_size)
  2385. {
  2386. Mpeg1Context *s = avctx->priv_data;
  2387. const uint8_t *buf_end;
  2388. const uint8_t *buf_ptr;
  2389. int ret, start_code, input_size;
  2390. AVFrame *picture = data;
  2391. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2392. dprintf("fill_buffer\n");
  2393. *data_size = 0;
  2394. /* special case for last picture */
  2395. if (buf_size == 0 && s2->low_delay==0 && s2->next_picture_ptr) {
  2396. *picture= *(AVFrame*)s2->next_picture_ptr;
  2397. s2->next_picture_ptr= NULL;
  2398. *data_size = sizeof(AVFrame);
  2399. return 0;
  2400. }
  2401. if(s2->flags&CODEC_FLAG_TRUNCATED){
  2402. int next= mpeg1_find_frame_end(s2, buf, buf_size);
  2403. if( ff_combine_frame(s2, next, &buf, &buf_size) < 0 )
  2404. return buf_size;
  2405. }
  2406. buf_ptr = buf;
  2407. buf_end = buf + buf_size;
  2408. #if 0
  2409. if (s->repeat_field % 2 == 1) {
  2410. s->repeat_field++;
  2411. //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
  2412. // s2->picture_number, s->repeat_field);
  2413. if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
  2414. *data_size = sizeof(AVPicture);
  2415. goto the_end;
  2416. }
  2417. }
  2418. #endif
  2419. if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == ff_get_fourcc("VCR2"))
  2420. vcr2_init_sequence(avctx);
  2421. s->slice_count= 0;
  2422. for(;;) {
  2423. /* find start next code */
  2424. start_code = find_start_code(&buf_ptr, buf_end);
  2425. if (start_code < 0){
  2426. if(s2->pict_type != B_TYPE || avctx->hurry_up==0){
  2427. if(avctx->thread_count > 1){
  2428. int i;
  2429. avctx->execute(avctx, slice_decode_thread, (void**)&(s2->thread_context[0]), NULL, s->slice_count);
  2430. for(i=0; i<s->slice_count; i++)
  2431. s2->error_count += s2->thread_context[i]->error_count;
  2432. }
  2433. if (slice_end(avctx, picture)) {
  2434. if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
  2435. *data_size = sizeof(AVPicture);
  2436. }
  2437. }
  2438. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  2439. }
  2440. input_size = buf_end - buf_ptr;
  2441. if(avctx->debug & FF_DEBUG_STARTCODE){
  2442. av_log(avctx, AV_LOG_DEBUG, "%3X at %d left %d\n", start_code, buf_ptr-buf, input_size);
  2443. }
  2444. /* prepare data for next start code */
  2445. switch(start_code) {
  2446. case SEQ_START_CODE:
  2447. mpeg1_decode_sequence(avctx, buf_ptr,
  2448. input_size);
  2449. break;
  2450. case PICTURE_START_CODE:
  2451. /* we have a complete image : we try to decompress it */
  2452. mpeg1_decode_picture(avctx,
  2453. buf_ptr, input_size);
  2454. break;
  2455. case EXT_START_CODE:
  2456. mpeg_decode_extension(avctx,
  2457. buf_ptr, input_size);
  2458. break;
  2459. case USER_START_CODE:
  2460. mpeg_decode_user_data(avctx,
  2461. buf_ptr, input_size);
  2462. break;
  2463. case GOP_START_CODE:
  2464. s2->first_field=0;
  2465. break;
  2466. default:
  2467. if (start_code >= SLICE_MIN_START_CODE &&
  2468. start_code <= SLICE_MAX_START_CODE) {
  2469. int mb_y= start_code - SLICE_MIN_START_CODE;
  2470. /* skip b frames if we dont have reference frames */
  2471. if(s2->last_picture_ptr==NULL && s2->pict_type==B_TYPE) break;
  2472. /* skip b frames if we are in a hurry */
  2473. if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
  2474. /* skip everything if we are in a hurry>=5 */
  2475. if(avctx->hurry_up>=5) break;
  2476. if (!s->mpeg_enc_ctx_allocated) break;
  2477. if(s2->first_slice){
  2478. s2->first_slice=0;
  2479. if(mpeg_field_start(s2) < 0)
  2480. return -1;
  2481. }
  2482. if(avctx->thread_count > 1){
  2483. int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
  2484. if(threshold <= mb_y){
  2485. MpegEncContext *thread_context= s2->thread_context[s->slice_count];
  2486. thread_context->start_mb_y= mb_y;
  2487. thread_context->end_mb_y = s2->mb_height;
  2488. if(s->slice_count){
  2489. s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
  2490. ff_update_duplicate_context(thread_context, s2);
  2491. }
  2492. init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
  2493. s->slice_count++;
  2494. }
  2495. buf_ptr += 2; //FIXME add minimum num of bytes per slice
  2496. }else{
  2497. ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
  2498. emms_c();
  2499. if(ret < 0){
  2500. if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
  2501. ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
  2502. }else{
  2503. ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, AC_END|DC_END|MV_END);
  2504. }
  2505. }
  2506. }
  2507. break;
  2508. }
  2509. }
  2510. }
  2511. static int mpeg_decode_end(AVCodecContext *avctx)
  2512. {
  2513. Mpeg1Context *s = avctx->priv_data;
  2514. if (s->mpeg_enc_ctx_allocated)
  2515. MPV_common_end(&s->mpeg_enc_ctx);
  2516. return 0;
  2517. }
  2518. AVCodec mpeg1video_decoder = {
  2519. "mpeg1video",
  2520. CODEC_TYPE_VIDEO,
  2521. CODEC_ID_MPEG1VIDEO,
  2522. sizeof(Mpeg1Context),
  2523. mpeg_decode_init,
  2524. NULL,
  2525. mpeg_decode_end,
  2526. mpeg_decode_frame,
  2527. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  2528. .flush= ff_mpeg_flush,
  2529. };
  2530. AVCodec mpeg2video_decoder = {
  2531. "mpeg2video",
  2532. CODEC_TYPE_VIDEO,
  2533. CODEC_ID_MPEG2VIDEO,
  2534. sizeof(Mpeg1Context),
  2535. mpeg_decode_init,
  2536. NULL,
  2537. mpeg_decode_end,
  2538. mpeg_decode_frame,
  2539. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  2540. .flush= ff_mpeg_flush,
  2541. };
  2542. //legacy decoder
  2543. AVCodec mpegvideo_decoder = {
  2544. "mpegvideo",
  2545. CODEC_TYPE_VIDEO,
  2546. CODEC_ID_MPEG2VIDEO,
  2547. sizeof(Mpeg1Context),
  2548. mpeg_decode_init,
  2549. NULL,
  2550. mpeg_decode_end,
  2551. mpeg_decode_frame,
  2552. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  2553. .flush= ff_mpeg_flush,
  2554. };
  2555. #ifdef CONFIG_ENCODERS
  2556. AVCodec mpeg1video_encoder = {
  2557. "mpeg1video",
  2558. CODEC_TYPE_VIDEO,
  2559. CODEC_ID_MPEG1VIDEO,
  2560. sizeof(MpegEncContext),
  2561. encode_init,
  2562. MPV_encode_picture,
  2563. MPV_encode_end,
  2564. .supported_framerates= frame_rate_tab+1,
  2565. };
  2566. #ifdef CONFIG_RISKY
  2567. AVCodec mpeg2video_encoder = {
  2568. "mpeg2video",
  2569. CODEC_TYPE_VIDEO,
  2570. CODEC_ID_MPEG2VIDEO,
  2571. sizeof(MpegEncContext),
  2572. encode_init,
  2573. MPV_encode_picture,
  2574. MPV_encode_end,
  2575. .supported_framerates= frame_rate_tab+1,
  2576. };
  2577. #endif
  2578. #endif
  2579. #ifdef HAVE_XVMC
  2580. static int mpeg_mc_decode_init(AVCodecContext *avctx){
  2581. Mpeg1Context *s;
  2582. if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
  2583. return -1;
  2584. if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
  2585. dprintf("mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
  2586. }
  2587. mpeg_decode_init(avctx);
  2588. s = avctx->priv_data;
  2589. avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
  2590. avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
  2591. return 0;
  2592. }
  2593. AVCodec mpeg_xvmc_decoder = {
  2594. "mpegvideo_xvmc",
  2595. CODEC_TYPE_VIDEO,
  2596. CODEC_ID_MPEG2VIDEO_XVMC,
  2597. sizeof(Mpeg1Context),
  2598. mpeg_mc_decode_init,
  2599. NULL,
  2600. mpeg_decode_end,
  2601. mpeg_decode_frame,
  2602. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL,
  2603. .flush= ff_mpeg_flush,
  2604. };
  2605. #endif
  2606. /* this is ugly i know, but the alternative is too make
  2607. hundreds of vars global and prefix them with ff_mpeg1_
  2608. which is far uglier. */
  2609. #include "mdec.c"