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.

2758 lines
93KB

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