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.

2315 lines
78KB

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