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.

3049 lines
102KB

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