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.

2793 lines
94KB

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