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.

3200 lines
107KB

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