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.

2667 lines
89KB

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