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.

3032 lines
101KB

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