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.

2595 lines
87KB

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