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.

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