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.

2778 lines
93KB

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