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.

3017 lines
101KB

  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 = 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. AVPanScan pan_scan; /** some temporary storage for the panscan */
  1569. int slice_count;
  1570. int swap_uv;//indicate VCR2
  1571. int save_aspect_info;
  1572. } Mpeg1Context;
  1573. static int mpeg_decode_init(AVCodecContext *avctx)
  1574. {
  1575. Mpeg1Context *s = avctx->priv_data;
  1576. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1577. MPV_decode_defaults(s2);
  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->pan_scan.width == 0 )||(s1->pan_scan.height == 0) ){
  1641. s->avctx->sample_aspect_ratio=
  1642. av_div_q(
  1643. mpeg2_aspect[s->aspect_ratio_info],
  1644. (AVRational){s->width, s->height}
  1645. );
  1646. }else{
  1647. s->avctx->sample_aspect_ratio=
  1648. av_div_q(
  1649. mpeg2_aspect[s->aspect_ratio_info],
  1650. (AVRational){s1->pan_scan.width, s1->pan_scan.height}
  1651. );
  1652. }
  1653. }else{
  1654. s->avctx->sample_aspect_ratio=
  1655. mpeg2_aspect[s->aspect_ratio_info];
  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. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1794. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  1795. }
  1796. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  1797. {
  1798. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1799. int i,nofco;
  1800. nofco = 1;
  1801. if(s->progressive_sequence){
  1802. if(s->repeat_first_field){
  1803. nofco++;
  1804. if(s->top_field_first)
  1805. nofco++;
  1806. }
  1807. }else{
  1808. if(s->picture_structure == PICT_FRAME){
  1809. nofco++;
  1810. if(s->repeat_first_field)
  1811. nofco++;
  1812. }
  1813. }
  1814. for(i=0; i<nofco; i++){
  1815. s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
  1816. skip_bits(&s->gb, 1); //marker
  1817. s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
  1818. skip_bits(&s->gb, 1); //marker
  1819. }
  1820. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1821. av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
  1822. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  1823. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  1824. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
  1825. );
  1826. }
  1827. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1828. {
  1829. int i, v, j;
  1830. dprintf("matrix extension\n");
  1831. if (get_bits1(&s->gb)) {
  1832. for(i=0;i<64;i++) {
  1833. v = get_bits(&s->gb, 8);
  1834. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1835. s->intra_matrix[j] = v;
  1836. s->chroma_intra_matrix[j] = v;
  1837. }
  1838. }
  1839. if (get_bits1(&s->gb)) {
  1840. for(i=0;i<64;i++) {
  1841. v = get_bits(&s->gb, 8);
  1842. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1843. s->inter_matrix[j] = v;
  1844. s->chroma_inter_matrix[j] = v;
  1845. }
  1846. }
  1847. if (get_bits1(&s->gb)) {
  1848. for(i=0;i<64;i++) {
  1849. v = get_bits(&s->gb, 8);
  1850. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1851. s->chroma_intra_matrix[j] = v;
  1852. }
  1853. }
  1854. if (get_bits1(&s->gb)) {
  1855. for(i=0;i<64;i++) {
  1856. v = get_bits(&s->gb, 8);
  1857. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1858. s->chroma_inter_matrix[j] = v;
  1859. }
  1860. }
  1861. }
  1862. static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
  1863. {
  1864. s->full_pel[0] = s->full_pel[1] = 0;
  1865. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1866. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1867. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1868. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1869. s->intra_dc_precision = get_bits(&s->gb, 2);
  1870. s->picture_structure = get_bits(&s->gb, 2);
  1871. s->top_field_first = get_bits1(&s->gb);
  1872. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1873. s->concealment_motion_vectors = get_bits1(&s->gb);
  1874. s->q_scale_type = get_bits1(&s->gb);
  1875. s->intra_vlc_format = get_bits1(&s->gb);
  1876. s->alternate_scan = get_bits1(&s->gb);
  1877. s->repeat_first_field = get_bits1(&s->gb);
  1878. s->chroma_420_type = get_bits1(&s->gb);
  1879. s->progressive_frame = get_bits1(&s->gb);
  1880. if(s->picture_structure == PICT_FRAME)
  1881. s->first_field=0;
  1882. else{
  1883. s->first_field ^= 1;
  1884. memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
  1885. }
  1886. if(s->alternate_scan){
  1887. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  1888. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  1889. }else{
  1890. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  1891. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  1892. }
  1893. /* composite display not parsed */
  1894. dprintf("intra_dc_precision=%d\n", s->intra_dc_precision);
  1895. dprintf("picture_structure=%d\n", s->picture_structure);
  1896. dprintf("top field first=%d\n", s->top_field_first);
  1897. dprintf("repeat first field=%d\n", s->repeat_first_field);
  1898. dprintf("conceal=%d\n", s->concealment_motion_vectors);
  1899. dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
  1900. dprintf("alternate_scan=%d\n", s->alternate_scan);
  1901. dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1902. dprintf("progressive_frame=%d\n", s->progressive_frame);
  1903. }
  1904. static void mpeg_decode_extension(AVCodecContext *avctx,
  1905. const uint8_t *buf, int buf_size)
  1906. {
  1907. Mpeg1Context *s1 = avctx->priv_data;
  1908. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1909. int ext_type;
  1910. init_get_bits(&s->gb, buf, buf_size*8);
  1911. ext_type = get_bits(&s->gb, 4);
  1912. switch(ext_type) {
  1913. case 0x1:
  1914. mpeg_decode_sequence_extension(s);
  1915. break;
  1916. case 0x2:
  1917. mpeg_decode_sequence_display_extension(s1);
  1918. break;
  1919. case 0x3:
  1920. mpeg_decode_quant_matrix_extension(s);
  1921. break;
  1922. case 0x7:
  1923. mpeg_decode_picture_display_extension(s1);
  1924. break;
  1925. case 0x8:
  1926. mpeg_decode_picture_coding_extension(s);
  1927. break;
  1928. }
  1929. }
  1930. static void exchange_uv(MpegEncContext *s){
  1931. short * tmp;
  1932. tmp = s->pblocks[4];
  1933. s->pblocks[4] = s->pblocks[5];
  1934. s->pblocks[5] = tmp;
  1935. }
  1936. static int mpeg_field_start(MpegEncContext *s){
  1937. AVCodecContext *avctx= s->avctx;
  1938. Mpeg1Context *s1 = (Mpeg1Context*)s;
  1939. /* start frame decoding */
  1940. if(s->first_field || s->picture_structure==PICT_FRAME){
  1941. if(MPV_frame_start(s, avctx) < 0)
  1942. return -1;
  1943. ff_er_frame_start(s);
  1944. /* first check if we must repeat the frame */
  1945. s->current_picture_ptr->repeat_pict = 0;
  1946. if (s->repeat_first_field) {
  1947. if (s->progressive_sequence) {
  1948. if (s->top_field_first)
  1949. s->current_picture_ptr->repeat_pict = 4;
  1950. else
  1951. s->current_picture_ptr->repeat_pict = 2;
  1952. } else if (s->progressive_frame) {
  1953. s->current_picture_ptr->repeat_pict = 1;
  1954. }
  1955. }
  1956. *s->current_picture_ptr->pan_scan= s1->pan_scan;
  1957. }else{ //second field
  1958. int i;
  1959. if(!s->current_picture_ptr){
  1960. av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
  1961. return -1;
  1962. }
  1963. for(i=0; i<4; i++){
  1964. s->current_picture.data[i] = s->current_picture_ptr->data[i];
  1965. if(s->picture_structure == PICT_BOTTOM_FIELD){
  1966. s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
  1967. }
  1968. }
  1969. }
  1970. #ifdef HAVE_XVMC
  1971. // MPV_frame_start will call this function too,
  1972. // but we need to call it on every field
  1973. if(s->avctx->xvmc_acceleration)
  1974. XVMC_field_start(s,avctx);
  1975. #endif
  1976. return 0;
  1977. }
  1978. #define DECODE_SLICE_ERROR -1
  1979. #define DECODE_SLICE_OK 0
  1980. /**
  1981. * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
  1982. * @return DECODE_SLICE_ERROR if the slice is damaged<br>
  1983. * DECODE_SLICE_OK if this slice is ok<br>
  1984. */
  1985. static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
  1986. const uint8_t **buf, int buf_size)
  1987. {
  1988. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1989. AVCodecContext *avctx= s->avctx;
  1990. int ret;
  1991. const int field_pic= s->picture_structure != PICT_FRAME;
  1992. s->resync_mb_x=
  1993. s->resync_mb_y= -1;
  1994. if (mb_y >= s->mb_height){
  1995. av_log(s->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", s->mb_y, s->mb_height);
  1996. return -1;
  1997. }
  1998. init_get_bits(&s->gb, *buf, buf_size*8);
  1999. ff_mpeg1_clean_buffers(s);
  2000. s->interlaced_dct = 0;
  2001. s->qscale = get_qscale(s);
  2002. if(s->qscale == 0){
  2003. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  2004. return -1;
  2005. }
  2006. /* extra slice info */
  2007. while (get_bits1(&s->gb) != 0) {
  2008. skip_bits(&s->gb, 8);
  2009. }
  2010. s->mb_x=0;
  2011. for(;;) {
  2012. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  2013. if (code < 0){
  2014. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  2015. return -1;
  2016. }
  2017. if (code >= 33) {
  2018. if (code == 33) {
  2019. s->mb_x += 33;
  2020. }
  2021. /* otherwise, stuffing, nothing to do */
  2022. } else {
  2023. s->mb_x += code;
  2024. break;
  2025. }
  2026. }
  2027. s->resync_mb_x= s->mb_x;
  2028. s->resync_mb_y= s->mb_y= mb_y;
  2029. s->mb_skip_run= 0;
  2030. ff_init_block_index(s);
  2031. if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
  2032. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  2033. 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",
  2034. 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],
  2035. s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")),
  2036. s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
  2037. s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
  2038. s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
  2039. }
  2040. }
  2041. for(;;) {
  2042. #ifdef HAVE_XVMC
  2043. //one 1 we memcpy blocks in xvmcvideo
  2044. if(s->avctx->xvmc_acceleration > 1)
  2045. XVMC_init_block(s);//set s->block
  2046. #endif
  2047. s->dsp.clear_blocks(s->block[0]);
  2048. ret = mpeg_decode_mb(s, s->block);
  2049. s->chroma_qscale= s->qscale;
  2050. dprintf("ret=%d\n", ret);
  2051. if (ret < 0)
  2052. return -1;
  2053. if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
  2054. const int wrap = field_pic ? 2*s->block_wrap[0] : s->block_wrap[0];
  2055. int xy = s->mb_x*2 + 1 + (s->mb_y*2 +1)*wrap;
  2056. int motion_x, motion_y, dir, i;
  2057. if(field_pic && !s->first_field)
  2058. xy += wrap/2;
  2059. for(i=0; i<2; i++){
  2060. for(dir=0; dir<2; dir++){
  2061. if (s->mb_intra || (dir==1 && s->pict_type != B_TYPE)) {
  2062. motion_x = motion_y = 0;
  2063. }else if (s->mv_type == MV_TYPE_16X16){
  2064. motion_x = s->mv[dir][0][0];
  2065. motion_y = s->mv[dir][0][1];
  2066. } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
  2067. motion_x = s->mv[dir][i][0];
  2068. motion_y = s->mv[dir][i][1];
  2069. }
  2070. s->current_picture.motion_val[dir][xy ][0] = motion_x;
  2071. s->current_picture.motion_val[dir][xy ][1] = motion_y;
  2072. s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
  2073. s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
  2074. }
  2075. xy += wrap;
  2076. }
  2077. }
  2078. s->dest[0] += 16;
  2079. s->dest[1] += 8;
  2080. s->dest[2] += 8;
  2081. MPV_decode_mb(s, s->block);
  2082. if (++s->mb_x >= s->mb_width) {
  2083. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  2084. s->mb_x = 0;
  2085. s->mb_y++;
  2086. if(s->mb_y<<field_pic >= s->mb_height){
  2087. int left= s->gb.size_in_bits - get_bits_count(&s->gb);
  2088. if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)))
  2089. || (avctx->error_resilience >= FF_ER_AGGRESSIVE && left>8)){
  2090. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d\n", left);
  2091. return -1;
  2092. }else
  2093. goto eos;
  2094. }
  2095. ff_init_block_index(s);
  2096. }
  2097. /* skip mb handling */
  2098. if (s->mb_skip_run == -1) {
  2099. /* read again increment */
  2100. s->mb_skip_run = 0;
  2101. for(;;) {
  2102. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  2103. if (code < 0){
  2104. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  2105. return -1;
  2106. }
  2107. if (code >= 33) {
  2108. if (code == 33) {
  2109. s->mb_skip_run += 33;
  2110. }else if(code == 35){
  2111. if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
  2112. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  2113. return -1;
  2114. }
  2115. goto eos; /* end of slice */
  2116. }
  2117. /* otherwise, stuffing, nothing to do */
  2118. } else {
  2119. s->mb_skip_run += code;
  2120. break;
  2121. }
  2122. }
  2123. }
  2124. }
  2125. eos: // end of slice
  2126. *buf += get_bits_count(&s->gb)/8 - 1;
  2127. //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  2128. return 0;
  2129. }
  2130. static int slice_decode_thread(AVCodecContext *c, void *arg){
  2131. MpegEncContext *s= arg;
  2132. const uint8_t *buf= s->gb.buffer;
  2133. int mb_y= s->start_mb_y;
  2134. s->error_count= 3*(s->end_mb_y - s->start_mb_y)*s->mb_width;
  2135. for(;;){
  2136. int start_code, ret;
  2137. ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
  2138. emms_c();
  2139. //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
  2140. //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);
  2141. if(ret < 0){
  2142. if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
  2143. 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);
  2144. }else{
  2145. 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);
  2146. }
  2147. if(s->mb_y == s->end_mb_y)
  2148. return 0;
  2149. start_code = find_start_code(&buf, s->gb.buffer_end);
  2150. mb_y= start_code - SLICE_MIN_START_CODE;
  2151. if(mb_y < 0 || mb_y >= s->end_mb_y)
  2152. return -1;
  2153. }
  2154. return 0; //not reached
  2155. }
  2156. /**
  2157. * handles slice ends.
  2158. * @return 1 if it seems to be the last slice of
  2159. */
  2160. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  2161. {
  2162. Mpeg1Context *s1 = avctx->priv_data;
  2163. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2164. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  2165. return 0;
  2166. #ifdef HAVE_XVMC
  2167. if(s->avctx->xvmc_acceleration)
  2168. XVMC_field_end(s);
  2169. #endif
  2170. /* end of slice reached */
  2171. if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
  2172. /* end of image */
  2173. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
  2174. ff_er_frame_end(s);
  2175. MPV_frame_end(s);
  2176. if (s->pict_type == B_TYPE || s->low_delay) {
  2177. *pict= *(AVFrame*)s->current_picture_ptr;
  2178. ff_print_debug_info(s, pict);
  2179. } else {
  2180. s->picture_number++;
  2181. /* latency of 1 frame for I and P frames */
  2182. /* XXX: use another variable than picture_number */
  2183. if (s->last_picture_ptr != NULL) {
  2184. *pict= *(AVFrame*)s->last_picture_ptr;
  2185. ff_print_debug_info(s, pict);
  2186. }
  2187. }
  2188. return 1;
  2189. } else {
  2190. return 0;
  2191. }
  2192. }
  2193. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  2194. const uint8_t *buf, int buf_size)
  2195. {
  2196. Mpeg1Context *s1 = avctx->priv_data;
  2197. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2198. int width,height;
  2199. int i, v, j;
  2200. init_get_bits(&s->gb, buf, buf_size*8);
  2201. width = get_bits(&s->gb, 12);
  2202. height = get_bits(&s->gb, 12);
  2203. if (width <= 0 || height <= 0 ||
  2204. (width % 2) != 0 || (height % 2) != 0)
  2205. return -1;
  2206. s->aspect_ratio_info= get_bits(&s->gb, 4);
  2207. if (s->aspect_ratio_info == 0)
  2208. return -1;
  2209. s->frame_rate_index = get_bits(&s->gb, 4);
  2210. if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
  2211. return -1;
  2212. s->bit_rate = get_bits(&s->gb, 18) * 400;
  2213. if (get_bits1(&s->gb) == 0) /* marker */
  2214. return -1;
  2215. s->width = width;
  2216. s->height = height;
  2217. s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
  2218. skip_bits(&s->gb, 1);
  2219. /* get matrix */
  2220. if (get_bits1(&s->gb)) {
  2221. for(i=0;i<64;i++) {
  2222. v = get_bits(&s->gb, 8);
  2223. if(v==0){
  2224. av_log(s->avctx, AV_LOG_ERROR, "intra matrix damaged\n");
  2225. return -1;
  2226. }
  2227. j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2228. s->intra_matrix[j] = v;
  2229. s->chroma_intra_matrix[j] = v;
  2230. }
  2231. #ifdef DEBUG
  2232. dprintf("intra matrix present\n");
  2233. for(i=0;i<64;i++)
  2234. dprintf(" %d", s->intra_matrix[s->dsp.idct_permutation[i]);
  2235. printf("\n");
  2236. #endif
  2237. } else {
  2238. for(i=0;i<64;i++) {
  2239. j = s->dsp.idct_permutation[i];
  2240. v = ff_mpeg1_default_intra_matrix[i];
  2241. s->intra_matrix[j] = v;
  2242. s->chroma_intra_matrix[j] = v;
  2243. }
  2244. }
  2245. if (get_bits1(&s->gb)) {
  2246. for(i=0;i<64;i++) {
  2247. v = get_bits(&s->gb, 8);
  2248. if(v==0){
  2249. av_log(s->avctx, AV_LOG_ERROR, "inter matrix damaged\n");
  2250. return -1;
  2251. }
  2252. j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2253. s->inter_matrix[j] = v;
  2254. s->chroma_inter_matrix[j] = v;
  2255. }
  2256. #ifdef DEBUG
  2257. dprintf("non intra matrix present\n");
  2258. for(i=0;i<64;i++)
  2259. dprintf(" %d", s->inter_matrix[s->dsp.idct_permutation[i]);
  2260. printf("\n");
  2261. #endif
  2262. } else {
  2263. for(i=0;i<64;i++) {
  2264. int j= s->dsp.idct_permutation[i];
  2265. v = ff_mpeg1_default_non_intra_matrix[i];
  2266. s->inter_matrix[j] = v;
  2267. s->chroma_inter_matrix[j] = v;
  2268. }
  2269. }
  2270. if(show_bits(&s->gb, 23) != 0){
  2271. av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
  2272. return -1;
  2273. }
  2274. /* we set mpeg2 parameters so that it emulates mpeg1 */
  2275. s->progressive_sequence = 1;
  2276. s->progressive_frame = 1;
  2277. s->picture_structure = PICT_FRAME;
  2278. s->frame_pred_frame_dct = 1;
  2279. s->chroma_format = 1;
  2280. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
  2281. avctx->sub_id = 1; /* indicates mpeg1 */
  2282. s->out_format = FMT_MPEG1;
  2283. s->swap_uv = 0;//AFAIK VCR2 don't have SEQ_HEADER
  2284. if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
  2285. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  2286. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
  2287. s->avctx->rc_buffer_size, s->bit_rate);
  2288. return 0;
  2289. }
  2290. static int vcr2_init_sequence(AVCodecContext *avctx)
  2291. {
  2292. Mpeg1Context *s1 = avctx->priv_data;
  2293. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2294. int i, v;
  2295. /* start new mpeg1 context decoding */
  2296. s->out_format = FMT_MPEG1;
  2297. if (s1->mpeg_enc_ctx_allocated) {
  2298. MPV_common_end(s);
  2299. }
  2300. s->width = avctx->width;
  2301. s->height = avctx->height;
  2302. avctx->has_b_frames= 0; //true?
  2303. s->low_delay= 1;
  2304. if(avctx->xvmc_acceleration){
  2305. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
  2306. }else{
  2307. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
  2308. }
  2309. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
  2310. if( avctx->idct_algo == FF_IDCT_AUTO )
  2311. avctx->idct_algo = FF_IDCT_SIMPLE;
  2312. if (MPV_common_init(s) < 0)
  2313. return -1;
  2314. exchange_uv(s);//common init reset pblocks, so we swap them here
  2315. s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
  2316. s1->mpeg_enc_ctx_allocated = 1;
  2317. for(i=0;i<64;i++) {
  2318. int j= s->dsp.idct_permutation[i];
  2319. v = ff_mpeg1_default_intra_matrix[i];
  2320. s->intra_matrix[j] = v;
  2321. s->chroma_intra_matrix[j] = v;
  2322. v = ff_mpeg1_default_non_intra_matrix[i];
  2323. s->inter_matrix[j] = v;
  2324. s->chroma_inter_matrix[j] = v;
  2325. }
  2326. s->progressive_sequence = 1;
  2327. s->progressive_frame = 1;
  2328. s->picture_structure = PICT_FRAME;
  2329. s->frame_pred_frame_dct = 1;
  2330. s->chroma_format = 1;
  2331. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
  2332. avctx->sub_id = 2; /* indicates mpeg2 */
  2333. return 0;
  2334. }
  2335. static void mpeg_decode_user_data(AVCodecContext *avctx,
  2336. const uint8_t *buf, int buf_size)
  2337. {
  2338. const uint8_t *p;
  2339. int len, flags;
  2340. p = buf;
  2341. len = buf_size;
  2342. /* we parse the DTG active format information */
  2343. if (len >= 5 &&
  2344. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  2345. flags = p[4];
  2346. p += 5;
  2347. len -= 5;
  2348. if (flags & 0x80) {
  2349. /* skip event id */
  2350. if (len < 2)
  2351. return;
  2352. p += 2;
  2353. len -= 2;
  2354. }
  2355. if (flags & 0x40) {
  2356. if (len < 1)
  2357. return;
  2358. avctx->dtg_active_format = p[0] & 0x0f;
  2359. }
  2360. }
  2361. }
  2362. static void mpeg_decode_gop(AVCodecContext *avctx,
  2363. const uint8_t *buf, int buf_size){
  2364. Mpeg1Context *s1 = avctx->priv_data;
  2365. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2366. int drop_frame_flag;
  2367. int time_code_hours, time_code_minutes;
  2368. int time_code_seconds, time_code_pictures;
  2369. int broken_link;
  2370. init_get_bits(&s->gb, buf, buf_size*8);
  2371. drop_frame_flag = get_bits1(&s->gb);
  2372. time_code_hours=get_bits(&s->gb,5);
  2373. time_code_minutes = get_bits(&s->gb,6);
  2374. skip_bits1(&s->gb);//marker bit
  2375. time_code_seconds = get_bits(&s->gb,6);
  2376. time_code_pictures = get_bits(&s->gb,6);
  2377. /*broken_link indicate that after editing the
  2378. reference frames of the first B-Frames after GOP I-Frame
  2379. are missing (open gop)*/
  2380. broken_link = get_bits1(&s->gb);
  2381. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  2382. av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) broken_link=%d\n",
  2383. time_code_hours, time_code_minutes, time_code_seconds,
  2384. time_code_pictures, broken_link);
  2385. }
  2386. /**
  2387. * finds the end of the current frame in the bitstream.
  2388. * @return the position of the first byte of the next frame, or -1
  2389. */
  2390. static int mpeg1_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
  2391. ParseContext *pc= &s->parse_context;
  2392. int i;
  2393. uint32_t state;
  2394. state= pc->state;
  2395. i=0;
  2396. if(!pc->frame_start_found){
  2397. for(i=0; i<buf_size; i++){
  2398. state= (state<<8) | buf[i];
  2399. if(state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
  2400. i++;
  2401. pc->frame_start_found=1;
  2402. break;
  2403. }
  2404. }
  2405. }
  2406. if(pc->frame_start_found){
  2407. for(; i<buf_size; i++){
  2408. state= (state<<8) | buf[i];
  2409. if((state&0xFFFFFF00) == 0x100){
  2410. if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
  2411. pc->frame_start_found=0;
  2412. pc->state=-1;
  2413. return i-3;
  2414. }
  2415. }
  2416. }
  2417. }
  2418. pc->state= state;
  2419. return END_NOT_FOUND;
  2420. }
  2421. /* handle buffering and image synchronisation */
  2422. static int mpeg_decode_frame(AVCodecContext *avctx,
  2423. void *data, int *data_size,
  2424. uint8_t *buf, int buf_size)
  2425. {
  2426. Mpeg1Context *s = avctx->priv_data;
  2427. const uint8_t *buf_end;
  2428. const uint8_t *buf_ptr;
  2429. int ret, start_code, input_size;
  2430. AVFrame *picture = data;
  2431. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2432. dprintf("fill_buffer\n");
  2433. *data_size = 0;
  2434. /* special case for last picture */
  2435. if (buf_size == 0 && s2->low_delay==0 && s2->next_picture_ptr) {
  2436. *picture= *(AVFrame*)s2->next_picture_ptr;
  2437. s2->next_picture_ptr= NULL;
  2438. *data_size = sizeof(AVFrame);
  2439. return 0;
  2440. }
  2441. if(s2->flags&CODEC_FLAG_TRUNCATED){
  2442. int next= mpeg1_find_frame_end(s2, buf, buf_size);
  2443. if( ff_combine_frame(s2, next, &buf, &buf_size) < 0 )
  2444. return buf_size;
  2445. }
  2446. buf_ptr = buf;
  2447. buf_end = buf + buf_size;
  2448. #if 0
  2449. if (s->repeat_field % 2 == 1) {
  2450. s->repeat_field++;
  2451. //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
  2452. // s2->picture_number, s->repeat_field);
  2453. if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
  2454. *data_size = sizeof(AVPicture);
  2455. goto the_end;
  2456. }
  2457. }
  2458. #endif
  2459. if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == ff_get_fourcc("VCR2"))
  2460. vcr2_init_sequence(avctx);
  2461. s->slice_count= 0;
  2462. for(;;) {
  2463. /* find start next code */
  2464. start_code = find_start_code(&buf_ptr, buf_end);
  2465. if (start_code < 0){
  2466. if(s2->pict_type != B_TYPE || avctx->hurry_up==0){
  2467. if(avctx->thread_count > 1){
  2468. int i;
  2469. avctx->execute(avctx, slice_decode_thread, (void**)&(s2->thread_context[0]), NULL, s->slice_count);
  2470. for(i=0; i<s->slice_count; i++)
  2471. s2->error_count += s2->thread_context[i]->error_count;
  2472. }
  2473. if (slice_end(avctx, picture)) {
  2474. if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
  2475. *data_size = sizeof(AVPicture);
  2476. }
  2477. }
  2478. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  2479. }
  2480. input_size = buf_end - buf_ptr;
  2481. if(avctx->debug & FF_DEBUG_STARTCODE){
  2482. av_log(avctx, AV_LOG_DEBUG, "%3X at %d left %d\n", start_code, buf_ptr-buf, input_size);
  2483. }
  2484. /* prepare data for next start code */
  2485. switch(start_code) {
  2486. case SEQ_START_CODE:
  2487. mpeg1_decode_sequence(avctx, buf_ptr,
  2488. input_size);
  2489. break;
  2490. case PICTURE_START_CODE:
  2491. /* we have a complete image : we try to decompress it */
  2492. mpeg1_decode_picture(avctx,
  2493. buf_ptr, input_size);
  2494. break;
  2495. case EXT_START_CODE:
  2496. mpeg_decode_extension(avctx,
  2497. buf_ptr, input_size);
  2498. break;
  2499. case USER_START_CODE:
  2500. mpeg_decode_user_data(avctx,
  2501. buf_ptr, input_size);
  2502. break;
  2503. case GOP_START_CODE:
  2504. s2->first_field=0;
  2505. mpeg_decode_gop(avctx,
  2506. buf_ptr, input_size);
  2507. break;
  2508. default:
  2509. if (start_code >= SLICE_MIN_START_CODE &&
  2510. start_code <= SLICE_MAX_START_CODE) {
  2511. int mb_y= start_code - SLICE_MIN_START_CODE;
  2512. /* skip b frames if we dont have reference frames */
  2513. if(s2->last_picture_ptr==NULL && s2->pict_type==B_TYPE) break;
  2514. /* skip b frames if we are in a hurry */
  2515. if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
  2516. /* skip everything if we are in a hurry>=5 */
  2517. if(avctx->hurry_up>=5) break;
  2518. if (!s->mpeg_enc_ctx_allocated) break;
  2519. if(s2->first_slice){
  2520. s2->first_slice=0;
  2521. if(mpeg_field_start(s2) < 0)
  2522. return -1;
  2523. }
  2524. if(avctx->thread_count > 1){
  2525. int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
  2526. if(threshold <= mb_y){
  2527. MpegEncContext *thread_context= s2->thread_context[s->slice_count];
  2528. thread_context->start_mb_y= mb_y;
  2529. thread_context->end_mb_y = s2->mb_height;
  2530. if(s->slice_count){
  2531. s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
  2532. ff_update_duplicate_context(thread_context, s2);
  2533. }
  2534. init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
  2535. s->slice_count++;
  2536. }
  2537. buf_ptr += 2; //FIXME add minimum num of bytes per slice
  2538. }else{
  2539. ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
  2540. emms_c();
  2541. if(ret < 0){
  2542. if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
  2543. 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);
  2544. }else{
  2545. 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);
  2546. }
  2547. }
  2548. }
  2549. break;
  2550. }
  2551. }
  2552. }
  2553. static int mpeg_decode_end(AVCodecContext *avctx)
  2554. {
  2555. Mpeg1Context *s = avctx->priv_data;
  2556. if (s->mpeg_enc_ctx_allocated)
  2557. MPV_common_end(&s->mpeg_enc_ctx);
  2558. return 0;
  2559. }
  2560. AVCodec mpeg1video_decoder = {
  2561. "mpeg1video",
  2562. CODEC_TYPE_VIDEO,
  2563. CODEC_ID_MPEG1VIDEO,
  2564. sizeof(Mpeg1Context),
  2565. mpeg_decode_init,
  2566. NULL,
  2567. mpeg_decode_end,
  2568. mpeg_decode_frame,
  2569. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  2570. .flush= ff_mpeg_flush,
  2571. };
  2572. AVCodec mpeg2video_decoder = {
  2573. "mpeg2video",
  2574. CODEC_TYPE_VIDEO,
  2575. CODEC_ID_MPEG2VIDEO,
  2576. sizeof(Mpeg1Context),
  2577. mpeg_decode_init,
  2578. NULL,
  2579. mpeg_decode_end,
  2580. mpeg_decode_frame,
  2581. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  2582. .flush= ff_mpeg_flush,
  2583. };
  2584. //legacy decoder
  2585. AVCodec mpegvideo_decoder = {
  2586. "mpegvideo",
  2587. CODEC_TYPE_VIDEO,
  2588. CODEC_ID_MPEG2VIDEO,
  2589. sizeof(Mpeg1Context),
  2590. mpeg_decode_init,
  2591. NULL,
  2592. mpeg_decode_end,
  2593. mpeg_decode_frame,
  2594. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  2595. .flush= ff_mpeg_flush,
  2596. };
  2597. #ifdef CONFIG_ENCODERS
  2598. AVCodec mpeg1video_encoder = {
  2599. "mpeg1video",
  2600. CODEC_TYPE_VIDEO,
  2601. CODEC_ID_MPEG1VIDEO,
  2602. sizeof(MpegEncContext),
  2603. encode_init,
  2604. MPV_encode_picture,
  2605. MPV_encode_end,
  2606. .supported_framerates= frame_rate_tab+1,
  2607. };
  2608. #ifdef CONFIG_RISKY
  2609. AVCodec mpeg2video_encoder = {
  2610. "mpeg2video",
  2611. CODEC_TYPE_VIDEO,
  2612. CODEC_ID_MPEG2VIDEO,
  2613. sizeof(MpegEncContext),
  2614. encode_init,
  2615. MPV_encode_picture,
  2616. MPV_encode_end,
  2617. .supported_framerates= frame_rate_tab+1,
  2618. };
  2619. #endif
  2620. #endif
  2621. #ifdef HAVE_XVMC
  2622. static int mpeg_mc_decode_init(AVCodecContext *avctx){
  2623. Mpeg1Context *s;
  2624. if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
  2625. return -1;
  2626. if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
  2627. dprintf("mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
  2628. }
  2629. mpeg_decode_init(avctx);
  2630. s = avctx->priv_data;
  2631. avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
  2632. avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
  2633. return 0;
  2634. }
  2635. AVCodec mpeg_xvmc_decoder = {
  2636. "mpegvideo_xvmc",
  2637. CODEC_TYPE_VIDEO,
  2638. CODEC_ID_MPEG2VIDEO_XVMC,
  2639. sizeof(Mpeg1Context),
  2640. mpeg_mc_decode_init,
  2641. NULL,
  2642. mpeg_decode_end,
  2643. mpeg_decode_frame,
  2644. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL,
  2645. .flush= ff_mpeg_flush,
  2646. };
  2647. #endif
  2648. /* this is ugly i know, but the alternative is too make
  2649. hundreds of vars global and prefix them with ff_mpeg1_
  2650. which is far uglier. */
  2651. #include "mdec.c"