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.

2808 lines
94KB

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