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.

2231 lines
74KB

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