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.

2784 lines
94KB

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