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.

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