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