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.

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