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.

3213 lines
108KB

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