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.

2403 lines
80KB

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