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.

3318 lines
112KB

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