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.

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