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.

3046 lines
102KB

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