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.

2925 lines
98KB

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