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.

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