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.

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