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.

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