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.

2709 lines
91KB

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