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.

2732 lines
92KB

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