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.

3198 lines
107KB

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