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.

2754 lines
93KB

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