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.

2234 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 + 399) / 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_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_incr != 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. s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
  1620. s->last_dc[1] = s->last_dc[0];
  1621. s->last_dc[2] = s->last_dc[0];
  1622. memset(s->last_mv, 0, sizeof(s->last_mv));
  1623. /* start frame decoding */
  1624. if (s->first_slice) {
  1625. if(s->first_field || s->picture_structure==PICT_FRAME){
  1626. if(MPV_frame_start(s, avctx) < 0)
  1627. return DECODE_SLICE_FATAL_ERROR;
  1628. /* first check if we must repeat the frame */
  1629. s->current_picture.repeat_pict = 0;
  1630. if (s->repeat_first_field) {
  1631. if (s->progressive_sequence) {
  1632. if (s->top_field_first)
  1633. s->current_picture.repeat_pict = 4;
  1634. else
  1635. s->current_picture.repeat_pict = 2;
  1636. } else if (s->progressive_frame) {
  1637. s->current_picture.repeat_pict = 1;
  1638. }
  1639. }
  1640. // printf("%d \n", s->current_picture.repeat_pict);
  1641. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  1642. printf("qp:%d fc:%2d%2d%2d%2d %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
  1643. s->qscale, s->mpeg_f_code[0][0],s->mpeg_f_code[0][1],s->mpeg_f_code[1][0],s->mpeg_f_code[1][1],
  1644. s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")),
  1645. s->progressive_sequence ? "pro" :"", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
  1646. s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
  1647. s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
  1648. }
  1649. }else{ //second field
  1650. int i;
  1651. for(i=0; i<4; i++){
  1652. s->current_picture.data[i] = s->current_picture_ptr->data[i];
  1653. if(s->picture_structure == PICT_BOTTOM_FIELD){
  1654. s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
  1655. }
  1656. }
  1657. }
  1658. }
  1659. s->first_slice = 0;
  1660. init_get_bits(&s->gb, buf, buf_size*8);
  1661. s->qscale = get_qscale(s);
  1662. /* extra slice info */
  1663. while (get_bits1(&s->gb) != 0) {
  1664. skip_bits(&s->gb, 8);
  1665. }
  1666. s->mb_x=0;
  1667. for(;;) {
  1668. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1669. if (code < 0)
  1670. return -1; /* error = end of slice, but empty slice is bad or?*/
  1671. if (code >= 33) {
  1672. if (code == 33) {
  1673. s->mb_x += 33;
  1674. }
  1675. /* otherwise, stuffing, nothing to do */
  1676. } else {
  1677. s->mb_x += code;
  1678. break;
  1679. }
  1680. }
  1681. s->mb_y = start_code;
  1682. s->mb_incr= 1;
  1683. for(;;) {
  1684. s->dsp.clear_blocks(s->block[0]);
  1685. ret = mpeg_decode_mb(s, s->block);
  1686. dprintf("ret=%d\n", ret);
  1687. if (ret < 0)
  1688. return -1;
  1689. MPV_decode_mb(s, s->block);
  1690. if (++s->mb_x >= s->mb_width) {
  1691. if(s->picture_structure==PICT_FRAME){
  1692. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  1693. }else{
  1694. if(!s->first_field){
  1695. ff_draw_horiz_band(s, 32*s->mb_y, 32);
  1696. }
  1697. }
  1698. s->mb_x = 0;
  1699. s->mb_y++;
  1700. PRINT_QP("%s", "\n");
  1701. }
  1702. PRINT_QP("%2d", s->qscale);
  1703. /* skip mb handling */
  1704. if (s->mb_incr == 0) {
  1705. /* read again increment */
  1706. s->mb_incr = 1;
  1707. for(;;) {
  1708. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1709. if (code < 0)
  1710. goto eos; /* error = end of slice */
  1711. if (code >= 33) {
  1712. if (code == 33) {
  1713. s->mb_incr += 33;
  1714. }
  1715. /* otherwise, stuffing, nothing to do */
  1716. } else {
  1717. s->mb_incr += code;
  1718. break;
  1719. }
  1720. }
  1721. }
  1722. if(s->mb_y<<field_pic >= s->mb_height){
  1723. fprintf(stderr, "slice too long\n");
  1724. return DECODE_SLICE_ERROR;
  1725. }
  1726. }
  1727. eos: //end of slice
  1728. emms_c();
  1729. //intf("%d %d %d %d\n", s->mb_y, s->mb_height, s->pict_type, s->picture_number);
  1730. /* end of slice reached */
  1731. if (s->mb_y<<field_pic == s->mb_height && !s->first_field) {
  1732. /* end of image */
  1733. if(s->mpeg2)
  1734. s->qscale >>=1;
  1735. MPV_frame_end(s);
  1736. if (s->pict_type == B_TYPE || s->low_delay) {
  1737. *pict= *(AVFrame*)&s->current_picture;
  1738. } else {
  1739. s->picture_number++;
  1740. /* latency of 1 frame for I and P frames */
  1741. /* XXX: use another variable than picture_number */
  1742. if (s->last_picture_ptr == NULL) {
  1743. return DECODE_SLICE_OK;
  1744. } else {
  1745. *pict= *(AVFrame*)&s->last_picture;
  1746. }
  1747. }
  1748. return DECODE_SLICE_EOP;
  1749. } else {
  1750. return DECODE_SLICE_OK;
  1751. }
  1752. }
  1753. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1754. uint8_t *buf, int buf_size)
  1755. {
  1756. Mpeg1Context *s1 = avctx->priv_data;
  1757. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1758. int width, height, i, v, j;
  1759. float aspect;
  1760. init_get_bits(&s->gb, buf, buf_size*8);
  1761. width = get_bits(&s->gb, 12);
  1762. height = get_bits(&s->gb, 12);
  1763. s->aspect_ratio_info= get_bits(&s->gb, 4);
  1764. if(!s->mpeg2){
  1765. aspect= mpeg1_aspect[s->aspect_ratio_info];
  1766. if(aspect!=0.0) avctx->aspect_ratio= width/(aspect*height);
  1767. }
  1768. s->frame_rate_index = get_bits(&s->gb, 4);
  1769. if (s->frame_rate_index == 0)
  1770. return -1;
  1771. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1772. if (get_bits1(&s->gb) == 0) /* marker */
  1773. return -1;
  1774. if (width <= 0 || height <= 0 ||
  1775. (width % 2) != 0 || (height % 2) != 0)
  1776. return -1;
  1777. if (width != s->width ||
  1778. height != s->height) {
  1779. /* start new mpeg1 context decoding */
  1780. s->out_format = FMT_MPEG1;
  1781. if (s1->mpeg_enc_ctx_allocated) {
  1782. MPV_common_end(s);
  1783. }
  1784. s->width = width;
  1785. s->height = height;
  1786. avctx->has_b_frames= 1;
  1787. s->avctx = avctx;
  1788. avctx->width = width;
  1789. avctx->height = height;
  1790. av_reduce(
  1791. &avctx->frame_rate,
  1792. &avctx->frame_rate_base,
  1793. frame_rate_tab[s->frame_rate_index],
  1794. MPEG1_FRAME_RATE_BASE, //FIXME store in allready reduced form
  1795. 1<<30
  1796. );
  1797. avctx->bit_rate = s->bit_rate;
  1798. if (MPV_common_init(s) < 0)
  1799. return -1;
  1800. s1->mpeg_enc_ctx_allocated = 1;
  1801. }
  1802. skip_bits(&s->gb, 10); /* vbv_buffer_size */
  1803. skip_bits(&s->gb, 1);
  1804. /* get matrix */
  1805. if (get_bits1(&s->gb)) {
  1806. for(i=0;i<64;i++) {
  1807. v = get_bits(&s->gb, 8);
  1808. j = s->intra_scantable.permutated[i];
  1809. s->intra_matrix[j] = v;
  1810. s->chroma_intra_matrix[j] = v;
  1811. }
  1812. #ifdef DEBUG
  1813. dprintf("intra matrix present\n");
  1814. for(i=0;i<64;i++)
  1815. dprintf(" %d", s->intra_matrix[s->intra_scantable.permutated[i]]);
  1816. printf("\n");
  1817. #endif
  1818. } else {
  1819. for(i=0;i<64;i++) {
  1820. int j= s->dsp.idct_permutation[i];
  1821. v = ff_mpeg1_default_intra_matrix[i];
  1822. s->intra_matrix[j] = v;
  1823. s->chroma_intra_matrix[j] = v;
  1824. }
  1825. }
  1826. if (get_bits1(&s->gb)) {
  1827. for(i=0;i<64;i++) {
  1828. v = get_bits(&s->gb, 8);
  1829. j = s->intra_scantable.permutated[i];
  1830. s->inter_matrix[j] = v;
  1831. s->chroma_inter_matrix[j] = v;
  1832. }
  1833. #ifdef DEBUG
  1834. dprintf("non intra matrix present\n");
  1835. for(i=0;i<64;i++)
  1836. dprintf(" %d", s->inter_matrix[s->intra_scantable.permutated[i]]);
  1837. printf("\n");
  1838. #endif
  1839. } else {
  1840. for(i=0;i<64;i++) {
  1841. int j= s->dsp.idct_permutation[i];
  1842. v = ff_mpeg1_default_non_intra_matrix[i];
  1843. s->inter_matrix[j] = v;
  1844. s->chroma_inter_matrix[j] = v;
  1845. }
  1846. }
  1847. /* we set mpeg2 parameters so that it emulates mpeg1 */
  1848. s->progressive_sequence = 1;
  1849. s->progressive_frame = 1;
  1850. s->picture_structure = PICT_FRAME;
  1851. s->frame_pred_frame_dct = 1;
  1852. s->mpeg2 = 0;
  1853. avctx->sub_id = 1; /* indicates mpeg1 */
  1854. return 0;
  1855. }
  1856. static void mpeg_decode_user_data(AVCodecContext *avctx,
  1857. const uint8_t *buf, int buf_size)
  1858. {
  1859. const uint8_t *p;
  1860. int len, flags;
  1861. p = buf;
  1862. len = buf_size;
  1863. /* we parse the DTG active format information */
  1864. if (len >= 5 &&
  1865. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  1866. flags = p[4];
  1867. p += 5;
  1868. len -= 5;
  1869. if (flags & 0x80) {
  1870. /* skip event id */
  1871. if (len < 2)
  1872. return;
  1873. p += 2;
  1874. len -= 2;
  1875. }
  1876. if (flags & 0x40) {
  1877. if (len < 1)
  1878. return;
  1879. avctx->dtg_active_format = p[0] & 0x0f;
  1880. }
  1881. }
  1882. }
  1883. /* handle buffering and image synchronisation */
  1884. static int mpeg_decode_frame(AVCodecContext *avctx,
  1885. void *data, int *data_size,
  1886. uint8_t *buf, int buf_size)
  1887. {
  1888. Mpeg1Context *s = avctx->priv_data;
  1889. uint8_t *buf_end, *buf_ptr, *buf_start;
  1890. int len, start_code_found, ret, code, start_code, input_size;
  1891. AVFrame *picture = data;
  1892. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1893. dprintf("fill_buffer\n");
  1894. *data_size = 0;
  1895. /* special case for last picture */
  1896. if (buf_size == 0) {
  1897. if (s2->picture_number > 0) {
  1898. *picture= *(AVFrame*)&s2->next_picture;
  1899. *data_size = sizeof(AVFrame);
  1900. }
  1901. return 0;
  1902. }
  1903. buf_ptr = buf;
  1904. buf_end = buf + buf_size;
  1905. #if 0
  1906. if (s->repeat_field % 2 == 1) {
  1907. s->repeat_field++;
  1908. //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
  1909. // s2->picture_number, s->repeat_field);
  1910. if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
  1911. *data_size = sizeof(AVPicture);
  1912. goto the_end;
  1913. }
  1914. }
  1915. #endif
  1916. while (buf_ptr < buf_end) {
  1917. buf_start = buf_ptr;
  1918. /* find start next code */
  1919. code = find_start_code(&buf_ptr, buf_end, &s->header_state);
  1920. if (code >= 0) {
  1921. start_code_found = 1;
  1922. } else {
  1923. start_code_found = 0;
  1924. }
  1925. /* copy to buffer */
  1926. len = buf_ptr - buf_start;
  1927. if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
  1928. /* data too big : flush */
  1929. s->buf_ptr = s->buffer;
  1930. if (start_code_found)
  1931. s->start_code = code;
  1932. } else {
  1933. memcpy(s->buf_ptr, buf_start, len);
  1934. s->buf_ptr += len;
  1935. if( (!(s2->flags&CODEC_FLAG_TRUNCATED)) && (!start_code_found)
  1936. && s->buf_ptr+4<s->buffer+s->buffer_size){
  1937. start_code_found= 1;
  1938. code= 0x1FF;
  1939. s->header_state=0xFF;
  1940. s->buf_ptr[0]=0;
  1941. s->buf_ptr[1]=0;
  1942. s->buf_ptr[2]=1;
  1943. s->buf_ptr[3]=0xFF;
  1944. s->buf_ptr+=4;
  1945. }
  1946. if (start_code_found) {
  1947. /* prepare data for next start code */
  1948. input_size = s->buf_ptr - s->buffer;
  1949. start_code = s->start_code;
  1950. s->buf_ptr = s->buffer;
  1951. s->start_code = code;
  1952. switch(start_code) {
  1953. case SEQ_START_CODE:
  1954. mpeg1_decode_sequence(avctx, s->buffer,
  1955. input_size);
  1956. break;
  1957. case PICTURE_START_CODE:
  1958. /* we have a complete image : we try to decompress it */
  1959. mpeg1_decode_picture(avctx,
  1960. s->buffer, input_size);
  1961. break;
  1962. case EXT_START_CODE:
  1963. mpeg_decode_extension(avctx,
  1964. s->buffer, input_size);
  1965. break;
  1966. case USER_START_CODE:
  1967. mpeg_decode_user_data(avctx,
  1968. s->buffer, input_size);
  1969. break;
  1970. default:
  1971. if (start_code >= SLICE_MIN_START_CODE &&
  1972. start_code <= SLICE_MAX_START_CODE) {
  1973. /* skip b frames if we dont have reference frames */
  1974. if(s2->last_picture_ptr==NULL && s2->pict_type==B_TYPE) break;
  1975. /* skip b frames if we are in a hurry */
  1976. if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
  1977. /* skip everything if we are in a hurry>=5 */
  1978. if(avctx->hurry_up>=5) break;
  1979. ret = mpeg_decode_slice(avctx, picture,
  1980. start_code, s->buffer, input_size);
  1981. if (ret == DECODE_SLICE_EOP) {
  1982. *data_size = sizeof(AVPicture);
  1983. goto the_end;
  1984. }else if(ret<0){
  1985. fprintf(stderr,"Error while decoding slice\n");
  1986. if(ret==DECODE_SLICE_FATAL_ERROR) return -1;
  1987. }
  1988. }
  1989. break;
  1990. }
  1991. }
  1992. }
  1993. }
  1994. the_end:
  1995. return buf_ptr - buf;
  1996. }
  1997. static int mpeg_decode_end(AVCodecContext *avctx)
  1998. {
  1999. Mpeg1Context *s = avctx->priv_data;
  2000. if (s->mpeg_enc_ctx_allocated)
  2001. MPV_common_end(&s->mpeg_enc_ctx);
  2002. return 0;
  2003. }
  2004. AVCodec mpeg_decoder = {
  2005. "mpegvideo",
  2006. CODEC_TYPE_VIDEO,
  2007. CODEC_ID_MPEG1VIDEO,
  2008. sizeof(Mpeg1Context),
  2009. mpeg_decode_init,
  2010. NULL,
  2011. mpeg_decode_end,
  2012. mpeg_decode_frame,
  2013. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  2014. };