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.

2775 lines
94KB

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