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.

2582 lines
86KB

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