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.

2232 lines
74KB

  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. s->mv_type = MV_TYPE_FIELD;
  914. if (s->picture_structure == PICT_FRAME) {
  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->field_select[i][0] = get_bits1(&s->gb);
  930. for(k=0;k<2;k++) {
  931. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  932. s->last_mv[i][0][k]);
  933. s->last_mv[i][0][k] = val;
  934. s->last_mv[i][1][k] = val;
  935. s->mv[i][0][k] = val;
  936. }
  937. }
  938. break;
  939. case MT_DMV:
  940. {
  941. int dmx, dmy, mx, my, m;
  942. mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  943. s->last_mv[i][0][0]);
  944. s->last_mv[i][0][0] = mx;
  945. s->last_mv[i][1][0] = mx;
  946. dmx = get_dmv(s);
  947. my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  948. s->last_mv[i][0][1] >> 1);
  949. dmy = get_dmv(s);
  950. s->mv_type = MV_TYPE_DMV;
  951. /* XXX: totally broken */
  952. if (s->picture_structure == PICT_FRAME) {
  953. s->last_mv[i][0][1] = my << 1;
  954. s->last_mv[i][1][1] = my << 1;
  955. m = s->top_field_first ? 1 : 3;
  956. /* top -> top pred */
  957. s->mv[i][0][0] = mx;
  958. s->mv[i][0][1] = my << 1;
  959. s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  960. s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
  961. m = 4 - m;
  962. s->mv[i][2][0] = mx;
  963. s->mv[i][2][1] = my << 1;
  964. s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  965. s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
  966. } else {
  967. s->last_mv[i][0][1] = my;
  968. s->last_mv[i][1][1] = my;
  969. s->mv[i][0][0] = mx;
  970. s->mv[i][0][1] = my;
  971. s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx;
  972. s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1
  973. /* + 2 * cur_field */;
  974. }
  975. }
  976. break;
  977. }
  978. }
  979. }
  980. }
  981. if ((mb_type & MB_INTRA) && s->concealment_motion_vectors) {
  982. skip_bits1(&s->gb); /* marker */
  983. }
  984. if (mb_type & MB_PAT) {
  985. cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
  986. if (cbp < 0){
  987. fprintf(stderr, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
  988. return -1;
  989. }
  990. cbp++;
  991. }
  992. dprintf("cbp=%x\n", cbp);
  993. if (s->mpeg2) {
  994. if (s->mb_intra) {
  995. for(i=0;i<6;i++) {
  996. if (mpeg2_decode_block_intra(s, block[i], i) < 0)
  997. return -1;
  998. }
  999. } else {
  1000. for(i=0;i<6;i++) {
  1001. if (cbp & 32) {
  1002. if (mpeg2_decode_block_non_intra(s, block[i], i) < 0)
  1003. return -1;
  1004. } else {
  1005. s->block_last_index[i] = -1;
  1006. }
  1007. cbp+=cbp;
  1008. }
  1009. }
  1010. } else {
  1011. if (s->mb_intra) {
  1012. for(i=0;i<6;i++) {
  1013. if (mpeg1_decode_block_intra(s, block[i], i) < 0)
  1014. return -1;
  1015. }
  1016. }else{
  1017. for(i=0;i<6;i++) {
  1018. if (cbp & 32) {
  1019. if (mpeg1_decode_block_inter(s, block[i], i) < 0)
  1020. return -1;
  1021. } else {
  1022. s->block_last_index[i] = -1;
  1023. }
  1024. cbp+=cbp;
  1025. }
  1026. }
  1027. }
  1028. return 0;
  1029. }
  1030. /* as h263, but only 17 codes */
  1031. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
  1032. {
  1033. int code, sign, val, m, l, shift;
  1034. code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
  1035. if (code < 0) {
  1036. return 0xffff;
  1037. }
  1038. if (code == 0) {
  1039. return pred;
  1040. }
  1041. sign = get_bits1(&s->gb);
  1042. shift = fcode - 1;
  1043. val = (code - 1) << shift;
  1044. if (shift > 0)
  1045. val |= get_bits(&s->gb, shift);
  1046. val++;
  1047. if (sign)
  1048. val = -val;
  1049. val += pred;
  1050. /* modulo decoding */
  1051. l = (1 << shift) * 16;
  1052. m = 2 * l;
  1053. if (val < -l) {
  1054. val += m;
  1055. } else if (val >= l) {
  1056. val -= m;
  1057. }
  1058. return val;
  1059. }
  1060. static inline int decode_dc(MpegEncContext *s, int component)
  1061. {
  1062. int code, diff;
  1063. if (component == 0) {
  1064. code = get_vlc2(&s->gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
  1065. } else {
  1066. code = get_vlc2(&s->gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
  1067. }
  1068. if (code < 0){
  1069. fprintf(stderr, "invalid dc code at %d %d\n", s->mb_x, s->mb_y);
  1070. return 0xffff;
  1071. }
  1072. if (code == 0) {
  1073. diff = 0;
  1074. } else {
  1075. diff = get_bits(&s->gb, code);
  1076. if ((diff & (1 << (code - 1))) == 0)
  1077. diff = (-1 << code) | (diff + 1);
  1078. }
  1079. return diff;
  1080. }
  1081. static inline int mpeg1_decode_block_intra(MpegEncContext *s,
  1082. DCTELEM *block,
  1083. int n)
  1084. {
  1085. int level, dc, diff, i, j, run;
  1086. int component;
  1087. RLTable *rl = &rl_mpeg1;
  1088. uint8_t * const scantable= s->intra_scantable.permutated;
  1089. const uint16_t *quant_matrix= s->intra_matrix;
  1090. const int qscale= s->qscale;
  1091. /* DC coef */
  1092. component = (n <= 3 ? 0 : n - 4 + 1);
  1093. diff = decode_dc(s, component);
  1094. if (diff >= 0xffff)
  1095. return -1;
  1096. dc = s->last_dc[component];
  1097. dc += diff;
  1098. s->last_dc[component] = dc;
  1099. block[0] = dc<<3;
  1100. dprintf("dc=%d diff=%d\n", dc, diff);
  1101. i = 0;
  1102. {
  1103. OPEN_READER(re, &s->gb);
  1104. /* now quantify & encode AC coefs */
  1105. for(;;) {
  1106. UPDATE_CACHE(re, &s->gb);
  1107. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  1108. if(level == 127){
  1109. break;
  1110. } else if(level != 0) {
  1111. i += run;
  1112. j = scantable[i];
  1113. level= (level*qscale*quant_matrix[j])>>3;
  1114. level= (level-1)|1;
  1115. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1116. LAST_SKIP_BITS(re, &s->gb, 1);
  1117. } else {
  1118. /* escape */
  1119. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1120. UPDATE_CACHE(re, &s->gb);
  1121. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  1122. if (level == -128) {
  1123. level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
  1124. } else if (level == 0) {
  1125. level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
  1126. }
  1127. i += run;
  1128. j = scantable[i];
  1129. if(level<0){
  1130. level= -level;
  1131. level= (level*qscale*quant_matrix[j])>>3;
  1132. level= (level-1)|1;
  1133. level= -level;
  1134. }else{
  1135. level= (level*qscale*quant_matrix[j])>>3;
  1136. level= (level-1)|1;
  1137. }
  1138. }
  1139. if (i > 63){
  1140. fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1141. return -1;
  1142. }
  1143. block[j] = level;
  1144. }
  1145. CLOSE_READER(re, &s->gb);
  1146. }
  1147. s->block_last_index[n] = i;
  1148. return 0;
  1149. }
  1150. static inline int mpeg1_decode_block_inter(MpegEncContext *s,
  1151. DCTELEM *block,
  1152. int n)
  1153. {
  1154. int level, i, j, run;
  1155. RLTable *rl = &rl_mpeg1;
  1156. uint8_t * const scantable= s->intra_scantable.permutated;
  1157. const uint16_t *quant_matrix= s->inter_matrix;
  1158. const int qscale= s->qscale;
  1159. {
  1160. int v;
  1161. OPEN_READER(re, &s->gb);
  1162. i = -1;
  1163. /* special case for the first coef. no need to add a second vlc table */
  1164. UPDATE_CACHE(re, &s->gb);
  1165. v= SHOW_UBITS(re, &s->gb, 2);
  1166. if (v & 2) {
  1167. LAST_SKIP_BITS(re, &s->gb, 2);
  1168. level= (3*qscale*quant_matrix[0])>>4;
  1169. level= (level-1)|1;
  1170. if(v&1)
  1171. level= -level;
  1172. block[0] = level;
  1173. i++;
  1174. }
  1175. /* now quantify & encode AC coefs */
  1176. for(;;) {
  1177. UPDATE_CACHE(re, &s->gb);
  1178. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  1179. if(level == 127){
  1180. break;
  1181. } else if(level != 0) {
  1182. i += run;
  1183. j = scantable[i];
  1184. level= ((level*2+1)*qscale*quant_matrix[j])>>4;
  1185. level= (level-1)|1;
  1186. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1187. LAST_SKIP_BITS(re, &s->gb, 1);
  1188. } else {
  1189. /* escape */
  1190. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1191. UPDATE_CACHE(re, &s->gb);
  1192. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  1193. if (level == -128) {
  1194. level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
  1195. } else if (level == 0) {
  1196. level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
  1197. }
  1198. i += run;
  1199. j = scantable[i];
  1200. if(level<0){
  1201. level= -level;
  1202. level= ((level*2+1)*qscale*quant_matrix[j])>>4;
  1203. level= (level-1)|1;
  1204. level= -level;
  1205. }else{
  1206. level= ((level*2+1)*qscale*quant_matrix[j])>>4;
  1207. level= (level-1)|1;
  1208. }
  1209. }
  1210. if (i > 63){
  1211. fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1212. return -1;
  1213. }
  1214. block[j] = level;
  1215. }
  1216. CLOSE_READER(re, &s->gb);
  1217. }
  1218. s->block_last_index[n] = i;
  1219. return 0;
  1220. }
  1221. /* Also does unquantization here, since I will never support mpeg2
  1222. encoding */
  1223. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
  1224. DCTELEM *block,
  1225. int n)
  1226. {
  1227. int level, i, j, run;
  1228. RLTable *rl = &rl_mpeg1;
  1229. uint8_t * const scantable= s->intra_scantable.permutated;
  1230. const uint16_t *quant_matrix;
  1231. const int qscale= s->qscale;
  1232. int mismatch;
  1233. mismatch = 1;
  1234. {
  1235. int v;
  1236. OPEN_READER(re, &s->gb);
  1237. i = -1;
  1238. if (n < 4)
  1239. quant_matrix = s->inter_matrix;
  1240. else
  1241. quant_matrix = s->chroma_inter_matrix;
  1242. /* special case for the first coef. no need to add a second vlc table */
  1243. UPDATE_CACHE(re, &s->gb);
  1244. v= SHOW_UBITS(re, &s->gb, 2);
  1245. if (v & 2) {
  1246. LAST_SKIP_BITS(re, &s->gb, 2);
  1247. level= (3*qscale*quant_matrix[0])>>5;
  1248. if(v&1)
  1249. level= -level;
  1250. block[0] = level;
  1251. mismatch ^= level;
  1252. i++;
  1253. }
  1254. /* now quantify & encode AC coefs */
  1255. for(;;) {
  1256. UPDATE_CACHE(re, &s->gb);
  1257. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  1258. if(level == 127){
  1259. break;
  1260. } else if(level != 0) {
  1261. i += run;
  1262. j = scantable[i];
  1263. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1264. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1265. LAST_SKIP_BITS(re, &s->gb, 1);
  1266. } else {
  1267. /* escape */
  1268. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1269. UPDATE_CACHE(re, &s->gb);
  1270. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1271. i += run;
  1272. j = scantable[i];
  1273. if(level<0){
  1274. level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
  1275. level= -level;
  1276. }else{
  1277. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1278. }
  1279. }
  1280. if (i > 63){
  1281. fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1282. return -1;
  1283. }
  1284. mismatch ^= level;
  1285. block[j] = level;
  1286. }
  1287. CLOSE_READER(re, &s->gb);
  1288. }
  1289. block[63] ^= (mismatch & 1);
  1290. s->block_last_index[n] = i;
  1291. return 0;
  1292. }
  1293. static inline int mpeg2_decode_block_intra(MpegEncContext *s,
  1294. DCTELEM *block,
  1295. int n)
  1296. {
  1297. int level, dc, diff, i, j, run;
  1298. int component;
  1299. RLTable *rl;
  1300. uint8_t * const scantable= s->intra_scantable.permutated;
  1301. const uint16_t *quant_matrix;
  1302. const int qscale= s->qscale;
  1303. int mismatch;
  1304. /* DC coef */
  1305. if (n < 4){
  1306. quant_matrix = s->intra_matrix;
  1307. component = 0;
  1308. }else{
  1309. quant_matrix = s->chroma_intra_matrix;
  1310. component = n - 3;
  1311. }
  1312. diff = decode_dc(s, component);
  1313. if (diff >= 0xffff)
  1314. return -1;
  1315. dc = s->last_dc[component];
  1316. dc += diff;
  1317. s->last_dc[component] = dc;
  1318. block[0] = dc << (3 - s->intra_dc_precision);
  1319. dprintf("dc=%d\n", block[0]);
  1320. mismatch = block[0] ^ 1;
  1321. i = 0;
  1322. if (s->intra_vlc_format)
  1323. rl = &rl_mpeg2;
  1324. else
  1325. rl = &rl_mpeg1;
  1326. {
  1327. OPEN_READER(re, &s->gb);
  1328. /* now quantify & encode AC coefs */
  1329. for(;;) {
  1330. UPDATE_CACHE(re, &s->gb);
  1331. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
  1332. if(level == 127){
  1333. break;
  1334. } else if(level != 0) {
  1335. i += run;
  1336. j = scantable[i];
  1337. level= (level*qscale*quant_matrix[j])>>4;
  1338. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1339. LAST_SKIP_BITS(re, &s->gb, 1);
  1340. } else {
  1341. /* escape */
  1342. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1343. UPDATE_CACHE(re, &s->gb);
  1344. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1345. i += run;
  1346. j = scantable[i];
  1347. if(level<0){
  1348. level= (-level*qscale*quant_matrix[j])>>4;
  1349. level= -level;
  1350. }else{
  1351. level= (level*qscale*quant_matrix[j])>>4;
  1352. }
  1353. }
  1354. if (i > 63){
  1355. fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1356. return -1;
  1357. }
  1358. mismatch^= level;
  1359. block[j] = level;
  1360. }
  1361. CLOSE_READER(re, &s->gb);
  1362. }
  1363. block[63]^= mismatch&1;
  1364. s->block_last_index[n] = i;
  1365. return 0;
  1366. }
  1367. /* compressed picture size */
  1368. #define PICTURE_BUFFER_SIZE 100000
  1369. typedef struct Mpeg1Context {
  1370. MpegEncContext mpeg_enc_ctx;
  1371. uint32_t header_state;
  1372. int start_code; /* current start code */
  1373. uint8_t buffer[PICTURE_BUFFER_SIZE];
  1374. uint8_t *buf_ptr;
  1375. int buffer_size;
  1376. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  1377. int repeat_field; /* true if we must repeat the field */
  1378. } Mpeg1Context;
  1379. static int mpeg_decode_init(AVCodecContext *avctx)
  1380. {
  1381. Mpeg1Context *s = avctx->priv_data;
  1382. s->mpeg_enc_ctx.flags= avctx->flags;
  1383. common_init(&s->mpeg_enc_ctx);
  1384. init_vlcs(&s->mpeg_enc_ctx);
  1385. s->header_state = 0xff;
  1386. s->mpeg_enc_ctx_allocated = 0;
  1387. s->buffer_size = PICTURE_BUFFER_SIZE;
  1388. s->start_code = -1;
  1389. s->buf_ptr = s->buffer;
  1390. s->mpeg_enc_ctx.picture_number = 0;
  1391. s->repeat_field = 0;
  1392. s->mpeg_enc_ctx.codec_id= avctx->codec->id;
  1393. return 0;
  1394. }
  1395. /* return the 8 bit start code value and update the search
  1396. state. Return -1 if no start code found */
  1397. static int find_start_code(uint8_t **pbuf_ptr, uint8_t *buf_end,
  1398. uint32_t *header_state)
  1399. {
  1400. uint8_t *buf_ptr;
  1401. unsigned int state, v;
  1402. int val;
  1403. state = *header_state;
  1404. buf_ptr = *pbuf_ptr;
  1405. while (buf_ptr < buf_end) {
  1406. v = *buf_ptr++;
  1407. if (state == 0x000001) {
  1408. state = ((state << 8) | v) & 0xffffff;
  1409. val = state;
  1410. goto found;
  1411. }
  1412. state = ((state << 8) | v) & 0xffffff;
  1413. }
  1414. val = -1;
  1415. found:
  1416. *pbuf_ptr = buf_ptr;
  1417. *header_state = state;
  1418. return val;
  1419. }
  1420. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1421. uint8_t *buf, int buf_size)
  1422. {
  1423. Mpeg1Context *s1 = avctx->priv_data;
  1424. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1425. int ref, f_code;
  1426. init_get_bits(&s->gb, buf, buf_size*8);
  1427. ref = get_bits(&s->gb, 10); /* temporal ref */
  1428. s->pict_type = get_bits(&s->gb, 3);
  1429. dprintf("pict_type=%d number=%d\n", s->pict_type, s->picture_number);
  1430. skip_bits(&s->gb, 16);
  1431. if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
  1432. s->full_pel[0] = get_bits1(&s->gb);
  1433. f_code = get_bits(&s->gb, 3);
  1434. if (f_code == 0)
  1435. return -1;
  1436. s->mpeg_f_code[0][0] = f_code;
  1437. s->mpeg_f_code[0][1] = f_code;
  1438. }
  1439. if (s->pict_type == B_TYPE) {
  1440. s->full_pel[1] = get_bits1(&s->gb);
  1441. f_code = get_bits(&s->gb, 3);
  1442. if (f_code == 0)
  1443. return -1;
  1444. s->mpeg_f_code[1][0] = f_code;
  1445. s->mpeg_f_code[1][1] = f_code;
  1446. }
  1447. s->current_picture.pict_type= s->pict_type;
  1448. s->current_picture.key_frame= s->pict_type == I_TYPE;
  1449. s->y_dc_scale = 8;
  1450. s->c_dc_scale = 8;
  1451. s->first_slice = 1;
  1452. return 0;
  1453. }
  1454. static void mpeg_decode_sequence_extension(MpegEncContext *s)
  1455. {
  1456. int horiz_size_ext, vert_size_ext;
  1457. int bit_rate_ext, vbv_buf_ext;
  1458. int frame_rate_ext_n, frame_rate_ext_d;
  1459. float aspect;
  1460. skip_bits(&s->gb, 8); /* profil and level */
  1461. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1462. skip_bits(&s->gb, 2); /* chroma_format */
  1463. horiz_size_ext = get_bits(&s->gb, 2);
  1464. vert_size_ext = get_bits(&s->gb, 2);
  1465. s->width |= (horiz_size_ext << 12);
  1466. s->height |= (vert_size_ext << 12);
  1467. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1468. s->bit_rate = ((s->bit_rate / 400) | (bit_rate_ext << 12)) * 400;
  1469. skip_bits1(&s->gb); /* marker */
  1470. vbv_buf_ext = get_bits(&s->gb, 8);
  1471. s->low_delay = get_bits1(&s->gb);
  1472. frame_rate_ext_n = get_bits(&s->gb, 2);
  1473. frame_rate_ext_d = get_bits(&s->gb, 5);
  1474. if (frame_rate_ext_d >= 1)
  1475. s->frame_rate = (s->frame_rate * frame_rate_ext_n) / frame_rate_ext_d;
  1476. dprintf("sequence extension\n");
  1477. s->mpeg2 = 1;
  1478. s->avctx->sub_id = 2; /* indicates mpeg2 found */
  1479. aspect= mpeg2_aspect[s->aspect_ratio_info];
  1480. if(aspect>0.0) s->avctx->aspect_ratio= s->width/(aspect*s->height);
  1481. else if(aspect<0.0) s->avctx->aspect_ratio= -1.0/aspect;
  1482. }
  1483. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1484. {
  1485. int i, v, j;
  1486. dprintf("matrix extension\n");
  1487. if (get_bits1(&s->gb)) {
  1488. for(i=0;i<64;i++) {
  1489. v = get_bits(&s->gb, 8);
  1490. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1491. s->intra_matrix[j] = v;
  1492. s->chroma_intra_matrix[j] = v;
  1493. }
  1494. }
  1495. if (get_bits1(&s->gb)) {
  1496. for(i=0;i<64;i++) {
  1497. v = get_bits(&s->gb, 8);
  1498. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1499. s->inter_matrix[j] = v;
  1500. s->chroma_inter_matrix[j] = v;
  1501. }
  1502. }
  1503. if (get_bits1(&s->gb)) {
  1504. for(i=0;i<64;i++) {
  1505. v = get_bits(&s->gb, 8);
  1506. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1507. s->chroma_intra_matrix[j] = v;
  1508. }
  1509. }
  1510. if (get_bits1(&s->gb)) {
  1511. for(i=0;i<64;i++) {
  1512. v = get_bits(&s->gb, 8);
  1513. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1514. s->chroma_inter_matrix[j] = v;
  1515. }
  1516. }
  1517. }
  1518. static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
  1519. {
  1520. s->full_pel[0] = s->full_pel[1] = 0;
  1521. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1522. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1523. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1524. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1525. s->intra_dc_precision = get_bits(&s->gb, 2);
  1526. s->picture_structure = get_bits(&s->gb, 2);
  1527. s->top_field_first = get_bits1(&s->gb);
  1528. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1529. s->concealment_motion_vectors = get_bits1(&s->gb);
  1530. s->q_scale_type = get_bits1(&s->gb);
  1531. s->intra_vlc_format = get_bits1(&s->gb);
  1532. s->alternate_scan = get_bits1(&s->gb);
  1533. s->repeat_first_field = get_bits1(&s->gb);
  1534. s->chroma_420_type = get_bits1(&s->gb);
  1535. s->progressive_frame = get_bits1(&s->gb);
  1536. if(s->picture_structure == PICT_FRAME)
  1537. s->first_field=0;
  1538. else{
  1539. s->first_field ^= 1;
  1540. memset(s->mbskip_table, 0, s->mb_width*s->mb_height);
  1541. }
  1542. if(s->alternate_scan){
  1543. ff_init_scantable(s, &s->inter_scantable , ff_alternate_vertical_scan);
  1544. ff_init_scantable(s, &s->intra_scantable , ff_alternate_vertical_scan);
  1545. ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_vertical_scan);
  1546. ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
  1547. }else{
  1548. ff_init_scantable(s, &s->inter_scantable , ff_zigzag_direct);
  1549. ff_init_scantable(s, &s->intra_scantable , ff_zigzag_direct);
  1550. ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  1551. ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
  1552. }
  1553. /* composite display not parsed */
  1554. dprintf("intra_dc_precision=%d\n", s->intra_dc_precision);
  1555. dprintf("picture_structure=%d\n", s->picture_structure);
  1556. dprintf("top field first=%d\n", s->top_field_first);
  1557. dprintf("repeat first field=%d\n", s->repeat_first_field);
  1558. dprintf("conceal=%d\n", s->concealment_motion_vectors);
  1559. dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
  1560. dprintf("alternate_scan=%d\n", s->alternate_scan);
  1561. dprintf("frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1562. dprintf("progressive_frame=%d\n", s->progressive_frame);
  1563. }
  1564. static void mpeg_decode_extension(AVCodecContext *avctx,
  1565. uint8_t *buf, int buf_size)
  1566. {
  1567. Mpeg1Context *s1 = avctx->priv_data;
  1568. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1569. int ext_type;
  1570. init_get_bits(&s->gb, buf, buf_size*8);
  1571. ext_type = get_bits(&s->gb, 4);
  1572. switch(ext_type) {
  1573. case 0x1:
  1574. /* sequence ext */
  1575. mpeg_decode_sequence_extension(s);
  1576. break;
  1577. case 0x3:
  1578. /* quant matrix extension */
  1579. mpeg_decode_quant_matrix_extension(s);
  1580. break;
  1581. case 0x8:
  1582. /* picture extension */
  1583. mpeg_decode_picture_coding_extension(s);
  1584. break;
  1585. }
  1586. }
  1587. #define DECODE_SLICE_FATAL_ERROR -2
  1588. #define DECODE_SLICE_ERROR -1
  1589. #define DECODE_SLICE_OK 0
  1590. #define DECODE_SLICE_EOP 1
  1591. /**
  1592. * decodes a slice.
  1593. * @return DECODE_SLICE_FATAL_ERROR if a non recoverable error occured<br>
  1594. * DECODE_SLICE_ERROR if the slice is damaged<br>
  1595. * DECODE_SLICE_OK if this slice is ok<br>
  1596. * DECODE_SLICE_EOP if the end of the picture is reached
  1597. */
  1598. static int mpeg_decode_slice(AVCodecContext *avctx,
  1599. AVFrame *pict,
  1600. int start_code,
  1601. uint8_t *buf, int buf_size)
  1602. {
  1603. Mpeg1Context *s1 = avctx->priv_data;
  1604. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1605. int ret;
  1606. const int field_pic= s->picture_structure != PICT_FRAME;
  1607. start_code = (start_code - 1) & 0xff;
  1608. if (start_code >= s->mb_height){
  1609. fprintf(stderr, "slice below image (%d >= %d)\n", start_code, s->mb_height);
  1610. return DECODE_SLICE_ERROR;
  1611. }
  1612. s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
  1613. s->last_dc[1] = s->last_dc[0];
  1614. s->last_dc[2] = s->last_dc[0];
  1615. memset(s->last_mv, 0, sizeof(s->last_mv));
  1616. /* start frame decoding */
  1617. if (s->first_slice && (s->first_field || s->picture_structure==PICT_FRAME)) {
  1618. if(MPV_frame_start(s, avctx) < 0)
  1619. return DECODE_SLICE_FATAL_ERROR;
  1620. /* first check if we must repeat the frame */
  1621. s->current_picture.repeat_pict = 0;
  1622. if (s->repeat_first_field) {
  1623. if (s->progressive_sequence) {
  1624. if (s->top_field_first)
  1625. s->current_picture.repeat_pict = 4;
  1626. else
  1627. s->current_picture.repeat_pict = 2;
  1628. } else if (s->progressive_frame) {
  1629. s->current_picture.repeat_pict = 1;
  1630. }
  1631. }
  1632. // printf("%d \n", s->current_picture.repeat_pict);
  1633. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  1634. 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",
  1635. 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],
  1636. s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")),
  1637. s->progressive_sequence ? "pro" :"", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
  1638. s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
  1639. s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
  1640. }
  1641. }
  1642. s->first_slice = 0;
  1643. init_get_bits(&s->gb, buf, buf_size*8);
  1644. s->qscale = get_qscale(s);
  1645. /* extra slice info */
  1646. while (get_bits1(&s->gb) != 0) {
  1647. skip_bits(&s->gb, 8);
  1648. }
  1649. s->mb_x=0;
  1650. for(;;) {
  1651. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1652. if (code < 0)
  1653. return -1; /* error = end of slice, but empty slice is bad or?*/
  1654. if (code >= 33) {
  1655. if (code == 33) {
  1656. s->mb_x += 33;
  1657. }
  1658. /* otherwise, stuffing, nothing to do */
  1659. } else {
  1660. s->mb_x += code;
  1661. break;
  1662. }
  1663. }
  1664. s->mb_y = start_code;
  1665. s->mb_incr= 1;
  1666. for(;;) {
  1667. s->dsp.clear_blocks(s->block[0]);
  1668. ret = mpeg_decode_mb(s, s->block);
  1669. dprintf("ret=%d\n", ret);
  1670. if (ret < 0)
  1671. return -1;
  1672. //printf("%d %d\n", s->mb_x, s->mb_y);
  1673. //FIXME this isnt the most beautifull way to solve the problem ...
  1674. if(s->picture_structure!=PICT_FRAME){
  1675. if(s->picture_structure == PICT_BOTTOM_FIELD){
  1676. s->current_picture.data[0] += s->linesize;
  1677. s->current_picture.data[1] += s->uvlinesize;
  1678. s->current_picture.data[2] += s->uvlinesize;
  1679. }
  1680. s->linesize *= 2;
  1681. s->uvlinesize *= 2;
  1682. }
  1683. MPV_decode_mb(s, s->block);
  1684. if(s->picture_structure!=PICT_FRAME){
  1685. s->linesize /= 2;
  1686. s->uvlinesize /= 2;
  1687. if(s->picture_structure == PICT_BOTTOM_FIELD){
  1688. s->current_picture.data[0] -= s->linesize;
  1689. s->current_picture.data[1] -= s->uvlinesize;
  1690. s->current_picture.data[2] -= s->uvlinesize;
  1691. }
  1692. }
  1693. if (++s->mb_x >= s->mb_width) {
  1694. if(s->picture_structure==PICT_FRAME){
  1695. ff_draw_horiz_band(s, 16*s->mb_y, 16);
  1696. }else{
  1697. if(!s->first_field){
  1698. ff_draw_horiz_band(s, 32*s->mb_y, 32);
  1699. }
  1700. }
  1701. s->mb_x = 0;
  1702. s->mb_y++;
  1703. PRINT_QP("%s", "\n");
  1704. }
  1705. PRINT_QP("%2d", s->qscale);
  1706. /* skip mb handling */
  1707. if (s->mb_incr == 0) {
  1708. /* read again increment */
  1709. s->mb_incr = 1;
  1710. for(;;) {
  1711. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1712. if (code < 0)
  1713. goto eos; /* error = end of slice */
  1714. if (code >= 33) {
  1715. if (code == 33) {
  1716. s->mb_incr += 33;
  1717. }
  1718. /* otherwise, stuffing, nothing to do */
  1719. } else {
  1720. s->mb_incr += code;
  1721. break;
  1722. }
  1723. }
  1724. }
  1725. if(s->mb_y<<field_pic >= s->mb_height){
  1726. fprintf(stderr, "slice too long\n");
  1727. return DECODE_SLICE_ERROR;
  1728. }
  1729. }
  1730. eos: //end of slice
  1731. emms_c();
  1732. //intf("%d %d %d %d\n", s->mb_y, s->mb_height, s->pict_type, s->picture_number);
  1733. /* end of slice reached */
  1734. if (s->mb_y<<field_pic == s->mb_height && !s->first_field) {
  1735. /* end of image */
  1736. if(s->mpeg2)
  1737. s->qscale >>=1;
  1738. MPV_frame_end(s);
  1739. if (s->pict_type == B_TYPE || s->low_delay) {
  1740. *pict= *(AVFrame*)&s->current_picture;
  1741. } else {
  1742. s->picture_number++;
  1743. /* latency of 1 frame for I and P frames */
  1744. /* XXX: use another variable than picture_number */
  1745. if (s->last_picture.data[0] == NULL) {
  1746. return DECODE_SLICE_OK;
  1747. } else {
  1748. *pict= *(AVFrame*)&s->last_picture;
  1749. }
  1750. }
  1751. return DECODE_SLICE_EOP;
  1752. } else {
  1753. return DECODE_SLICE_OK;
  1754. }
  1755. }
  1756. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1757. uint8_t *buf, int buf_size)
  1758. {
  1759. Mpeg1Context *s1 = avctx->priv_data;
  1760. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1761. int width, height, i, v, j;
  1762. float aspect;
  1763. init_get_bits(&s->gb, buf, buf_size*8);
  1764. width = get_bits(&s->gb, 12);
  1765. height = get_bits(&s->gb, 12);
  1766. s->aspect_ratio_info= get_bits(&s->gb, 4);
  1767. if(!s->mpeg2){
  1768. aspect= mpeg1_aspect[s->aspect_ratio_info];
  1769. if(aspect!=0.0) avctx->aspect_ratio= width/(aspect*height);
  1770. }
  1771. s->frame_rate_index = get_bits(&s->gb, 4);
  1772. if (s->frame_rate_index == 0)
  1773. return -1;
  1774. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1775. if (get_bits1(&s->gb) == 0) /* marker */
  1776. return -1;
  1777. if (width <= 0 || height <= 0 ||
  1778. (width % 2) != 0 || (height % 2) != 0)
  1779. return -1;
  1780. if (width != s->width ||
  1781. height != s->height) {
  1782. /* start new mpeg1 context decoding */
  1783. s->out_format = FMT_MPEG1;
  1784. if (s1->mpeg_enc_ctx_allocated) {
  1785. MPV_common_end(s);
  1786. }
  1787. s->width = width;
  1788. s->height = height;
  1789. avctx->has_b_frames= 1;
  1790. s->avctx = avctx;
  1791. avctx->width = width;
  1792. avctx->height = height;
  1793. if (s->frame_rate_index >= 9) {
  1794. /* at least give a valid frame rate (some old mpeg1 have this) */
  1795. avctx->frame_rate = 25 * FRAME_RATE_BASE;
  1796. } else {
  1797. avctx->frame_rate = frame_rate_tab[s->frame_rate_index];
  1798. }
  1799. s->frame_rate = avctx->frame_rate;
  1800. avctx->bit_rate = s->bit_rate;
  1801. if (MPV_common_init(s) < 0)
  1802. return -1;
  1803. s1->mpeg_enc_ctx_allocated = 1;
  1804. }
  1805. skip_bits(&s->gb, 10); /* vbv_buffer_size */
  1806. skip_bits(&s->gb, 1);
  1807. /* get matrix */
  1808. if (get_bits1(&s->gb)) {
  1809. for(i=0;i<64;i++) {
  1810. v = get_bits(&s->gb, 8);
  1811. j = s->intra_scantable.permutated[i];
  1812. s->intra_matrix[j] = v;
  1813. s->chroma_intra_matrix[j] = v;
  1814. }
  1815. #ifdef DEBUG
  1816. dprintf("intra matrix present\n");
  1817. for(i=0;i<64;i++)
  1818. dprintf(" %d", s->intra_matrix[s->intra_scantable.permutated[i]]);
  1819. printf("\n");
  1820. #endif
  1821. } else {
  1822. for(i=0;i<64;i++) {
  1823. int j= s->dsp.idct_permutation[i];
  1824. v = ff_mpeg1_default_intra_matrix[i];
  1825. s->intra_matrix[j] = v;
  1826. s->chroma_intra_matrix[j] = v;
  1827. }
  1828. }
  1829. if (get_bits1(&s->gb)) {
  1830. for(i=0;i<64;i++) {
  1831. v = get_bits(&s->gb, 8);
  1832. j = s->intra_scantable.permutated[i];
  1833. s->inter_matrix[j] = v;
  1834. s->chroma_inter_matrix[j] = v;
  1835. }
  1836. #ifdef DEBUG
  1837. dprintf("non intra matrix present\n");
  1838. for(i=0;i<64;i++)
  1839. dprintf(" %d", s->inter_matrix[s->intra_scantable.permutated[i]]);
  1840. printf("\n");
  1841. #endif
  1842. } else {
  1843. for(i=0;i<64;i++) {
  1844. int j= s->dsp.idct_permutation[i];
  1845. v = ff_mpeg1_default_non_intra_matrix[i];
  1846. s->inter_matrix[j] = v;
  1847. s->chroma_inter_matrix[j] = v;
  1848. }
  1849. }
  1850. /* we set mpeg2 parameters so that it emulates mpeg1 */
  1851. s->progressive_sequence = 1;
  1852. s->progressive_frame = 1;
  1853. s->picture_structure = PICT_FRAME;
  1854. s->frame_pred_frame_dct = 1;
  1855. s->mpeg2 = 0;
  1856. avctx->sub_id = 1; /* indicates mpeg1 */
  1857. return 0;
  1858. }
  1859. static void mpeg_decode_user_data(AVCodecContext *avctx,
  1860. const uint8_t *buf, int buf_size)
  1861. {
  1862. const uint8_t *p;
  1863. int len, flags;
  1864. p = buf;
  1865. len = buf_size;
  1866. /* we parse the DTG active format information */
  1867. if (len >= 5 &&
  1868. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  1869. flags = p[4];
  1870. p += 5;
  1871. len -= 5;
  1872. if (flags & 0x80) {
  1873. /* skip event id */
  1874. if (len < 2)
  1875. return;
  1876. p += 2;
  1877. len -= 2;
  1878. }
  1879. if (flags & 0x40) {
  1880. if (len < 1)
  1881. return;
  1882. avctx->dtg_active_format = p[0] & 0x0f;
  1883. }
  1884. }
  1885. }
  1886. /* handle buffering and image synchronisation */
  1887. static int mpeg_decode_frame(AVCodecContext *avctx,
  1888. void *data, int *data_size,
  1889. uint8_t *buf, int buf_size)
  1890. {
  1891. Mpeg1Context *s = avctx->priv_data;
  1892. uint8_t *buf_end, *buf_ptr, *buf_start;
  1893. int len, start_code_found, ret, code, start_code, input_size;
  1894. AVFrame *picture = data;
  1895. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1896. dprintf("fill_buffer\n");
  1897. *data_size = 0;
  1898. /* special case for last picture */
  1899. if (buf_size == 0) {
  1900. if (s2->picture_number > 0) {
  1901. *picture= *(AVFrame*)&s2->next_picture;
  1902. *data_size = sizeof(AVFrame);
  1903. }
  1904. return 0;
  1905. }
  1906. buf_ptr = buf;
  1907. buf_end = buf + buf_size;
  1908. #if 0
  1909. if (s->repeat_field % 2 == 1) {
  1910. s->repeat_field++;
  1911. //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
  1912. // s2->picture_number, s->repeat_field);
  1913. if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
  1914. *data_size = sizeof(AVPicture);
  1915. goto the_end;
  1916. }
  1917. }
  1918. #endif
  1919. while (buf_ptr < buf_end) {
  1920. buf_start = buf_ptr;
  1921. /* find start next code */
  1922. code = find_start_code(&buf_ptr, buf_end, &s->header_state);
  1923. if (code >= 0) {
  1924. start_code_found = 1;
  1925. } else {
  1926. start_code_found = 0;
  1927. }
  1928. /* copy to buffer */
  1929. len = buf_ptr - buf_start;
  1930. if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
  1931. /* data too big : flush */
  1932. s->buf_ptr = s->buffer;
  1933. if (start_code_found)
  1934. s->start_code = code;
  1935. } else {
  1936. memcpy(s->buf_ptr, buf_start, len);
  1937. s->buf_ptr += len;
  1938. if( (!(s2->flags&CODEC_FLAG_TRUNCATED)) && (!start_code_found)
  1939. && s->buf_ptr+4<s->buffer+s->buffer_size){
  1940. start_code_found= 1;
  1941. code= 0x1FF;
  1942. s->header_state=0xFF;
  1943. s->buf_ptr[0]=0;
  1944. s->buf_ptr[1]=0;
  1945. s->buf_ptr[2]=1;
  1946. s->buf_ptr[3]=0xFF;
  1947. s->buf_ptr+=4;
  1948. }
  1949. if (start_code_found) {
  1950. /* prepare data for next start code */
  1951. input_size = s->buf_ptr - s->buffer;
  1952. start_code = s->start_code;
  1953. s->buf_ptr = s->buffer;
  1954. s->start_code = code;
  1955. switch(start_code) {
  1956. case SEQ_START_CODE:
  1957. mpeg1_decode_sequence(avctx, s->buffer,
  1958. input_size);
  1959. break;
  1960. case PICTURE_START_CODE:
  1961. /* we have a complete image : we try to decompress it */
  1962. mpeg1_decode_picture(avctx,
  1963. s->buffer, input_size);
  1964. break;
  1965. case EXT_START_CODE:
  1966. mpeg_decode_extension(avctx,
  1967. s->buffer, input_size);
  1968. break;
  1969. case USER_START_CODE:
  1970. mpeg_decode_user_data(avctx,
  1971. s->buffer, input_size);
  1972. break;
  1973. default:
  1974. if (start_code >= SLICE_MIN_START_CODE &&
  1975. start_code <= SLICE_MAX_START_CODE) {
  1976. /* skip b frames if we dont have reference frames */
  1977. if(s2->last_picture.data[0]==NULL && s2->pict_type==B_TYPE) break;
  1978. /* skip b frames if we are in a hurry */
  1979. if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
  1980. /* skip everything if we are in a hurry>=5 */
  1981. if(avctx->hurry_up>=5) break;
  1982. ret = mpeg_decode_slice(avctx, picture,
  1983. start_code, s->buffer, input_size);
  1984. if (ret == DECODE_SLICE_EOP) {
  1985. *data_size = sizeof(AVPicture);
  1986. goto the_end;
  1987. }else if(ret<0){
  1988. fprintf(stderr,"Error while decoding slice\n");
  1989. if(ret==DECODE_SLICE_FATAL_ERROR) return -1;
  1990. }
  1991. }
  1992. break;
  1993. }
  1994. }
  1995. }
  1996. }
  1997. the_end:
  1998. return buf_ptr - buf;
  1999. }
  2000. static int mpeg_decode_end(AVCodecContext *avctx)
  2001. {
  2002. Mpeg1Context *s = avctx->priv_data;
  2003. if (s->mpeg_enc_ctx_allocated)
  2004. MPV_common_end(&s->mpeg_enc_ctx);
  2005. return 0;
  2006. }
  2007. AVCodec mpeg_decoder = {
  2008. "mpegvideo",
  2009. CODEC_TYPE_VIDEO,
  2010. CODEC_ID_MPEG1VIDEO,
  2011. sizeof(Mpeg1Context),
  2012. mpeg_decode_init,
  2013. NULL,
  2014. mpeg_decode_end,
  2015. mpeg_decode_frame,
  2016. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  2017. };