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.

2694 lines
90KB

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