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.

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