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.

2789 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];
  257. time_code = (int64_t)s->fake_picture_number * MPEG1_FRAME_RATE_BASE;
  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) / MPEG1_FRAME_RATE_BASE));
  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 < (24 * 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->codec_id == CODEC_ID_MPEG2VIDEO) {
  897. if (s->q_scale_type) {
  898. return non_linear_qscale[qscale];
  899. } else {
  900. return qscale << 1;
  901. }
  902. }
  903. return qscale;
  904. }
  905. /* motion type (for mpeg2) */
  906. #define MT_FIELD 1
  907. #define MT_FRAME 2
  908. #define MT_16X8 2
  909. #define MT_DMV 3
  910. static int mpeg_decode_mb(MpegEncContext *s,
  911. DCTELEM block[6][64])
  912. {
  913. int i, j, k, cbp, val, mb_type, motion_type;
  914. dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
  915. assert(s->mb_skiped==0);
  916. if (s->mb_skip_run-- != 0) {
  917. if(s->pict_type == I_TYPE){
  918. av_log(s->avctx, AV_LOG_ERROR, "skiped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
  919. return -1;
  920. }
  921. /* skip mb */
  922. s->mb_intra = 0;
  923. for(i=0;i<6;i++)
  924. s->block_last_index[i] = -1;
  925. s->mv_type = MV_TYPE_16X16;
  926. if (s->pict_type == P_TYPE) {
  927. /* if P type, zero motion vector is implied */
  928. s->mv_dir = MV_DIR_FORWARD;
  929. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  930. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  931. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  932. s->mb_skiped = 1;
  933. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
  934. } else {
  935. /* if B type, reuse previous vectors and directions */
  936. s->mv[0][0][0] = s->last_mv[0][0][0];
  937. s->mv[0][0][1] = s->last_mv[0][0][1];
  938. s->mv[1][0][0] = s->last_mv[1][0][0];
  939. s->mv[1][0][1] = s->last_mv[1][0][1];
  940. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
  941. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1] | MB_TYPE_SKIP;
  942. // assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
  943. if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
  944. s->mb_skiped = 1;
  945. }
  946. return 0;
  947. }
  948. switch(s->pict_type) {
  949. default:
  950. case I_TYPE:
  951. if (get_bits1(&s->gb) == 0) {
  952. if (get_bits1(&s->gb) == 0){
  953. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
  954. return -1;
  955. }
  956. mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
  957. } else {
  958. mb_type = MB_TYPE_INTRA;
  959. }
  960. break;
  961. case P_TYPE:
  962. mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
  963. if (mb_type < 0){
  964. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
  965. return -1;
  966. }
  967. mb_type = ptype2mb_type[ mb_type ];
  968. break;
  969. case B_TYPE:
  970. mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
  971. if (mb_type < 0){
  972. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
  973. return -1;
  974. }
  975. mb_type = btype2mb_type[ mb_type ];
  976. break;
  977. }
  978. dprintf("mb_type=%x\n", mb_type);
  979. // motion_type = 0; /* avoid warning */
  980. if (IS_INTRA(mb_type)) {
  981. /* compute dct type */
  982. if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
  983. !s->frame_pred_frame_dct) {
  984. s->interlaced_dct = get_bits1(&s->gb);
  985. }
  986. if (IS_QUANT(mb_type))
  987. s->qscale = get_qscale(s);
  988. if (s->concealment_motion_vectors) {
  989. /* just parse them */
  990. if (s->picture_structure != PICT_FRAME)
  991. skip_bits1(&s->gb); /* field select */
  992. s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
  993. mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
  994. s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
  995. mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
  996. skip_bits1(&s->gb); /* marker */
  997. }else
  998. memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
  999. s->mb_intra = 1;
  1000. #ifdef HAVE_XVMC
  1001. //one 1 we memcpy blocks in xvmcvideo
  1002. if(s->avctx->xvmc_acceleration > 1){
  1003. XVMC_pack_pblocks(s,-1);//inter are always full blocks
  1004. if(s->swap_uv){
  1005. exchange_uv(s);
  1006. }
  1007. }
  1008. #endif
  1009. if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
  1010. for(i=0;i<6;i++) {
  1011. if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
  1012. return -1;
  1013. }
  1014. } else {
  1015. for(i=0;i<6;i++) {
  1016. if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0)
  1017. return -1;
  1018. }
  1019. }
  1020. } else {
  1021. if (mb_type & MB_TYPE_ZERO_MV){
  1022. assert(mb_type & MB_TYPE_CBP);
  1023. /* compute dct type */
  1024. if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
  1025. !s->frame_pred_frame_dct) {
  1026. s->interlaced_dct = get_bits1(&s->gb);
  1027. }
  1028. if (IS_QUANT(mb_type))
  1029. s->qscale = get_qscale(s);
  1030. s->mv_dir = MV_DIR_FORWARD;
  1031. s->mv_type = MV_TYPE_16X16;
  1032. s->last_mv[0][0][0] = 0;
  1033. s->last_mv[0][0][1] = 0;
  1034. s->last_mv[0][1][0] = 0;
  1035. s->last_mv[0][1][1] = 0;
  1036. s->mv[0][0][0] = 0;
  1037. s->mv[0][0][1] = 0;
  1038. }else{
  1039. assert(mb_type & MB_TYPE_L0L1);
  1040. //FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
  1041. /* get additionnal motion vector type */
  1042. if (s->frame_pred_frame_dct)
  1043. motion_type = MT_FRAME;
  1044. else{
  1045. motion_type = get_bits(&s->gb, 2);
  1046. }
  1047. /* compute dct type */
  1048. if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
  1049. !s->frame_pred_frame_dct && HAS_CBP(mb_type)) {
  1050. s->interlaced_dct = get_bits1(&s->gb);
  1051. }
  1052. if (IS_QUANT(mb_type))
  1053. s->qscale = get_qscale(s);
  1054. /* motion vectors */
  1055. s->mv_dir = 0;
  1056. for(i=0;i<2;i++) {
  1057. if (USES_LIST(mb_type, i)) {
  1058. s->mv_dir |= (MV_DIR_FORWARD >> i);
  1059. dprintf("motion_type=%d\n", motion_type);
  1060. switch(motion_type) {
  1061. case MT_FRAME: /* or MT_16X8 */
  1062. if (s->picture_structure == PICT_FRAME) {
  1063. /* MT_FRAME */
  1064. mb_type |= MB_TYPE_16x16;
  1065. s->mv_type = MV_TYPE_16X16;
  1066. s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
  1067. mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
  1068. s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
  1069. mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
  1070. /* full_pel: only for mpeg1 */
  1071. if (s->full_pel[i]){
  1072. s->mv[i][0][0] <<= 1;
  1073. s->mv[i][0][1] <<= 1;
  1074. }
  1075. } else {
  1076. /* MT_16X8 */
  1077. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  1078. s->mv_type = MV_TYPE_16X8;
  1079. for(j=0;j<2;j++) {
  1080. s->field_select[i][j] = get_bits1(&s->gb);
  1081. for(k=0;k<2;k++) {
  1082. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  1083. s->last_mv[i][j][k]);
  1084. s->last_mv[i][j][k] = val;
  1085. s->mv[i][j][k] = val;
  1086. }
  1087. }
  1088. }
  1089. break;
  1090. case MT_FIELD:
  1091. s->mv_type = MV_TYPE_FIELD;
  1092. if (s->picture_structure == PICT_FRAME) {
  1093. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  1094. for(j=0;j<2;j++) {
  1095. s->field_select[i][j] = get_bits1(&s->gb);
  1096. val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  1097. s->last_mv[i][j][0]);
  1098. s->last_mv[i][j][0] = val;
  1099. s->mv[i][j][0] = val;
  1100. dprintf("fmx=%d\n", val);
  1101. val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  1102. s->last_mv[i][j][1] >> 1);
  1103. s->last_mv[i][j][1] = val << 1;
  1104. s->mv[i][j][1] = val;
  1105. dprintf("fmy=%d\n", val);
  1106. }
  1107. } else {
  1108. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  1109. s->field_select[i][0] = get_bits1(&s->gb);
  1110. for(k=0;k<2;k++) {
  1111. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  1112. s->last_mv[i][0][k]);
  1113. s->last_mv[i][0][k] = val;
  1114. s->last_mv[i][1][k] = val;
  1115. s->mv[i][0][k] = val;
  1116. }
  1117. }
  1118. break;
  1119. case MT_DMV:
  1120. {
  1121. int dmx, dmy, mx, my, m;
  1122. mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  1123. s->last_mv[i][0][0]);
  1124. s->last_mv[i][0][0] = mx;
  1125. s->last_mv[i][1][0] = mx;
  1126. dmx = get_dmv(s);
  1127. my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  1128. s->last_mv[i][0][1] >> 1);
  1129. dmy = get_dmv(s);
  1130. s->mv_type = MV_TYPE_DMV;
  1131. s->last_mv[i][0][1] = my<<1;
  1132. s->last_mv[i][1][1] = my<<1;
  1133. s->mv[i][0][0] = mx;
  1134. s->mv[i][0][1] = my;
  1135. s->mv[i][1][0] = mx;//not used
  1136. s->mv[i][1][1] = my;//not used
  1137. if (s->picture_structure == PICT_FRAME) {
  1138. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  1139. //m = 1 + 2 * s->top_field_first;
  1140. m = s->top_field_first ? 1 : 3;
  1141. /* top -> top pred */
  1142. s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  1143. s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
  1144. m = 4 - m;
  1145. s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  1146. s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
  1147. } else {
  1148. mb_type |= MB_TYPE_16x16;
  1149. s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
  1150. s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
  1151. if(s->picture_structure == PICT_TOP_FIELD)
  1152. s->mv[i][2][1]--;
  1153. else
  1154. s->mv[i][2][1]++;
  1155. }
  1156. }
  1157. break;
  1158. default:
  1159. av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
  1160. return -1;
  1161. }
  1162. }
  1163. }
  1164. }
  1165. s->mb_intra = 0;
  1166. if (HAS_CBP(mb_type)) {
  1167. cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
  1168. if (cbp < 0){
  1169. av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
  1170. return -1;
  1171. }
  1172. cbp++;
  1173. #ifdef HAVE_XVMC
  1174. //on 1 we memcpy blocks in xvmcvideo
  1175. if(s->avctx->xvmc_acceleration > 1){
  1176. XVMC_pack_pblocks(s,cbp);
  1177. if(s->swap_uv){
  1178. exchange_uv(s);
  1179. }
  1180. }
  1181. #endif
  1182. if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
  1183. for(i=0;i<6;i++) {
  1184. if (cbp & 32) {
  1185. if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
  1186. return -1;
  1187. } else {
  1188. s->block_last_index[i] = -1;
  1189. }
  1190. cbp+=cbp;
  1191. }
  1192. } else {
  1193. for(i=0;i<6;i++) {
  1194. if (cbp & 32) {
  1195. if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
  1196. return -1;
  1197. } else {
  1198. s->block_last_index[i] = -1;
  1199. }
  1200. cbp+=cbp;
  1201. }
  1202. }
  1203. }else{
  1204. for(i=0;i<6;i++)
  1205. s->block_last_index[i] = -1;
  1206. }
  1207. }
  1208. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
  1209. return 0;
  1210. }
  1211. /* as h263, but only 17 codes */
  1212. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
  1213. {
  1214. int code, sign, val, l, shift;
  1215. code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
  1216. if (code == 0) {
  1217. return pred;
  1218. }
  1219. if (code < 0) {
  1220. return 0xffff;
  1221. }
  1222. sign = get_bits1(&s->gb);
  1223. shift = fcode - 1;
  1224. val = code;
  1225. if (shift) {
  1226. val = (val - 1) << shift;
  1227. val |= get_bits(&s->gb, shift);
  1228. val++;
  1229. }
  1230. if (sign)
  1231. val = -val;
  1232. val += pred;
  1233. /* modulo decoding */
  1234. l = 1 << (shift+4);
  1235. val = ((val + l)&(l*2-1)) - l;
  1236. return val;
  1237. }
  1238. static inline int decode_dc(GetBitContext *gb, int component)
  1239. {
  1240. int code, diff;
  1241. if (component == 0) {
  1242. code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
  1243. } else {
  1244. code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
  1245. }
  1246. if (code < 0){
  1247. av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
  1248. return 0xffff;
  1249. }
  1250. if (code == 0) {
  1251. diff = 0;
  1252. } else {
  1253. diff = get_xbits(gb, code);
  1254. }
  1255. return diff;
  1256. }
  1257. static inline int mpeg1_decode_block_intra(MpegEncContext *s,
  1258. DCTELEM *block,
  1259. int n)
  1260. {
  1261. int level, dc, diff, i, j, run;
  1262. int component;
  1263. RLTable *rl = &rl_mpeg1;
  1264. uint8_t * const scantable= s->intra_scantable.permutated;
  1265. const uint16_t *quant_matrix= s->intra_matrix;
  1266. const int qscale= s->qscale;
  1267. /* DC coef */
  1268. component = (n <= 3 ? 0 : n - 4 + 1);
  1269. diff = decode_dc(&s->gb, component);
  1270. if (diff >= 0xffff)
  1271. return -1;
  1272. dc = s->last_dc[component];
  1273. dc += diff;
  1274. s->last_dc[component] = dc;
  1275. block[0] = dc<<3;
  1276. dprintf("dc=%d diff=%d\n", dc, diff);
  1277. i = 0;
  1278. {
  1279. OPEN_READER(re, &s->gb);
  1280. /* now quantify & encode AC coefs */
  1281. for(;;) {
  1282. UPDATE_CACHE(re, &s->gb);
  1283. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  1284. if(level == 127){
  1285. break;
  1286. } else if(level != 0) {
  1287. i += run;
  1288. j = scantable[i];
  1289. level= (level*qscale*quant_matrix[j])>>3;
  1290. level= (level-1)|1;
  1291. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1292. LAST_SKIP_BITS(re, &s->gb, 1);
  1293. } else {
  1294. /* escape */
  1295. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1296. UPDATE_CACHE(re, &s->gb);
  1297. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  1298. if (level == -128) {
  1299. level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
  1300. } else if (level == 0) {
  1301. level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
  1302. }
  1303. i += run;
  1304. j = scantable[i];
  1305. if(level<0){
  1306. level= -level;
  1307. level= (level*qscale*quant_matrix[j])>>3;
  1308. level= (level-1)|1;
  1309. level= -level;
  1310. }else{
  1311. level= (level*qscale*quant_matrix[j])>>3;
  1312. level= (level-1)|1;
  1313. }
  1314. }
  1315. if (i > 63){
  1316. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1317. return -1;
  1318. }
  1319. block[j] = level;
  1320. }
  1321. CLOSE_READER(re, &s->gb);
  1322. }
  1323. s->block_last_index[n] = i;
  1324. return 0;
  1325. }
  1326. static inline int mpeg1_decode_block_inter(MpegEncContext *s,
  1327. DCTELEM *block,
  1328. int n)
  1329. {
  1330. int level, i, j, run;
  1331. RLTable *rl = &rl_mpeg1;
  1332. uint8_t * const scantable= s->intra_scantable.permutated;
  1333. const uint16_t *quant_matrix= s->inter_matrix;
  1334. const int qscale= s->qscale;
  1335. {
  1336. int v;
  1337. OPEN_READER(re, &s->gb);
  1338. i = -1;
  1339. /* special case for the first coef. no need to add a second vlc table */
  1340. UPDATE_CACHE(re, &s->gb);
  1341. v= SHOW_UBITS(re, &s->gb, 2);
  1342. if (v & 2) {
  1343. LAST_SKIP_BITS(re, &s->gb, 2);
  1344. level= (3*qscale*quant_matrix[0])>>4;
  1345. level= (level-1)|1;
  1346. if(v&1)
  1347. level= -level;
  1348. block[0] = level;
  1349. i++;
  1350. }
  1351. /* now quantify & encode AC coefs */
  1352. for(;;) {
  1353. UPDATE_CACHE(re, &s->gb);
  1354. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  1355. if(level == 127){
  1356. break;
  1357. } else if(level != 0) {
  1358. i += run;
  1359. j = scantable[i];
  1360. level= ((level*2+1)*qscale*quant_matrix[j])>>4;
  1361. level= (level-1)|1;
  1362. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1363. LAST_SKIP_BITS(re, &s->gb, 1);
  1364. } else {
  1365. /* escape */
  1366. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1367. UPDATE_CACHE(re, &s->gb);
  1368. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  1369. if (level == -128) {
  1370. level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
  1371. } else if (level == 0) {
  1372. level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
  1373. }
  1374. i += run;
  1375. j = scantable[i];
  1376. if(level<0){
  1377. level= -level;
  1378. level= ((level*2+1)*qscale*quant_matrix[j])>>4;
  1379. level= (level-1)|1;
  1380. level= -level;
  1381. }else{
  1382. level= ((level*2+1)*qscale*quant_matrix[j])>>4;
  1383. level= (level-1)|1;
  1384. }
  1385. }
  1386. if (i > 63){
  1387. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1388. return -1;
  1389. }
  1390. block[j] = level;
  1391. }
  1392. CLOSE_READER(re, &s->gb);
  1393. }
  1394. s->block_last_index[n] = i;
  1395. return 0;
  1396. }
  1397. /* Also does unquantization here, since I will never support mpeg2
  1398. encoding */
  1399. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
  1400. DCTELEM *block,
  1401. int n)
  1402. {
  1403. int level, i, j, run;
  1404. RLTable *rl = &rl_mpeg1;
  1405. uint8_t * const scantable= s->intra_scantable.permutated;
  1406. const uint16_t *quant_matrix;
  1407. const int qscale= s->qscale;
  1408. int mismatch;
  1409. mismatch = 1;
  1410. {
  1411. int v;
  1412. OPEN_READER(re, &s->gb);
  1413. i = -1;
  1414. if (n < 4)
  1415. quant_matrix = s->inter_matrix;
  1416. else
  1417. quant_matrix = s->chroma_inter_matrix;
  1418. /* special case for the first coef. no need to add a second vlc table */
  1419. UPDATE_CACHE(re, &s->gb);
  1420. v= SHOW_UBITS(re, &s->gb, 2);
  1421. if (v & 2) {
  1422. LAST_SKIP_BITS(re, &s->gb, 2);
  1423. level= (3*qscale*quant_matrix[0])>>5;
  1424. if(v&1)
  1425. level= -level;
  1426. block[0] = level;
  1427. mismatch ^= level;
  1428. i++;
  1429. }
  1430. /* now quantify & encode AC coefs */
  1431. for(;;) {
  1432. UPDATE_CACHE(re, &s->gb);
  1433. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  1434. if(level == 127){
  1435. break;
  1436. } else if(level != 0) {
  1437. i += run;
  1438. j = scantable[i];
  1439. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1440. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1441. LAST_SKIP_BITS(re, &s->gb, 1);
  1442. } else {
  1443. /* escape */
  1444. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1445. UPDATE_CACHE(re, &s->gb);
  1446. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1447. i += run;
  1448. j = scantable[i];
  1449. if(level<0){
  1450. level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
  1451. level= -level;
  1452. }else{
  1453. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1454. }
  1455. }
  1456. if (i > 63){
  1457. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1458. return -1;
  1459. }
  1460. mismatch ^= level;
  1461. block[j] = level;
  1462. }
  1463. CLOSE_READER(re, &s->gb);
  1464. }
  1465. block[63] ^= (mismatch & 1);
  1466. s->block_last_index[n] = i;
  1467. return 0;
  1468. }
  1469. static inline int mpeg2_decode_block_intra(MpegEncContext *s,
  1470. DCTELEM *block,
  1471. int n)
  1472. {
  1473. int level, dc, diff, i, j, run;
  1474. int component;
  1475. RLTable *rl;
  1476. uint8_t * const scantable= s->intra_scantable.permutated;
  1477. const uint16_t *quant_matrix;
  1478. const int qscale= s->qscale;
  1479. int mismatch;
  1480. /* DC coef */
  1481. if (n < 4){
  1482. quant_matrix = s->intra_matrix;
  1483. component = 0;
  1484. }else{
  1485. quant_matrix = s->chroma_intra_matrix;
  1486. component = n - 3;
  1487. }
  1488. diff = decode_dc(&s->gb, component);
  1489. if (diff >= 0xffff)
  1490. return -1;
  1491. dc = s->last_dc[component];
  1492. dc += diff;
  1493. s->last_dc[component] = dc;
  1494. block[0] = dc << (3 - s->intra_dc_precision);
  1495. dprintf("dc=%d\n", block[0]);
  1496. mismatch = block[0] ^ 1;
  1497. i = 0;
  1498. if (s->intra_vlc_format)
  1499. rl = &rl_mpeg2;
  1500. else
  1501. rl = &rl_mpeg1;
  1502. {
  1503. OPEN_READER(re, &s->gb);
  1504. /* now quantify & encode AC coefs */
  1505. for(;;) {
  1506. UPDATE_CACHE(re, &s->gb);
  1507. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  1508. if(level == 127){
  1509. break;
  1510. } else if(level != 0) {
  1511. i += run;
  1512. j = scantable[i];
  1513. level= (level*qscale*quant_matrix[j])>>4;
  1514. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1515. LAST_SKIP_BITS(re, &s->gb, 1);
  1516. } else {
  1517. /* escape */
  1518. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1519. UPDATE_CACHE(re, &s->gb);
  1520. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1521. i += run;
  1522. j = scantable[i];
  1523. if(level<0){
  1524. level= (-level*qscale*quant_matrix[j])>>4;
  1525. level= -level;
  1526. }else{
  1527. level= (level*qscale*quant_matrix[j])>>4;
  1528. }
  1529. }
  1530. if (i > 63){
  1531. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1532. return -1;
  1533. }
  1534. mismatch^= level;
  1535. block[j] = level;
  1536. }
  1537. CLOSE_READER(re, &s->gb);
  1538. }
  1539. block[63]^= mismatch&1;
  1540. s->block_last_index[n] = i;
  1541. return 0;
  1542. }
  1543. typedef struct Mpeg1Context {
  1544. MpegEncContext mpeg_enc_ctx;
  1545. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  1546. int repeat_field; /* true if we must repeat the field */
  1547. AVPanScan pan_scan; /** some temporary storage for the panscan */
  1548. } Mpeg1Context;
  1549. static int mpeg_decode_init(AVCodecContext *avctx)
  1550. {
  1551. Mpeg1Context *s = avctx->priv_data;
  1552. s->mpeg_enc_ctx.flags= avctx->flags;
  1553. common_init(&s->mpeg_enc_ctx);
  1554. init_vlcs();
  1555. s->mpeg_enc_ctx_allocated = 0;
  1556. s->mpeg_enc_ctx.picture_number = 0;
  1557. s->repeat_field = 0;
  1558. s->mpeg_enc_ctx.codec_id= avctx->codec->id;
  1559. return 0;
  1560. }
  1561. /* return the 8 bit start code value and update the search
  1562. state. Return -1 if no start code found */
  1563. static int find_start_code(uint8_t **pbuf_ptr, uint8_t *buf_end)
  1564. {
  1565. uint8_t *buf_ptr;
  1566. unsigned int state=0xFFFFFFFF, v;
  1567. int val;
  1568. buf_ptr = *pbuf_ptr;
  1569. while (buf_ptr < buf_end) {
  1570. v = *buf_ptr++;
  1571. if (state == 0x000001) {
  1572. state = ((state << 8) | v) & 0xffffff;
  1573. val = state;
  1574. goto found;
  1575. }
  1576. state = ((state << 8) | v) & 0xffffff;
  1577. }
  1578. val = -1;
  1579. found:
  1580. *pbuf_ptr = buf_ptr;
  1581. return val;
  1582. }
  1583. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1584. uint8_t *buf, int buf_size)
  1585. {
  1586. Mpeg1Context *s1 = avctx->priv_data;
  1587. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1588. int ref, f_code, vbv_delay;
  1589. init_get_bits(&s->gb, buf, buf_size*8);
  1590. ref = get_bits(&s->gb, 10); /* temporal ref */
  1591. s->pict_type = get_bits(&s->gb, 3);
  1592. dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number);
  1593. vbv_delay= get_bits(&s->gb, 16);
  1594. if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
  1595. s->full_pel[0] = get_bits1(&s->gb);
  1596. f_code = get_bits(&s->gb, 3);
  1597. if (f_code == 0)
  1598. return -1;
  1599. s->mpeg_f_code[0][0] = f_code;
  1600. s->mpeg_f_code[0][1] = f_code;
  1601. }
  1602. if (s->pict_type == B_TYPE) {
  1603. s->full_pel[1] = get_bits1(&s->gb);
  1604. f_code = get_bits(&s->gb, 3);
  1605. if (f_code == 0)
  1606. return -1;
  1607. s->mpeg_f_code[1][0] = f_code;
  1608. s->mpeg_f_code[1][1] = f_code;
  1609. }
  1610. s->current_picture.pict_type= s->pict_type;
  1611. s->current_picture.key_frame= s->pict_type == I_TYPE;
  1612. s->y_dc_scale = 8;
  1613. s->c_dc_scale = 8;
  1614. s->first_slice = 1;
  1615. return 0;
  1616. }
  1617. static void mpeg_decode_sequence_extension(MpegEncContext *s)
  1618. {
  1619. int horiz_size_ext, vert_size_ext;
  1620. int bit_rate_ext;
  1621. int frame_rate_ext_n, frame_rate_ext_d;
  1622. int level, profile;
  1623. skip_bits(&s->gb, 1); /* profil and level esc*/
  1624. profile= get_bits(&s->gb, 3);
  1625. level= get_bits(&s->gb, 4);
  1626. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1627. skip_bits(&s->gb, 2); /* chroma_format */
  1628. horiz_size_ext = get_bits(&s->gb, 2);
  1629. vert_size_ext = get_bits(&s->gb, 2);
  1630. s->width |= (horiz_size_ext << 12);
  1631. s->height |= (vert_size_ext << 12);
  1632. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1633. s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400;
  1634. skip_bits1(&s->gb); /* marker */
  1635. s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
  1636. s->low_delay = get_bits1(&s->gb);
  1637. if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
  1638. frame_rate_ext_n = get_bits(&s->gb, 2);
  1639. frame_rate_ext_d = get_bits(&s->gb, 5);
  1640. av_reduce(
  1641. &s->avctx->frame_rate,
  1642. &s->avctx->frame_rate_base,
  1643. frame_rate_tab[s->frame_rate_index] * (frame_rate_ext_n+1),
  1644. MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d+1),
  1645. 1<<30);
  1646. dprintf("sequence extension\n");
  1647. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
  1648. s->avctx->sub_id = 2; /* indicates mpeg2 found */
  1649. if(s->aspect_ratio_info <= 1)
  1650. s->avctx->sample_aspect_ratio= mpeg2_aspect[s->aspect_ratio_info];
  1651. else{
  1652. s->avctx->sample_aspect_ratio=
  1653. av_div_q(
  1654. mpeg2_aspect[s->aspect_ratio_info],
  1655. (AVRational){s->width, s->height}
  1656. );
  1657. }
  1658. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1659. av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
  1660. profile, level, s->avctx->rc_buffer_size, s->bit_rate);
  1661. }
  1662. static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
  1663. {
  1664. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1665. int color_description, w, h;
  1666. skip_bits(&s->gb, 3); /* video format */
  1667. color_description= get_bits1(&s->gb);
  1668. if(color_description){
  1669. skip_bits(&s->gb, 8); /* color primaries */
  1670. skip_bits(&s->gb, 8); /* transfer_characteristics */
  1671. skip_bits(&s->gb, 8); /* matrix_coefficients */
  1672. }
  1673. w= get_bits(&s->gb, 14);
  1674. skip_bits(&s->gb, 1); //marker
  1675. h= get_bits(&s->gb, 14);
  1676. skip_bits(&s->gb, 1); //marker
  1677. s1->pan_scan.width= 16*w;
  1678. s1->pan_scan.height=16*h;
  1679. if(s->aspect_ratio_info > 1)
  1680. s->avctx->sample_aspect_ratio=
  1681. av_div_q(
  1682. mpeg2_aspect[s->aspect_ratio_info],
  1683. (AVRational){w, h}
  1684. );
  1685. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1686. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  1687. }
  1688. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  1689. {
  1690. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1691. int i;
  1692. for(i=0; i<1; i++){ //FIXME count
  1693. s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
  1694. skip_bits(&s->gb, 1); //marker
  1695. s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
  1696. skip_bits(&s->gb, 1); //marker
  1697. }
  1698. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1699. av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
  1700. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  1701. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  1702. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
  1703. );
  1704. }
  1705. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1706. {
  1707. int i, v, j;
  1708. dprintf("matrix extension\n");
  1709. if (get_bits1(&s->gb)) {
  1710. for(i=0;i<64;i++) {
  1711. v = get_bits(&s->gb, 8);
  1712. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1713. s->intra_matrix[j] = v;
  1714. s->chroma_intra_matrix[j] = v;
  1715. }
  1716. }
  1717. if (get_bits1(&s->gb)) {
  1718. for(i=0;i<64;i++) {
  1719. v = get_bits(&s->gb, 8);
  1720. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1721. s->inter_matrix[j] = v;
  1722. s->chroma_inter_matrix[j] = v;
  1723. }
  1724. }
  1725. if (get_bits1(&s->gb)) {
  1726. for(i=0;i<64;i++) {
  1727. v = get_bits(&s->gb, 8);
  1728. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1729. s->chroma_intra_matrix[j] = v;
  1730. }
  1731. }
  1732. if (get_bits1(&s->gb)) {
  1733. for(i=0;i<64;i++) {
  1734. v = get_bits(&s->gb, 8);
  1735. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1736. s->chroma_inter_matrix[j] = v;
  1737. }
  1738. }
  1739. }
  1740. static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
  1741. {
  1742. s->full_pel[0] = s->full_pel[1] = 0;
  1743. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1744. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1745. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1746. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1747. s->intra_dc_precision = get_bits(&s->gb, 2);
  1748. s->picture_structure = get_bits(&s->gb, 2);
  1749. s->top_field_first = get_bits1(&s->gb);
  1750. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1751. s->concealment_motion_vectors = get_bits1(&s->gb);
  1752. s->q_scale_type = get_bits1(&s->gb);
  1753. s->intra_vlc_format = get_bits1(&s->gb);
  1754. s->alternate_scan = get_bits1(&s->gb);
  1755. s->repeat_first_field = get_bits1(&s->gb);
  1756. s->chroma_420_type = get_bits1(&s->gb);
  1757. s->progressive_frame = get_bits1(&s->gb);
  1758. if(s->picture_structure == PICT_FRAME)
  1759. s->first_field=0;
  1760. else{
  1761. s->first_field ^= 1;
  1762. memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
  1763. }
  1764. if(s->alternate_scan){
  1765. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  1766. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  1767. }else{
  1768. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  1769. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  1770. }
  1771. /* composite display not parsed */
  1772. dprintf("intra_dc_precision=%d\n", s->intra_dc_precision);
  1773. dprintf("picture_structure=%d\n", s->picture_structure);
  1774. dprintf("top field first=%d\n", s->top_field_first);
  1775. dprintf("repeat first field=%d\n", s->repeat_first_field);
  1776. dprintf("conceal=%d\n", s->concealment_motion_vectors);
  1777. dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
  1778. dprintf("alternate_scan=%d\n", s->alternate_scan);
  1779. dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1780. dprintf("progressive_frame=%d\n", s->progressive_frame);
  1781. }
  1782. static void mpeg_decode_extension(AVCodecContext *avctx,
  1783. uint8_t *buf, int buf_size)
  1784. {
  1785. Mpeg1Context *s1 = avctx->priv_data;
  1786. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1787. int ext_type;
  1788. init_get_bits(&s->gb, buf, buf_size*8);
  1789. ext_type = get_bits(&s->gb, 4);
  1790. switch(ext_type) {
  1791. case 0x1:
  1792. mpeg_decode_sequence_extension(s);
  1793. break;
  1794. case 0x2:
  1795. mpeg_decode_sequence_display_extension(s1);
  1796. break;
  1797. case 0x3:
  1798. mpeg_decode_quant_matrix_extension(s);
  1799. break;
  1800. case 0x7:
  1801. mpeg_decode_picture_display_extension(s1);
  1802. break;
  1803. case 0x8:
  1804. mpeg_decode_picture_coding_extension(s);
  1805. break;
  1806. }
  1807. }
  1808. static void exchange_uv(MpegEncContext *s){
  1809. short * tmp;
  1810. tmp = s->pblocks[4];
  1811. s->pblocks[4] = s->pblocks[5];
  1812. s->pblocks[5] = tmp;
  1813. }
  1814. #define DECODE_SLICE_FATAL_ERROR -2
  1815. #define DECODE_SLICE_ERROR -1
  1816. #define DECODE_SLICE_OK 0
  1817. /**
  1818. * decodes a slice.
  1819. * @return DECODE_SLICE_FATAL_ERROR if a non recoverable error occured<br>
  1820. * DECODE_SLICE_ERROR if the slice is damaged<br>
  1821. * DECODE_SLICE_OK if this slice is ok<br>
  1822. */
  1823. static int mpeg_decode_slice(AVCodecContext *avctx,
  1824. AVFrame *pict,
  1825. int start_code,
  1826. uint8_t **buf, int buf_size)
  1827. {
  1828. Mpeg1Context *s1 = avctx->priv_data;
  1829. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1830. int ret;
  1831. const int field_pic= s->picture_structure != PICT_FRAME;
  1832. s->resync_mb_x= s->mb_x =
  1833. s->resync_mb_y= s->mb_y = -1;
  1834. start_code = (start_code - 1) & 0xff;
  1835. if (start_code >= s->mb_height){
  1836. av_log(s->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", start_code, s->mb_height);
  1837. return -1;
  1838. }
  1839. ff_mpeg1_clean_buffers(s);
  1840. s->interlaced_dct = 0;
  1841. /* start frame decoding */
  1842. if (s->first_slice) {
  1843. if(s->first_field || s->picture_structure==PICT_FRAME){
  1844. if(MPV_frame_start(s, avctx) < 0)
  1845. return DECODE_SLICE_FATAL_ERROR;
  1846. ff_er_frame_start(s);
  1847. /* first check if we must repeat the frame */
  1848. s->current_picture_ptr->repeat_pict = 0;
  1849. if (s->repeat_first_field) {
  1850. if (s->progressive_sequence) {
  1851. if (s->top_field_first)
  1852. s->current_picture_ptr->repeat_pict = 4;
  1853. else
  1854. s->current_picture_ptr->repeat_pict = 2;
  1855. } else if (s->progressive_frame) {
  1856. s->current_picture_ptr->repeat_pict = 1;
  1857. }
  1858. }
  1859. *s->current_picture_ptr->pan_scan= s1->pan_scan;
  1860. }else{ //second field
  1861. int i;
  1862. if(!s->current_picture_ptr){
  1863. av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
  1864. return -1;
  1865. }
  1866. for(i=0; i<4; i++){
  1867. s->current_picture.data[i] = s->current_picture_ptr->data[i];
  1868. if(s->picture_structure == PICT_BOTTOM_FIELD){
  1869. s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
  1870. }
  1871. }
  1872. }
  1873. #ifdef HAVE_XVMC
  1874. // MPV_frame_start will call this function too,
  1875. // but we need to call it on every field
  1876. if(s->avctx->xvmc_acceleration)
  1877. XVMC_field_start(s,avctx);
  1878. #endif
  1879. }//fi(s->first_slice)
  1880. init_get_bits(&s->gb, *buf, buf_size*8);
  1881. s->qscale = get_qscale(s);
  1882. if (s->first_slice && (s->first_field || s->picture_structure==PICT_FRAME)) {
  1883. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  1884. 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",
  1885. 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],
  1886. s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")),
  1887. s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
  1888. s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
  1889. s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
  1890. }
  1891. }
  1892. s->first_slice = 0;
  1893. if(s->qscale == 0){
  1894. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  1895. return -1;
  1896. }
  1897. /* extra slice info */
  1898. while (get_bits1(&s->gb) != 0) {
  1899. skip_bits(&s->gb, 8);
  1900. }
  1901. s->mb_x=0;
  1902. for(;;) {
  1903. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1904. if (code < 0){
  1905. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  1906. return -1;
  1907. }
  1908. if (code >= 33) {
  1909. if (code == 33) {
  1910. s->mb_x += 33;
  1911. }
  1912. /* otherwise, stuffing, nothing to do */
  1913. } else {
  1914. s->mb_x += code;
  1915. break;
  1916. }
  1917. }
  1918. s->resync_mb_x= s->mb_x;
  1919. s->resync_mb_y= s->mb_y = start_code;
  1920. s->mb_skip_run= 0;
  1921. ff_init_block_index(s);
  1922. for(;;) {
  1923. #ifdef HAVE_XVMC
  1924. //one 1 we memcpy blocks in xvmcvideo
  1925. if(s->avctx->xvmc_acceleration > 1)
  1926. XVMC_init_block(s);//set s->block
  1927. #endif
  1928. s->dsp.clear_blocks(s->block[0]);
  1929. ret = mpeg_decode_mb(s, s->block);
  1930. s->chroma_qscale= s->qscale;
  1931. dprintf("ret=%d\n", ret);
  1932. if (ret < 0)
  1933. return -1;
  1934. if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
  1935. const int wrap = field_pic ? 2*s->block_wrap[0] : s->block_wrap[0];
  1936. int xy = s->mb_x*2 + 1 + (s->mb_y*2 +1)*wrap;
  1937. int motion_for_top_x, motion_for_top_y, motion_back_top_x, motion_back_top_y;
  1938. int motion_for_bottom_x, motion_for_bottom_y, motion_back_bottom_x, motion_back_bottom_y;
  1939. if(field_pic && !s->first_field)
  1940. xy += wrap/2;
  1941. if (s->mb_intra) {
  1942. motion_for_top_x = motion_for_top_y = motion_back_top_x = motion_back_top_y =
  1943. motion_for_bottom_x = motion_for_bottom_y = motion_back_bottom_x = motion_back_bottom_y = 0;
  1944. }else if (s->mv_type == MV_TYPE_16X16){
  1945. motion_for_top_x = motion_for_bottom_x = s->mv[0][0][0];
  1946. motion_for_top_y = motion_for_bottom_y = s->mv[0][0][1];
  1947. motion_back_top_x = motion_back_bottom_x = s->mv[1][0][0];
  1948. motion_back_top_y = motion_back_bottom_y = s->mv[1][0][1];
  1949. } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
  1950. motion_for_top_x = s->mv[0][0][0];
  1951. motion_for_top_y = s->mv[0][0][1];
  1952. motion_for_bottom_x = s->mv[0][1][0];
  1953. motion_for_bottom_y = s->mv[0][1][1];
  1954. motion_back_top_x = s->mv[1][0][0];
  1955. motion_back_top_y = s->mv[1][0][1];
  1956. motion_back_bottom_x = s->mv[1][1][0];
  1957. motion_back_bottom_y = s->mv[1][1][1];
  1958. }
  1959. s->current_picture.motion_val[0][xy][0] = motion_for_top_x;
  1960. s->current_picture.motion_val[0][xy][1] = motion_for_top_y;
  1961. s->current_picture.motion_val[0][xy + 1][0] = motion_for_top_x;
  1962. s->current_picture.motion_val[0][xy + 1][1] = motion_for_top_y;
  1963. s->current_picture.motion_val[0][xy + wrap][0] = motion_for_bottom_x;
  1964. s->current_picture.motion_val[0][xy + wrap][1] = motion_for_bottom_y;
  1965. s->current_picture.motion_val[0][xy + 1 + wrap][0] = motion_for_bottom_x;
  1966. s->current_picture.motion_val[0][xy + 1 + wrap][1] = motion_for_bottom_y;
  1967. if(s->pict_type != B_TYPE){
  1968. motion_back_top_x = motion_back_top_y = motion_back_bottom_x = motion_back_bottom_y = 0;
  1969. }
  1970. s->current_picture.motion_val[1][xy][0] = motion_back_top_x;
  1971. s->current_picture.motion_val[1][xy][1] = motion_back_top_y;
  1972. s->current_picture.motion_val[1][xy + 1][0] = motion_back_top_x;
  1973. s->current_picture.motion_val[1][xy + 1][1] = motion_back_top_y;
  1974. s->current_picture.motion_val[1][xy + wrap][0] = motion_back_bottom_x;
  1975. s->current_picture.motion_val[1][xy + wrap][1] = motion_back_bottom_y;
  1976. s->current_picture.motion_val[1][xy + 1 + wrap][0] = motion_back_bottom_x;
  1977. s->current_picture.motion_val[1][xy + 1 + wrap][1] = motion_back_bottom_y;
  1978. }
  1979. s->dest[0] += 16;
  1980. s->dest[1] += 8;
  1981. s->dest[2] += 8;
  1982. MPV_decode_mb(s, s->block);
  1983. if (++s->mb_x >= s->mb_width) {
  1984. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  1985. s->mb_x = 0;
  1986. s->mb_y++;
  1987. if(s->mb_y<<field_pic >= s->mb_height){
  1988. int left= s->gb.size_in_bits - get_bits_count(&s->gb);
  1989. if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)))
  1990. || (avctx->error_resilience >= FF_ER_AGGRESSIVE && left>8)){
  1991. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d\n", left);
  1992. return -1;
  1993. }else
  1994. goto eos;
  1995. }
  1996. ff_init_block_index(s);
  1997. }
  1998. /* skip mb handling */
  1999. if (s->mb_skip_run == -1) {
  2000. /* read again increment */
  2001. s->mb_skip_run = 0;
  2002. for(;;) {
  2003. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  2004. if (code < 0){
  2005. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  2006. return -1;
  2007. }
  2008. if (code >= 33) {
  2009. if (code == 33) {
  2010. s->mb_skip_run += 33;
  2011. }else if(code == 35){
  2012. if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
  2013. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  2014. return -1;
  2015. }
  2016. goto eos; /* end of slice */
  2017. }
  2018. /* otherwise, stuffing, nothing to do */
  2019. } else {
  2020. s->mb_skip_run += code;
  2021. break;
  2022. }
  2023. }
  2024. }
  2025. }
  2026. eos: // end of slice
  2027. *buf += get_bits_count(&s->gb)/8 - 1;
  2028. //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  2029. return 0;
  2030. }
  2031. /**
  2032. * handles slice ends.
  2033. * @return 1 if it seems to be the last slice of
  2034. */
  2035. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  2036. {
  2037. Mpeg1Context *s1 = avctx->priv_data;
  2038. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2039. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  2040. return 0;
  2041. #ifdef HAVE_XVMC
  2042. if(s->avctx->xvmc_acceleration)
  2043. XVMC_field_end(s);
  2044. #endif
  2045. /* end of slice reached */
  2046. if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
  2047. /* end of image */
  2048. if(s->codec_id == CODEC_ID_MPEG2VIDEO){
  2049. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
  2050. }else
  2051. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG1;
  2052. ff_er_frame_end(s);
  2053. MPV_frame_end(s);
  2054. if (s->pict_type == B_TYPE || s->low_delay) {
  2055. *pict= *(AVFrame*)s->current_picture_ptr;
  2056. ff_print_debug_info(s, pict);
  2057. } else {
  2058. s->picture_number++;
  2059. /* latency of 1 frame for I and P frames */
  2060. /* XXX: use another variable than picture_number */
  2061. if (s->last_picture_ptr != NULL) {
  2062. *pict= *(AVFrame*)s->last_picture_ptr;
  2063. ff_print_debug_info(s, pict);
  2064. }
  2065. }
  2066. return 1;
  2067. } else {
  2068. return 0;
  2069. }
  2070. }
  2071. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  2072. uint8_t *buf, int buf_size)
  2073. {
  2074. Mpeg1Context *s1 = avctx->priv_data;
  2075. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2076. int width, height, i, v, j;
  2077. float aspect;
  2078. init_get_bits(&s->gb, buf, buf_size*8);
  2079. width = get_bits(&s->gb, 12);
  2080. height = get_bits(&s->gb, 12);
  2081. s->aspect_ratio_info= get_bits(&s->gb, 4);
  2082. if (s->aspect_ratio_info == 0)
  2083. return -1;
  2084. aspect= 1.0/mpeg1_aspect[s->aspect_ratio_info];
  2085. avctx->sample_aspect_ratio= av_d2q(aspect, 255);
  2086. s->frame_rate_index = get_bits(&s->gb, 4);
  2087. if (s->frame_rate_index == 0)
  2088. return -1;
  2089. s->bit_rate = get_bits(&s->gb, 18) * 400;
  2090. if (get_bits1(&s->gb) == 0) /* marker */
  2091. return -1;
  2092. if (width <= 0 || height <= 0 ||
  2093. (width % 2) != 0 || (height % 2) != 0)
  2094. return -1;
  2095. if (width != s->width ||
  2096. height != s->height) {
  2097. /* start new mpeg1 context decoding */
  2098. s->out_format = FMT_MPEG1;
  2099. if (s1->mpeg_enc_ctx_allocated) {
  2100. MPV_common_end(s);
  2101. }
  2102. s->width = width;
  2103. s->height = height;
  2104. avctx->has_b_frames= 1;
  2105. s->avctx = avctx;
  2106. avctx->width = width;
  2107. avctx->height = height;
  2108. av_reduce(
  2109. &avctx->frame_rate,
  2110. &avctx->frame_rate_base,
  2111. frame_rate_tab[s->frame_rate_index],
  2112. MPEG1_FRAME_RATE_BASE, //FIXME store in allready reduced form
  2113. 1<<30
  2114. );
  2115. avctx->bit_rate = s->bit_rate;
  2116. //get_format() or set_video(width,height,aspect,pix_fmt);
  2117. //until then pix_fmt may be changed right after codec init
  2118. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
  2119. if( avctx->idct_algo == FF_IDCT_AUTO )
  2120. avctx->idct_algo = FF_IDCT_SIMPLE;
  2121. if (MPV_common_init(s) < 0)
  2122. return -1;
  2123. s1->mpeg_enc_ctx_allocated = 1;
  2124. s->swap_uv = 0;//just in case vcr2 and mpeg2 stream have been concatinated
  2125. }
  2126. s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
  2127. skip_bits(&s->gb, 1);
  2128. /* get matrix */
  2129. if (get_bits1(&s->gb)) {
  2130. for(i=0;i<64;i++) {
  2131. v = get_bits(&s->gb, 8);
  2132. j = s->intra_scantable.permutated[i];
  2133. s->intra_matrix[j] = v;
  2134. s->chroma_intra_matrix[j] = v;
  2135. }
  2136. #ifdef DEBUG
  2137. dprintf("intra matrix present\n");
  2138. for(i=0;i<64;i++)
  2139. dprintf(" %d", s->intra_matrix[s->intra_scantable.permutated[i]]);
  2140. printf("\n");
  2141. #endif
  2142. } else {
  2143. for(i=0;i<64;i++) {
  2144. int j= s->dsp.idct_permutation[i];
  2145. v = ff_mpeg1_default_intra_matrix[i];
  2146. s->intra_matrix[j] = v;
  2147. s->chroma_intra_matrix[j] = v;
  2148. }
  2149. }
  2150. if (get_bits1(&s->gb)) {
  2151. for(i=0;i<64;i++) {
  2152. v = get_bits(&s->gb, 8);
  2153. j = s->intra_scantable.permutated[i];
  2154. s->inter_matrix[j] = v;
  2155. s->chroma_inter_matrix[j] = v;
  2156. }
  2157. #ifdef DEBUG
  2158. dprintf("non intra matrix present\n");
  2159. for(i=0;i<64;i++)
  2160. dprintf(" %d", s->inter_matrix[s->intra_scantable.permutated[i]]);
  2161. printf("\n");
  2162. #endif
  2163. } else {
  2164. for(i=0;i<64;i++) {
  2165. int j= s->dsp.idct_permutation[i];
  2166. v = ff_mpeg1_default_non_intra_matrix[i];
  2167. s->inter_matrix[j] = v;
  2168. s->chroma_inter_matrix[j] = v;
  2169. }
  2170. }
  2171. /* we set mpeg2 parameters so that it emulates mpeg1 */
  2172. s->progressive_sequence = 1;
  2173. s->progressive_frame = 1;
  2174. s->picture_structure = PICT_FRAME;
  2175. s->frame_pred_frame_dct = 1;
  2176. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
  2177. avctx->sub_id = 1; /* indicates mpeg1 */
  2178. if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
  2179. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  2180. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
  2181. s->avctx->rc_buffer_size, s->bit_rate);
  2182. return 0;
  2183. }
  2184. static int vcr2_init_sequence(AVCodecContext *avctx)
  2185. {
  2186. Mpeg1Context *s1 = avctx->priv_data;
  2187. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2188. int i, v;
  2189. /* start new mpeg1 context decoding */
  2190. s->out_format = FMT_MPEG1;
  2191. if (s1->mpeg_enc_ctx_allocated) {
  2192. MPV_common_end(s);
  2193. }
  2194. s->width = avctx->width;
  2195. s->height = avctx->height;
  2196. avctx->has_b_frames= 0; //true?
  2197. s->low_delay= 1;
  2198. s->avctx = avctx;
  2199. //get_format() or set_video(width,height,aspect,pix_fmt);
  2200. //until then pix_fmt may be changed right after codec init
  2201. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
  2202. if( avctx->idct_algo == FF_IDCT_AUTO )
  2203. avctx->idct_algo = FF_IDCT_SIMPLE;
  2204. if (MPV_common_init(s) < 0)
  2205. return -1;
  2206. exchange_uv(s);//common init reset pblocks, so we swap them here
  2207. s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
  2208. s1->mpeg_enc_ctx_allocated = 1;
  2209. for(i=0;i<64;i++) {
  2210. int j= s->dsp.idct_permutation[i];
  2211. v = ff_mpeg1_default_intra_matrix[i];
  2212. s->intra_matrix[j] = v;
  2213. s->chroma_intra_matrix[j] = v;
  2214. v = ff_mpeg1_default_non_intra_matrix[i];
  2215. s->inter_matrix[j] = v;
  2216. s->chroma_inter_matrix[j] = v;
  2217. }
  2218. s->progressive_sequence = 1;
  2219. s->progressive_frame = 1;
  2220. s->picture_structure = PICT_FRAME;
  2221. s->frame_pred_frame_dct = 1;
  2222. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
  2223. avctx->sub_id = 2; /* indicates mpeg2 */
  2224. return 0;
  2225. }
  2226. static void mpeg_decode_user_data(AVCodecContext *avctx,
  2227. const uint8_t *buf, int buf_size)
  2228. {
  2229. const uint8_t *p;
  2230. int len, flags;
  2231. p = buf;
  2232. len = buf_size;
  2233. /* we parse the DTG active format information */
  2234. if (len >= 5 &&
  2235. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  2236. flags = p[4];
  2237. p += 5;
  2238. len -= 5;
  2239. if (flags & 0x80) {
  2240. /* skip event id */
  2241. if (len < 2)
  2242. return;
  2243. p += 2;
  2244. len -= 2;
  2245. }
  2246. if (flags & 0x40) {
  2247. if (len < 1)
  2248. return;
  2249. avctx->dtg_active_format = p[0] & 0x0f;
  2250. }
  2251. }
  2252. }
  2253. /**
  2254. * finds the end of the current frame in the bitstream.
  2255. * @return the position of the first byte of the next frame, or -1
  2256. */
  2257. static int mpeg1_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
  2258. ParseContext *pc= &s->parse_context;
  2259. int i;
  2260. uint32_t state;
  2261. state= pc->state;
  2262. i=0;
  2263. if(!pc->frame_start_found){
  2264. for(i=0; i<buf_size; i++){
  2265. state= (state<<8) | buf[i];
  2266. if(state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
  2267. i++;
  2268. pc->frame_start_found=1;
  2269. break;
  2270. }
  2271. }
  2272. }
  2273. if(pc->frame_start_found){
  2274. for(; i<buf_size; i++){
  2275. state= (state<<8) | buf[i];
  2276. if((state&0xFFFFFF00) == 0x100){
  2277. if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
  2278. pc->frame_start_found=0;
  2279. pc->state=-1;
  2280. return i-3;
  2281. }
  2282. }
  2283. }
  2284. }
  2285. pc->state= state;
  2286. return END_NOT_FOUND;
  2287. }
  2288. /* handle buffering and image synchronisation */
  2289. static int mpeg_decode_frame(AVCodecContext *avctx,
  2290. void *data, int *data_size,
  2291. uint8_t *buf, int buf_size)
  2292. {
  2293. Mpeg1Context *s = avctx->priv_data;
  2294. uint8_t *buf_end, *buf_ptr;
  2295. int ret, start_code, input_size;
  2296. AVFrame *picture = data;
  2297. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2298. dprintf("fill_buffer\n");
  2299. *data_size = 0;
  2300. /* special case for last picture */
  2301. if (buf_size == 0 && s2->low_delay==0 && s2->next_picture_ptr) {
  2302. *picture= *(AVFrame*)s2->next_picture_ptr;
  2303. s2->next_picture_ptr= NULL;
  2304. *data_size = sizeof(AVFrame);
  2305. return 0;
  2306. }
  2307. if(s2->flags&CODEC_FLAG_TRUNCATED){
  2308. int next= mpeg1_find_frame_end(s2, buf, buf_size);
  2309. if( ff_combine_frame(s2, next, &buf, &buf_size) < 0 )
  2310. return buf_size;
  2311. }
  2312. buf_ptr = buf;
  2313. buf_end = buf + buf_size;
  2314. #if 0
  2315. if (s->repeat_field % 2 == 1) {
  2316. s->repeat_field++;
  2317. //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
  2318. // s2->picture_number, s->repeat_field);
  2319. if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
  2320. *data_size = sizeof(AVPicture);
  2321. goto the_end;
  2322. }
  2323. }
  2324. #endif
  2325. if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == ff_get_fourcc("VCR2"))
  2326. vcr2_init_sequence(avctx);
  2327. for(;;) {
  2328. /* find start next code */
  2329. start_code = find_start_code(&buf_ptr, buf_end);
  2330. if (start_code < 0){
  2331. if(s2->pict_type != B_TYPE || avctx->hurry_up==0){
  2332. if (slice_end(avctx, picture)) {
  2333. if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
  2334. *data_size = sizeof(AVPicture);
  2335. }
  2336. }
  2337. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  2338. }
  2339. input_size = buf_end - buf_ptr;
  2340. if(avctx->debug & FF_DEBUG_STARTCODE){
  2341. av_log(avctx, AV_LOG_DEBUG, "%3X at %d left %d\n", start_code, buf_ptr-buf, input_size);
  2342. }
  2343. /* prepare data for next start code */
  2344. switch(start_code) {
  2345. case SEQ_START_CODE:
  2346. mpeg1_decode_sequence(avctx, buf_ptr,
  2347. input_size);
  2348. break;
  2349. case PICTURE_START_CODE:
  2350. /* we have a complete image : we try to decompress it */
  2351. mpeg1_decode_picture(avctx,
  2352. buf_ptr, input_size);
  2353. break;
  2354. case EXT_START_CODE:
  2355. mpeg_decode_extension(avctx,
  2356. buf_ptr, input_size);
  2357. break;
  2358. case USER_START_CODE:
  2359. mpeg_decode_user_data(avctx,
  2360. buf_ptr, input_size);
  2361. break;
  2362. case GOP_START_CODE:
  2363. s2->first_field=0;
  2364. break;
  2365. default:
  2366. if (start_code >= SLICE_MIN_START_CODE &&
  2367. start_code <= SLICE_MAX_START_CODE) {
  2368. /* skip b frames if we dont have reference frames */
  2369. if(s2->last_picture_ptr==NULL && s2->pict_type==B_TYPE) break;
  2370. /* skip b frames if we are in a hurry */
  2371. if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
  2372. /* skip everything if we are in a hurry>=5 */
  2373. if(avctx->hurry_up>=5) break;
  2374. if (!s->mpeg_enc_ctx_allocated) break;
  2375. ret = mpeg_decode_slice(avctx, picture,
  2376. start_code, &buf_ptr, input_size);
  2377. emms_c();
  2378. if(ret < 0){
  2379. if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
  2380. 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);
  2381. if(ret==DECODE_SLICE_FATAL_ERROR) return -1;
  2382. }else{
  2383. 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);
  2384. }
  2385. }
  2386. break;
  2387. }
  2388. }
  2389. }
  2390. static int mpeg_decode_end(AVCodecContext *avctx)
  2391. {
  2392. Mpeg1Context *s = avctx->priv_data;
  2393. if (s->mpeg_enc_ctx_allocated)
  2394. MPV_common_end(&s->mpeg_enc_ctx);
  2395. return 0;
  2396. }
  2397. AVCodec mpeg1video_decoder = {
  2398. "mpeg1video",
  2399. CODEC_TYPE_VIDEO,
  2400. CODEC_ID_MPEG1VIDEO,
  2401. sizeof(Mpeg1Context),
  2402. mpeg_decode_init,
  2403. NULL,
  2404. mpeg_decode_end,
  2405. mpeg_decode_frame,
  2406. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  2407. .flush= ff_mpeg_flush,
  2408. };
  2409. AVCodec mpeg2video_decoder = {
  2410. "mpeg2video",
  2411. CODEC_TYPE_VIDEO,
  2412. CODEC_ID_MPEG2VIDEO,
  2413. sizeof(Mpeg1Context),
  2414. mpeg_decode_init,
  2415. NULL,
  2416. mpeg_decode_end,
  2417. mpeg_decode_frame,
  2418. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  2419. .flush= ff_mpeg_flush,
  2420. };
  2421. //legacy decoder
  2422. AVCodec mpegvideo_decoder = {
  2423. "mpegvideo",
  2424. CODEC_TYPE_VIDEO,
  2425. CODEC_ID_MPEG2VIDEO,
  2426. sizeof(Mpeg1Context),
  2427. mpeg_decode_init,
  2428. NULL,
  2429. mpeg_decode_end,
  2430. mpeg_decode_frame,
  2431. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  2432. .flush= ff_mpeg_flush,
  2433. };
  2434. #ifdef HAVE_XVMC
  2435. static int mpeg_mc_decode_init(AVCodecContext *avctx){
  2436. Mpeg1Context *s;
  2437. if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
  2438. return -1;
  2439. if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
  2440. dprintf("mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
  2441. }
  2442. mpeg_decode_init(avctx);
  2443. s = avctx->priv_data;
  2444. avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
  2445. avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
  2446. return 0;
  2447. }
  2448. AVCodec mpeg_xvmc_decoder = {
  2449. "mpegvideo_xvmc",
  2450. CODEC_TYPE_VIDEO,
  2451. CODEC_ID_MPEG2VIDEO_XVMC,
  2452. sizeof(Mpeg1Context),
  2453. mpeg_mc_decode_init,
  2454. NULL,
  2455. mpeg_decode_end,
  2456. mpeg_decode_frame,
  2457. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  2458. };
  2459. #endif
  2460. /* this is ugly i know, but the alternative is too make
  2461. hundreds of vars global and prefix them with ff_mpeg1_
  2462. which is far uglier. */
  2463. #include "mdec.c"