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.

2342 lines
79KB

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