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.

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