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.

2239 lines
74KB

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