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.

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