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.

2173 lines
73KB

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