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.

2486 lines
83KB

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