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.

4077 lines
139KB

  1. /*
  2. * The simplest mpeg encoder (well, it was the simplest!)
  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. * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  20. */
  21. #include <ctype.h>
  22. #include <limits.h>
  23. #include "avcodec.h"
  24. #include "dsputil.h"
  25. #include "mpegvideo.h"
  26. #include "simple_idct.h"
  27. #ifdef USE_FASTMEMCPY
  28. #include "fastmemcpy.h"
  29. #endif
  30. //#undef NDEBUG
  31. //#include <assert.h>
  32. static void encode_picture(MpegEncContext *s, int picture_number);
  33. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  34. DCTELEM *block, int n, int qscale);
  35. static void dct_unquantize_mpeg2_c(MpegEncContext *s,
  36. DCTELEM *block, int n, int qscale);
  37. static void dct_unquantize_h263_c(MpegEncContext *s,
  38. DCTELEM *block, int n, int qscale);
  39. static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w);
  40. static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
  41. static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
  42. void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c;
  43. /* enable all paranoid tests for rounding, overflows, etc... */
  44. //#define PARANOID
  45. //#define DEBUG
  46. /* for jpeg fast DCT */
  47. #define CONST_BITS 14
  48. static const uint16_t aanscales[64] = {
  49. /* precomputed values scaled up by 14 bits */
  50. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  51. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  52. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  53. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  54. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  55. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  56. 8867 , 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  57. 4520 , 6270, 5906, 5315, 4520, 3552, 2446, 1247
  58. };
  59. /* Input permutation for the simple_idct_mmx */
  60. static const uint8_t simple_mmx_permutation[64]={
  61. 0x00, 0x08, 0x04, 0x09, 0x01, 0x0C, 0x05, 0x0D,
  62. 0x10, 0x18, 0x14, 0x19, 0x11, 0x1C, 0x15, 0x1D,
  63. 0x20, 0x28, 0x24, 0x29, 0x21, 0x2C, 0x25, 0x2D,
  64. 0x12, 0x1A, 0x16, 0x1B, 0x13, 0x1E, 0x17, 0x1F,
  65. 0x02, 0x0A, 0x06, 0x0B, 0x03, 0x0E, 0x07, 0x0F,
  66. 0x30, 0x38, 0x34, 0x39, 0x31, 0x3C, 0x35, 0x3D,
  67. 0x22, 0x2A, 0x26, 0x2B, 0x23, 0x2E, 0x27, 0x2F,
  68. 0x32, 0x3A, 0x36, 0x3B, 0x33, 0x3E, 0x37, 0x3F,
  69. };
  70. static const uint8_t h263_chroma_roundtab[16] = {
  71. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  72. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
  73. };
  74. static UINT16 (*default_mv_penalty)[MAX_MV*2+1]=NULL;
  75. static UINT8 default_fcode_tab[MAX_MV*2+1];
  76. enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
  77. static void convert_matrix(MpegEncContext *s, int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
  78. const UINT16 *quant_matrix, int bias, int qmin, int qmax)
  79. {
  80. int qscale;
  81. for(qscale=qmin; qscale<=qmax; qscale++){
  82. int i;
  83. if (s->fdct == ff_jpeg_fdct_islow) {
  84. for(i=0;i<64;i++) {
  85. const int j= s->idct_permutation[i];
  86. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  87. /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  88. /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
  89. /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
  90. qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
  91. (qscale * quant_matrix[j]));
  92. }
  93. } else if (s->fdct == fdct_ifast) {
  94. for(i=0;i<64;i++) {
  95. const int j= s->idct_permutation[i];
  96. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  97. /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  98. /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
  99. /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
  100. qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) /
  101. (aanscales[i] * qscale * quant_matrix[j]));
  102. }
  103. } else {
  104. for(i=0;i<64;i++) {
  105. const int j= s->idct_permutation[i];
  106. /* We can safely suppose that 16 <= quant_matrix[i] <= 255
  107. So 16 <= qscale * quant_matrix[i] <= 7905
  108. so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
  109. so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
  110. */
  111. qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
  112. // qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
  113. qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
  114. if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1;
  115. qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]);
  116. }
  117. }
  118. }
  119. }
  120. // move into common.c perhaps
  121. #define CHECKED_ALLOCZ(p, size)\
  122. {\
  123. p= av_mallocz(size);\
  124. if(p==NULL){\
  125. perror("malloc");\
  126. goto fail;\
  127. }\
  128. }
  129. void ff_init_scantable(MpegEncContext *s, ScanTable *st, const UINT8 *src_scantable){
  130. int i;
  131. int end;
  132. st->scantable= src_scantable;
  133. for(i=0; i<64; i++){
  134. int j;
  135. j = src_scantable[i];
  136. st->permutated[i] = s->idct_permutation[j];
  137. #ifdef ARCH_POWERPC
  138. st->inverse[j] = i;
  139. #endif
  140. }
  141. end=-1;
  142. for(i=0; i<64; i++){
  143. int j;
  144. j = st->permutated[i];
  145. if(j>end) end=j;
  146. st->raster_end[i]= end;
  147. }
  148. }
  149. /* XXX: those functions should be suppressed ASAP when all IDCTs are
  150. converted */
  151. // *FIXME* this is ugly hack using local static
  152. static void (*ff_put_pixels_clamped)(const DCTELEM *block, UINT8 *pixels, int line_size);
  153. static void (*ff_add_pixels_clamped)(const DCTELEM *block, UINT8 *pixels, int line_size);
  154. static void ff_jref_idct_put(UINT8 *dest, int line_size, DCTELEM *block)
  155. {
  156. j_rev_dct (block);
  157. ff_put_pixels_clamped(block, dest, line_size);
  158. }
  159. static void ff_jref_idct_add(UINT8 *dest, int line_size, DCTELEM *block)
  160. {
  161. j_rev_dct (block);
  162. ff_add_pixels_clamped(block, dest, line_size);
  163. }
  164. /* init common dct for both encoder and decoder */
  165. int DCT_common_init(MpegEncContext *s)
  166. {
  167. int i;
  168. ff_put_pixels_clamped = s->dsp.put_pixels_clamped;
  169. ff_add_pixels_clamped = s->dsp.add_pixels_clamped;
  170. s->dct_unquantize_h263 = dct_unquantize_h263_c;
  171. s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
  172. s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
  173. s->dct_quantize= dct_quantize_c;
  174. if(s->avctx->dct_algo==FF_DCT_FASTINT)
  175. s->fdct = fdct_ifast;
  176. else
  177. s->fdct = ff_jpeg_fdct_islow; //slow/accurate/default
  178. if(s->avctx->idct_algo==FF_IDCT_INT){
  179. s->idct_put= ff_jref_idct_put;
  180. s->idct_add= ff_jref_idct_add;
  181. s->idct_permutation_type= FF_LIBMPEG2_IDCT_PERM;
  182. }else{ //accurate/default
  183. s->idct_put= simple_idct_put;
  184. s->idct_add= simple_idct_add;
  185. s->idct_permutation_type= FF_NO_IDCT_PERM;
  186. }
  187. #ifdef HAVE_MMX
  188. MPV_common_init_mmx(s);
  189. #endif
  190. #ifdef ARCH_ALPHA
  191. MPV_common_init_axp(s);
  192. #endif
  193. #ifdef HAVE_MLIB
  194. MPV_common_init_mlib(s);
  195. #endif
  196. #ifdef HAVE_MMI
  197. MPV_common_init_mmi(s);
  198. #endif
  199. #ifdef ARCH_ARMV4L
  200. MPV_common_init_armv4l(s);
  201. #endif
  202. #ifdef ARCH_POWERPC
  203. MPV_common_init_ppc(s);
  204. #endif
  205. s->fast_dct_quantize= s->dct_quantize;
  206. if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
  207. s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
  208. }
  209. switch(s->idct_permutation_type){
  210. case FF_NO_IDCT_PERM:
  211. for(i=0; i<64; i++)
  212. s->idct_permutation[i]= i;
  213. break;
  214. case FF_LIBMPEG2_IDCT_PERM:
  215. for(i=0; i<64; i++)
  216. s->idct_permutation[i]= (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
  217. break;
  218. case FF_SIMPLE_IDCT_PERM:
  219. for(i=0; i<64; i++)
  220. s->idct_permutation[i]= simple_mmx_permutation[i];
  221. break;
  222. case FF_TRANSPOSE_IDCT_PERM:
  223. for(i=0; i<64; i++)
  224. s->idct_permutation[i]= ((i&7)<<3) | (i>>3);
  225. break;
  226. default:
  227. fprintf(stderr, "Internal error, IDCT permutation not set\n");
  228. return -1;
  229. }
  230. /* load & permutate scantables
  231. note: only wmv uses differnt ones
  232. */
  233. ff_init_scantable(s, &s->inter_scantable , ff_zigzag_direct);
  234. ff_init_scantable(s, &s->intra_scantable , ff_zigzag_direct);
  235. ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  236. ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
  237. return 0;
  238. }
  239. /**
  240. * allocates a Picture
  241. * The pixels are allocated/set by calling get_buffer() if shared=0
  242. */
  243. static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
  244. if(shared){
  245. assert(pic->data[0]);
  246. assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
  247. pic->type= FF_BUFFER_TYPE_SHARED;
  248. }else{
  249. int r;
  250. assert(!pic->data[0]);
  251. r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
  252. if(r<0 || !pic->age || !pic->type || !pic->data[0]){
  253. fprintf(stderr, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
  254. return -1;
  255. }
  256. if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
  257. fprintf(stderr, "get_buffer() failed (stride changed)\n");
  258. return -1;
  259. }
  260. if(pic->linesize[1] != pic->linesize[2]){
  261. fprintf(stderr, "get_buffer() failed (uv stride missmatch)\n");
  262. return -1;
  263. }
  264. s->linesize = pic->linesize[0];
  265. s->uvlinesize= pic->linesize[1];
  266. }
  267. if(pic->qscale_table==NULL){
  268. if (s->encoding) {
  269. CHECKED_ALLOCZ(pic->mb_var , s->mb_num * sizeof(INT16))
  270. CHECKED_ALLOCZ(pic->mc_mb_var, s->mb_num * sizeof(INT16))
  271. CHECKED_ALLOCZ(pic->mb_mean , s->mb_num * sizeof(INT8))
  272. CHECKED_ALLOCZ(pic->mb_cmp_score, s->mb_num * sizeof(int32_t))
  273. }
  274. CHECKED_ALLOCZ(pic->mbskip_table , s->mb_num * sizeof(UINT8)+1) //the +1 is for the slice end check
  275. CHECKED_ALLOCZ(pic->qscale_table , s->mb_num * sizeof(UINT8))
  276. pic->qstride= s->mb_width;
  277. }
  278. //it might be nicer if the application would keep track of these but it would require a API change
  279. memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
  280. s->prev_pict_types[0]= s->pict_type;
  281. if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
  282. pic->age= INT_MAX; // skiped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
  283. return 0;
  284. fail: //for the CHECKED_ALLOCZ macro
  285. return -1;
  286. }
  287. /**
  288. * deallocates a picture
  289. */
  290. static void free_picture(MpegEncContext *s, Picture *pic){
  291. int i;
  292. if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
  293. s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
  294. }
  295. av_freep(&pic->mb_var);
  296. av_freep(&pic->mc_mb_var);
  297. av_freep(&pic->mb_mean);
  298. av_freep(&pic->mb_cmp_score);
  299. av_freep(&pic->mbskip_table);
  300. av_freep(&pic->qscale_table);
  301. if(pic->type == FF_BUFFER_TYPE_INTERNAL){
  302. for(i=0; i<4; i++){
  303. av_freep(&pic->base[i]);
  304. pic->data[i]= NULL;
  305. }
  306. av_freep(&pic->opaque);
  307. pic->type= 0;
  308. }else if(pic->type == FF_BUFFER_TYPE_SHARED){
  309. for(i=0; i<4; i++){
  310. pic->base[i]=
  311. pic->data[i]= NULL;
  312. }
  313. pic->type= 0;
  314. }
  315. }
  316. /* init common structure for both encoder and decoder */
  317. int MPV_common_init(MpegEncContext *s)
  318. {
  319. int y_size, c_size, yc_size, i;
  320. dsputil_init(&s->dsp, s->avctx->dsp_mask);
  321. DCT_common_init(s);
  322. s->flags= s->avctx->flags;
  323. s->mb_width = (s->width + 15) / 16;
  324. s->mb_height = (s->height + 15) / 16;
  325. /* set default edge pos, will be overriden in decode_header if needed */
  326. s->h_edge_pos= s->mb_width*16;
  327. s->v_edge_pos= s->mb_height*16;
  328. s->mb_num = s->mb_width * s->mb_height;
  329. y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  330. c_size = (s->mb_width + 2) * (s->mb_height + 2);
  331. yc_size = y_size + 2 * c_size;
  332. /* convert fourcc to upper case */
  333. s->avctx->fourcc= toupper( s->avctx->fourcc &0xFF)
  334. + (toupper((s->avctx->fourcc>>8 )&0xFF)<<8 )
  335. + (toupper((s->avctx->fourcc>>16)&0xFF)<<16)
  336. + (toupper((s->avctx->fourcc>>24)&0xFF)<<24);
  337. CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
  338. s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
  339. s->avctx->coded_frame= (AVFrame*)&s->current_picture;
  340. if (s->encoding) {
  341. int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
  342. /* Allocate MV tables */
  343. CHECKED_ALLOCZ(s->p_mv_table , mv_table_size * 2 * sizeof(INT16))
  344. CHECKED_ALLOCZ(s->b_forw_mv_table , mv_table_size * 2 * sizeof(INT16))
  345. CHECKED_ALLOCZ(s->b_back_mv_table , mv_table_size * 2 * sizeof(INT16))
  346. CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(INT16))
  347. CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(INT16))
  348. CHECKED_ALLOCZ(s->b_direct_mv_table , mv_table_size * 2 * sizeof(INT16))
  349. //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
  350. CHECKED_ALLOCZ(s->me.scratchpad, s->width*2*16*3*sizeof(uint8_t))
  351. CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t))
  352. CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
  353. if(s->codec_id==CODEC_ID_MPEG4){
  354. CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
  355. CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE);
  356. }
  357. if(s->msmpeg4_version){
  358. CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
  359. }
  360. CHECKED_ALLOCZ(s->avctx->stats_out, 256);
  361. }
  362. CHECKED_ALLOCZ(s->error_status_table, s->mb_num*sizeof(UINT8))
  363. if (s->out_format == FMT_H263 || s->encoding) {
  364. int size;
  365. /* Allocate MB type table */
  366. CHECKED_ALLOCZ(s->mb_type , s->mb_num * sizeof(UINT8))
  367. /* MV prediction */
  368. size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  369. CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(INT16));
  370. }
  371. if(s->codec_id==CODEC_ID_MPEG4){
  372. /* interlaced direct mode decoding tables */
  373. CHECKED_ALLOCZ(s->field_mv_table, s->mb_num*2*2 * sizeof(INT16))
  374. CHECKED_ALLOCZ(s->field_select_table, s->mb_num*2* sizeof(INT8))
  375. }
  376. /* 4mv b frame decoding table */
  377. //note this is needed for h263 without b frames too (segfault on damaged streams otherwise)
  378. CHECKED_ALLOCZ(s->co_located_type_table, s->mb_num * sizeof(UINT8))
  379. if (s->out_format == FMT_H263) {
  380. /* ac values */
  381. CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(INT16) * 16);
  382. s->ac_val[1] = s->ac_val[0] + y_size;
  383. s->ac_val[2] = s->ac_val[1] + c_size;
  384. /* cbp values */
  385. CHECKED_ALLOCZ(s->coded_block, y_size);
  386. /* divx501 bitstream reorder buffer */
  387. CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
  388. /* cbp, ac_pred, pred_dir */
  389. CHECKED_ALLOCZ(s->cbp_table , s->mb_num * sizeof(UINT8))
  390. CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(UINT8))
  391. }
  392. if (s->h263_pred || s->h263_plus || !s->encoding) {
  393. /* dc values */
  394. //MN: we need these for error resilience of intra-frames
  395. CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(INT16));
  396. s->dc_val[1] = s->dc_val[0] + y_size;
  397. s->dc_val[2] = s->dc_val[1] + c_size;
  398. for(i=0;i<yc_size;i++)
  399. s->dc_val[0][i] = 1024;
  400. }
  401. /* which mb is a intra block */
  402. CHECKED_ALLOCZ(s->mbintra_table, s->mb_num);
  403. memset(s->mbintra_table, 1, s->mb_num);
  404. /* default structure is frame */
  405. s->picture_structure = PICT_FRAME;
  406. /* init macroblock skip table */
  407. CHECKED_ALLOCZ(s->mbskip_table, s->mb_num+1);
  408. //Note the +1 is for a quicker mpeg4 slice_end detection
  409. CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
  410. s->block= s->blocks[0];
  411. s->parse_context.state= -1;
  412. s->context_initialized = 1;
  413. return 0;
  414. fail:
  415. MPV_common_end(s);
  416. return -1;
  417. }
  418. //extern int sads;
  419. /* init common structure for both encoder and decoder */
  420. void MPV_common_end(MpegEncContext *s)
  421. {
  422. int i;
  423. av_freep(&s->mb_type);
  424. av_freep(&s->p_mv_table);
  425. av_freep(&s->b_forw_mv_table);
  426. av_freep(&s->b_back_mv_table);
  427. av_freep(&s->b_bidir_forw_mv_table);
  428. av_freep(&s->b_bidir_back_mv_table);
  429. av_freep(&s->b_direct_mv_table);
  430. av_freep(&s->motion_val);
  431. av_freep(&s->dc_val[0]);
  432. av_freep(&s->ac_val[0]);
  433. av_freep(&s->coded_block);
  434. av_freep(&s->mbintra_table);
  435. av_freep(&s->cbp_table);
  436. av_freep(&s->pred_dir_table);
  437. av_freep(&s->me.scratchpad);
  438. av_freep(&s->me.map);
  439. av_freep(&s->me.score_map);
  440. av_freep(&s->mbskip_table);
  441. av_freep(&s->prev_pict_types);
  442. av_freep(&s->bitstream_buffer);
  443. av_freep(&s->tex_pb_buffer);
  444. av_freep(&s->pb2_buffer);
  445. av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
  446. av_freep(&s->co_located_type_table);
  447. av_freep(&s->field_mv_table);
  448. av_freep(&s->field_select_table);
  449. av_freep(&s->avctx->stats_out);
  450. av_freep(&s->ac_stats);
  451. av_freep(&s->error_status_table);
  452. for(i=0; i<MAX_PICTURE_COUNT; i++){
  453. free_picture(s, &s->picture[i]);
  454. }
  455. s->context_initialized = 0;
  456. }
  457. /* init video encoder */
  458. int MPV_encode_init(AVCodecContext *avctx)
  459. {
  460. MpegEncContext *s = avctx->priv_data;
  461. int i;
  462. avctx->pix_fmt = PIX_FMT_YUV420P;
  463. s->bit_rate = avctx->bit_rate;
  464. s->bit_rate_tolerance = avctx->bit_rate_tolerance;
  465. s->frame_rate = avctx->frame_rate;
  466. s->width = avctx->width;
  467. s->height = avctx->height;
  468. if(avctx->gop_size > 600){
  469. fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n");
  470. avctx->gop_size=600;
  471. }
  472. s->gop_size = avctx->gop_size;
  473. s->rtp_mode = avctx->rtp_mode;
  474. s->rtp_payload_size = avctx->rtp_payload_size;
  475. if (avctx->rtp_callback)
  476. s->rtp_callback = avctx->rtp_callback;
  477. s->qmin= avctx->qmin;
  478. s->qmax= avctx->qmax;
  479. s->max_qdiff= avctx->max_qdiff;
  480. s->qcompress= avctx->qcompress;
  481. s->qblur= avctx->qblur;
  482. s->avctx = avctx;
  483. s->flags= avctx->flags;
  484. s->max_b_frames= avctx->max_b_frames;
  485. s->b_frame_strategy= avctx->b_frame_strategy;
  486. s->codec_id= avctx->codec->id;
  487. s->luma_elim_threshold = avctx->luma_elim_threshold;
  488. s->chroma_elim_threshold= avctx->chroma_elim_threshold;
  489. s->strict_std_compliance= avctx->strict_std_compliance;
  490. s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
  491. s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
  492. s->mpeg_quant= avctx->mpeg_quant;
  493. if (s->gop_size <= 1) {
  494. s->intra_only = 1;
  495. s->gop_size = 12;
  496. } else {
  497. s->intra_only = 0;
  498. }
  499. s->me_method = avctx->me_method;
  500. /* Fixed QSCALE */
  501. s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
  502. s->adaptive_quant= ( s->avctx->lumi_masking
  503. || s->avctx->dark_masking
  504. || s->avctx->temporal_cplx_masking
  505. || s->avctx->spatial_cplx_masking
  506. || s->avctx->p_masking)
  507. && !s->fixed_qscale;
  508. s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
  509. switch(avctx->codec->id) {
  510. case CODEC_ID_MPEG1VIDEO:
  511. s->out_format = FMT_MPEG1;
  512. avctx->delay=0; //FIXME not sure, should check the spec
  513. break;
  514. case CODEC_ID_MJPEG:
  515. s->out_format = FMT_MJPEG;
  516. s->intra_only = 1; /* force intra only for jpeg */
  517. s->mjpeg_write_tables = 1; /* write all tables */
  518. s->mjpeg_data_only_frames = 0; /* write all the needed headers */
  519. s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
  520. s->mjpeg_vsample[1] = 1; /* the only currently supported values */
  521. s->mjpeg_vsample[2] = 1;
  522. s->mjpeg_hsample[0] = 2;
  523. s->mjpeg_hsample[1] = 1;
  524. s->mjpeg_hsample[2] = 1;
  525. if (mjpeg_init(s) < 0)
  526. return -1;
  527. avctx->delay=0;
  528. s->low_delay=1;
  529. break;
  530. #ifdef CONFIG_RISKY
  531. case CODEC_ID_H263:
  532. if (h263_get_picture_format(s->width, s->height) == 7) {
  533. printf("Input picture size isn't suitable for h263 codec! try h263+\n");
  534. return -1;
  535. }
  536. s->out_format = FMT_H263;
  537. avctx->delay=0;
  538. s->low_delay=1;
  539. break;
  540. case CODEC_ID_H263P:
  541. s->out_format = FMT_H263;
  542. s->h263_plus = 1;
  543. s->unrestricted_mv = 1;
  544. s->h263_aic = 1;
  545. /* These are just to be sure */
  546. s->umvplus = 0;
  547. s->umvplus_dec = 0;
  548. avctx->delay=0;
  549. s->low_delay=1;
  550. break;
  551. case CODEC_ID_RV10:
  552. s->out_format = FMT_H263;
  553. s->h263_rv10 = 1;
  554. avctx->delay=0;
  555. s->low_delay=1;
  556. break;
  557. case CODEC_ID_MPEG4:
  558. s->out_format = FMT_H263;
  559. s->h263_pred = 1;
  560. s->unrestricted_mv = 1;
  561. s->low_delay= s->max_b_frames ? 0 : 1;
  562. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  563. break;
  564. case CODEC_ID_MSMPEG4V1:
  565. s->out_format = FMT_H263;
  566. s->h263_msmpeg4 = 1;
  567. s->h263_pred = 1;
  568. s->unrestricted_mv = 1;
  569. s->msmpeg4_version= 1;
  570. avctx->delay=0;
  571. s->low_delay=1;
  572. break;
  573. case CODEC_ID_MSMPEG4V2:
  574. s->out_format = FMT_H263;
  575. s->h263_msmpeg4 = 1;
  576. s->h263_pred = 1;
  577. s->unrestricted_mv = 1;
  578. s->msmpeg4_version= 2;
  579. avctx->delay=0;
  580. s->low_delay=1;
  581. break;
  582. case CODEC_ID_MSMPEG4V3:
  583. s->out_format = FMT_H263;
  584. s->h263_msmpeg4 = 1;
  585. s->h263_pred = 1;
  586. s->unrestricted_mv = 1;
  587. s->msmpeg4_version= 3;
  588. avctx->delay=0;
  589. s->low_delay=1;
  590. break;
  591. case CODEC_ID_WMV1:
  592. s->out_format = FMT_H263;
  593. s->h263_msmpeg4 = 1;
  594. s->h263_pred = 1;
  595. s->unrestricted_mv = 1;
  596. s->msmpeg4_version= 4;
  597. avctx->delay=0;
  598. s->low_delay=1;
  599. break;
  600. case CODEC_ID_WMV2:
  601. s->out_format = FMT_H263;
  602. s->h263_msmpeg4 = 1;
  603. s->h263_pred = 1;
  604. s->unrestricted_mv = 1;
  605. s->msmpeg4_version= 5;
  606. avctx->delay=0;
  607. s->low_delay=1;
  608. break;
  609. #endif
  610. default:
  611. return -1;
  612. }
  613. { /* set up some save defaults, some codecs might override them later */
  614. static int done=0;
  615. if(!done){
  616. int i;
  617. done=1;
  618. default_mv_penalty= av_mallocz( sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1) );
  619. memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1));
  620. memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1));
  621. for(i=-16; i<16; i++){
  622. default_fcode_tab[i + MAX_MV]= 1;
  623. }
  624. }
  625. }
  626. s->me.mv_penalty= default_mv_penalty;
  627. s->fcode_tab= default_fcode_tab;
  628. s->y_dc_scale_table=
  629. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  630. /* dont use mv_penalty table for crap MV as it would be confused */
  631. //FIXME remove after fixing / removing old ME
  632. if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty;
  633. s->encoding = 1;
  634. /* init */
  635. if (MPV_common_init(s) < 0)
  636. return -1;
  637. ff_init_me(s);
  638. #ifdef CONFIG_ENCODERS
  639. #ifdef CONFIG_RISKY
  640. if (s->out_format == FMT_H263)
  641. h263_encode_init(s);
  642. if(s->msmpeg4_version)
  643. ff_msmpeg4_encode_init(s);
  644. #endif
  645. if (s->out_format == FMT_MPEG1)
  646. ff_mpeg1_encode_init(s);
  647. #endif
  648. /* init default q matrix */
  649. for(i=0;i<64;i++) {
  650. int j= s->idct_permutation[i];
  651. #ifdef CONFIG_RISKY
  652. if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
  653. s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
  654. s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
  655. }else if(s->out_format == FMT_H263){
  656. s->intra_matrix[j] =
  657. s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
  658. }else
  659. #endif
  660. { /* mpeg1 */
  661. s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
  662. s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
  663. }
  664. }
  665. /* precompute matrix */
  666. /* for mjpeg, we do include qscale in the matrix */
  667. if (s->out_format != FMT_MJPEG) {
  668. convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias,
  669. s->intra_matrix, s->intra_quant_bias, 1, 31);
  670. convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias,
  671. s->inter_matrix, s->inter_quant_bias, 1, 31);
  672. }
  673. if(ff_rate_control_init(s) < 0)
  674. return -1;
  675. s->picture_number = 0;
  676. s->picture_in_gop_number = 0;
  677. s->fake_picture_number = 0;
  678. /* motion detector init */
  679. s->f_code = 1;
  680. s->b_code = 1;
  681. return 0;
  682. }
  683. int MPV_encode_end(AVCodecContext *avctx)
  684. {
  685. MpegEncContext *s = avctx->priv_data;
  686. #ifdef STATS
  687. print_stats();
  688. #endif
  689. ff_rate_control_uninit(s);
  690. MPV_common_end(s);
  691. if (s->out_format == FMT_MJPEG)
  692. mjpeg_close(s);
  693. return 0;
  694. }
  695. void init_rl(RLTable *rl)
  696. {
  697. INT8 max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
  698. UINT8 index_run[MAX_RUN+1];
  699. int last, run, level, start, end, i;
  700. /* compute max_level[], max_run[] and index_run[] */
  701. for(last=0;last<2;last++) {
  702. if (last == 0) {
  703. start = 0;
  704. end = rl->last;
  705. } else {
  706. start = rl->last;
  707. end = rl->n;
  708. }
  709. memset(max_level, 0, MAX_RUN + 1);
  710. memset(max_run, 0, MAX_LEVEL + 1);
  711. memset(index_run, rl->n, MAX_RUN + 1);
  712. for(i=start;i<end;i++) {
  713. run = rl->table_run[i];
  714. level = rl->table_level[i];
  715. if (index_run[run] == rl->n)
  716. index_run[run] = i;
  717. if (level > max_level[run])
  718. max_level[run] = level;
  719. if (run > max_run[level])
  720. max_run[level] = run;
  721. }
  722. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  723. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  724. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  725. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  726. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  727. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  728. }
  729. }
  730. /* draw the edges of width 'w' of an image of size width, height */
  731. //FIXME check that this is ok for mpeg4 interlaced
  732. static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
  733. {
  734. UINT8 *ptr, *last_line;
  735. int i;
  736. last_line = buf + (height - 1) * wrap;
  737. for(i=0;i<w;i++) {
  738. /* top and bottom */
  739. memcpy(buf - (i + 1) * wrap, buf, width);
  740. memcpy(last_line + (i + 1) * wrap, last_line, width);
  741. }
  742. /* left and right */
  743. ptr = buf;
  744. for(i=0;i<height;i++) {
  745. memset(ptr - w, ptr[0], w);
  746. memset(ptr + width, ptr[width-1], w);
  747. ptr += wrap;
  748. }
  749. /* corners */
  750. for(i=0;i<w;i++) {
  751. memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
  752. memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
  753. memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
  754. memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
  755. }
  756. }
  757. static int find_unused_picture(MpegEncContext *s, int shared){
  758. int i;
  759. if(shared){
  760. for(i=0; i<MAX_PICTURE_COUNT; i++){
  761. if(s->picture[i].data[0]==NULL && s->picture[i].type==0) break;
  762. }
  763. }else{
  764. for(i=0; i<MAX_PICTURE_COUNT; i++){
  765. if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) break;
  766. }
  767. for(i=0; i<MAX_PICTURE_COUNT; i++){
  768. if(s->picture[i].data[0]==NULL) break;
  769. }
  770. }
  771. assert(i<MAX_PICTURE_COUNT);
  772. return i;
  773. }
  774. /* generic function for encode/decode called before a frame is coded/decoded */
  775. int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  776. {
  777. int i;
  778. AVFrame *pic;
  779. s->mb_skiped = 0;
  780. /* mark&release old frames */
  781. if (s->pict_type != B_TYPE && s->last_picture.data[0]) {
  782. for(i=0; i<MAX_PICTURE_COUNT; i++){
  783. //printf("%8X %d %d %X %X\n", s->picture[i].data[0], s->picture[i].type, i, s->next_picture.data[0], s->last_picture.data[0]);
  784. if(s->picture[i].data[0] == s->last_picture.data[0]){
  785. // s->picture[i].reference=0;
  786. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  787. break;
  788. }
  789. }
  790. assert(i<MAX_PICTURE_COUNT);
  791. /* release forgotten pictures */
  792. /* if(mpeg124/h263) */
  793. if(!s->encoding){
  794. for(i=0; i<MAX_PICTURE_COUNT; i++){
  795. if(s->picture[i].data[0] && s->picture[i].data[0] != s->next_picture.data[0] && s->picture[i].reference){
  796. fprintf(stderr, "releasing zombie picture\n");
  797. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  798. }
  799. }
  800. }
  801. }
  802. alloc:
  803. if(!s->encoding){
  804. i= find_unused_picture(s, 0);
  805. pic= (AVFrame*)&s->picture[i];
  806. pic->reference= s->pict_type != B_TYPE;
  807. pic->coded_picture_number= s->current_picture.coded_picture_number+1;
  808. alloc_picture(s, (Picture*)pic, 0);
  809. s->current_picture= s->picture[i];
  810. }
  811. if (s->pict_type != B_TYPE) {
  812. s->last_picture= s->next_picture;
  813. s->next_picture= s->current_picture;
  814. }
  815. if(s->pict_type != I_TYPE && s->last_picture.data[0]==NULL){
  816. fprintf(stderr, "warning: first frame is no keyframe\n");
  817. assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
  818. goto alloc;
  819. }
  820. s->hurry_up= s->avctx->hurry_up;
  821. s->error_resilience= avctx->error_resilience;
  822. /* set dequantizer, we cant do it during init as it might change for mpeg4
  823. and we cant do it in the header decode as init isnt called for mpeg4 there yet */
  824. if(s->out_format == FMT_H263){
  825. if(s->mpeg_quant)
  826. s->dct_unquantize = s->dct_unquantize_mpeg2;
  827. else
  828. s->dct_unquantize = s->dct_unquantize_h263;
  829. }else
  830. s->dct_unquantize = s->dct_unquantize_mpeg1;
  831. return 0;
  832. }
  833. /* generic function for encode/decode called after a frame has been coded/decoded */
  834. void MPV_frame_end(MpegEncContext *s)
  835. {
  836. int i;
  837. /* draw edge for correct motion prediction if outside */
  838. if(s->codec_id!=CODEC_ID_SVQ1){
  839. if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  840. draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
  841. draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  842. draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  843. }
  844. }
  845. emms_c();
  846. s->last_pict_type = s->pict_type;
  847. if(s->pict_type!=B_TYPE){
  848. s->last_non_b_pict_type= s->pict_type;
  849. }
  850. s->current_picture.quality= s->qscale; //FIXME get average of qscale_table
  851. s->current_picture.pict_type= s->pict_type;
  852. s->current_picture.key_frame= s->pict_type == I_TYPE;
  853. /* copy back current_picture variables */
  854. for(i=0; i<MAX_PICTURE_COUNT; i++){
  855. if(s->picture[i].data[0] == s->current_picture.data[0]){
  856. s->picture[i]= s->current_picture;
  857. break;
  858. }
  859. }
  860. assert(i<MAX_PICTURE_COUNT);
  861. /* release non refernce frames */
  862. for(i=0; i<MAX_PICTURE_COUNT; i++){
  863. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/)
  864. s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
  865. }
  866. if(s->avctx->debug&FF_DEBUG_SKIP){
  867. int x,y;
  868. for(y=0; y<s->mb_height; y++){
  869. for(x=0; x<s->mb_width; x++){
  870. int count= s->mbskip_table[x + y*s->mb_width];
  871. if(count>9) count=9;
  872. printf(" %1d", count);
  873. }
  874. printf("\n");
  875. }
  876. printf("pict type: %d\n", s->pict_type);
  877. }
  878. }
  879. static int get_sae(uint8_t *src, int ref, int stride){
  880. int x,y;
  881. int acc=0;
  882. for(y=0; y<16; y++){
  883. for(x=0; x<16; x++){
  884. acc+= ABS(src[x+y*stride] - ref);
  885. }
  886. }
  887. return acc;
  888. }
  889. static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
  890. int x, y, w, h;
  891. int acc=0;
  892. w= s->width &~15;
  893. h= s->height&~15;
  894. for(y=0; y<h; y+=16){
  895. for(x=0; x<w; x+=16){
  896. int offset= x + y*stride;
  897. int sad = s->dsp.pix_abs16x16(src + offset, ref + offset, stride);
  898. int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
  899. int sae = get_sae(src + offset, mean, stride);
  900. acc+= sae + 500 < sad;
  901. }
  902. }
  903. return acc;
  904. }
  905. static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
  906. AVFrame *pic;
  907. int i;
  908. const int encoding_delay= s->max_b_frames;
  909. int direct=1;
  910. if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
  911. if(pic_arg->linesize[0] != s->linesize) direct=0;
  912. if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
  913. if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
  914. // printf("%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
  915. if(direct){
  916. i= find_unused_picture(s, 1);
  917. pic= (AVFrame*)&s->picture[i];
  918. pic->reference= 1;
  919. for(i=0; i<4; i++){
  920. pic->data[i]= pic_arg->data[i];
  921. pic->linesize[i]= pic_arg->linesize[i];
  922. }
  923. alloc_picture(s, (Picture*)pic, 1);
  924. }else{
  925. i= find_unused_picture(s, 0);
  926. pic= (AVFrame*)&s->picture[i];
  927. pic->reference= 1;
  928. alloc_picture(s, (Picture*)pic, 0);
  929. if( pic->data[0] == pic_arg->data[0]
  930. && pic->data[1] == pic_arg->data[1]
  931. && pic->data[2] == pic_arg->data[2]){
  932. // empty
  933. }else{
  934. int h_chroma_shift, v_chroma_shift;
  935. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  936. for(i=0; i<3; i++){
  937. int src_stride= pic_arg->linesize[i];
  938. int dst_stride= i ? s->uvlinesize : s->linesize;
  939. int h_shift= i ? h_chroma_shift : 0;
  940. int v_shift= i ? v_chroma_shift : 0;
  941. int w= s->width >>h_shift;
  942. int h= s->height>>v_shift;
  943. uint8_t *src= pic_arg->data[i];
  944. uint8_t *dst= pic->data[i];
  945. if(src_stride==dst_stride)
  946. memcpy(dst, src, src_stride*h);
  947. else{
  948. while(h--){
  949. memcpy(dst, src, w);
  950. dst += dst_stride;
  951. src += src_stride;
  952. }
  953. }
  954. }
  955. }
  956. }
  957. pic->quality= pic_arg->quality;
  958. pic->pict_type= pic_arg->pict_type;
  959. pic->pts = pic_arg->pts;
  960. if(s->input_picture[encoding_delay])
  961. pic->display_picture_number= s->input_picture[encoding_delay]->display_picture_number + 1;
  962. /* shift buffer entries */
  963. for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
  964. s->input_picture[i-1]= s->input_picture[i];
  965. s->input_picture[encoding_delay]= (Picture*)pic;
  966. return 0;
  967. }
  968. static void select_input_picture(MpegEncContext *s){
  969. int i;
  970. const int encoding_delay= s->max_b_frames;
  971. int coded_pic_num=0;
  972. if(s->reordered_input_picture[0])
  973. coded_pic_num= s->reordered_input_picture[0]->coded_picture_number + 1;
  974. for(i=1; i<MAX_PICTURE_COUNT; i++)
  975. s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
  976. s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
  977. /* set next picture types & ordering */
  978. if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
  979. if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture.data[0]==NULL || s->intra_only){
  980. s->reordered_input_picture[0]= s->input_picture[0];
  981. s->reordered_input_picture[0]->pict_type= I_TYPE;
  982. s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
  983. }else{
  984. int b_frames;
  985. if(s->flags&CODEC_FLAG_PASS2){
  986. for(i=0; i<s->max_b_frames+1; i++){
  987. int pict_num= s->input_picture[0]->display_picture_number + i;
  988. int pict_type= s->rc_context.entry[pict_num].new_pict_type;
  989. s->input_picture[i]->pict_type= pict_type;
  990. if(i + 1 >= s->rc_context.num_entries) break;
  991. }
  992. }
  993. if(s->input_picture[0]->pict_type){
  994. /* user selected pict_type */
  995. for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){
  996. if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;
  997. }
  998. if(b_frames > s->max_b_frames){
  999. fprintf(stderr, "warning, too many bframes in a row\n");
  1000. b_frames = s->max_b_frames;
  1001. }
  1002. }else if(s->b_frame_strategy==0){
  1003. b_frames= s->max_b_frames;
  1004. }else if(s->b_frame_strategy==1){
  1005. for(i=1; i<s->max_b_frames+1; i++){
  1006. if(s->input_picture[i]->b_frame_score==0){
  1007. s->input_picture[i]->b_frame_score=
  1008. get_intra_count(s, s->input_picture[i ]->data[0],
  1009. s->input_picture[i-1]->data[0], s->linesize) + 1;
  1010. }
  1011. }
  1012. for(i=0; i<s->max_b_frames; i++){
  1013. if(s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
  1014. }
  1015. b_frames= FFMAX(0, i-1);
  1016. /* reset scores */
  1017. for(i=0; i<b_frames+1; i++){
  1018. s->input_picture[i]->b_frame_score=0;
  1019. }
  1020. }else{
  1021. fprintf(stderr, "illegal b frame strategy\n");
  1022. b_frames=0;
  1023. }
  1024. emms_c();
  1025. //static int b_count=0;
  1026. //b_count+= b_frames;
  1027. //printf("b_frames: %d\n", b_count);
  1028. s->reordered_input_picture[0]= s->input_picture[b_frames];
  1029. if( s->picture_in_gop_number + b_frames >= s->gop_size
  1030. || s->reordered_input_picture[0]->pict_type== I_TYPE)
  1031. s->reordered_input_picture[0]->pict_type= I_TYPE;
  1032. else
  1033. s->reordered_input_picture[0]->pict_type= P_TYPE;
  1034. s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
  1035. for(i=0; i<b_frames; i++){
  1036. coded_pic_num++;
  1037. s->reordered_input_picture[i+1]= s->input_picture[i];
  1038. s->reordered_input_picture[i+1]->pict_type= B_TYPE;
  1039. s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num;
  1040. }
  1041. }
  1042. }
  1043. if(s->reordered_input_picture[0]){
  1044. s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE;
  1045. if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
  1046. int i= find_unused_picture(s, 0);
  1047. Picture *pic= &s->picture[i];
  1048. s->new_picture= *s->reordered_input_picture[0];
  1049. /* mark us unused / free shared pic */
  1050. for(i=0; i<4; i++)
  1051. s->reordered_input_picture[0]->data[i]= NULL;
  1052. s->reordered_input_picture[0]->type= 0;
  1053. pic->pict_type = s->reordered_input_picture[0]->pict_type;
  1054. pic->quality = s->reordered_input_picture[0]->quality;
  1055. pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number;
  1056. pic->reference = s->reordered_input_picture[0]->reference;
  1057. alloc_picture(s, pic, 0);
  1058. s->current_picture= *pic;
  1059. }else{
  1060. assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
  1061. || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
  1062. s->new_picture= *s->reordered_input_picture[0];
  1063. for(i=0; i<4; i++){
  1064. s->reordered_input_picture[0]->data[i]-=16; //FIXME dirty
  1065. }
  1066. s->current_picture= *s->reordered_input_picture[0];
  1067. }
  1068. s->picture_number= s->new_picture.display_picture_number;
  1069. //printf("dpn:%d\n", s->picture_number);
  1070. }else{
  1071. memset(&s->new_picture, 0, sizeof(Picture));
  1072. }
  1073. }
  1074. int MPV_encode_picture(AVCodecContext *avctx,
  1075. unsigned char *buf, int buf_size, void *data)
  1076. {
  1077. MpegEncContext *s = avctx->priv_data;
  1078. AVFrame *pic_arg = data;
  1079. int i;
  1080. init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
  1081. s->picture_in_gop_number++;
  1082. load_input_picture(s, pic_arg);
  1083. select_input_picture(s);
  1084. /* output? */
  1085. if(s->new_picture.data[0]){
  1086. s->pict_type= s->new_picture.pict_type;
  1087. if (s->fixed_qscale){ /* the ratecontrol needs the last qscale so we dont touch it for CBR */
  1088. s->qscale= (int)(s->new_picture.quality+0.5);
  1089. assert(s->qscale);
  1090. }
  1091. //emms_c();
  1092. //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
  1093. MPV_frame_start(s, avctx);
  1094. encode_picture(s, s->picture_number);
  1095. avctx->real_pict_num = s->picture_number;
  1096. avctx->header_bits = s->header_bits;
  1097. avctx->mv_bits = s->mv_bits;
  1098. avctx->misc_bits = s->misc_bits;
  1099. avctx->i_tex_bits = s->i_tex_bits;
  1100. avctx->p_tex_bits = s->p_tex_bits;
  1101. avctx->i_count = s->i_count;
  1102. avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
  1103. avctx->skip_count = s->skip_count;
  1104. MPV_frame_end(s);
  1105. if (s->out_format == FMT_MJPEG)
  1106. mjpeg_picture_trailer(s);
  1107. if(s->flags&CODEC_FLAG_PASS1)
  1108. ff_write_pass1_stats(s);
  1109. }
  1110. s->input_picture_number++;
  1111. flush_put_bits(&s->pb);
  1112. s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
  1113. s->total_bits += s->frame_bits;
  1114. avctx->frame_bits = s->frame_bits;
  1115. for(i=0; i<4; i++){
  1116. avctx->error[i] += s->current_picture.error[i];
  1117. }
  1118. return pbBufPtr(&s->pb) - s->pb.buf;
  1119. }
  1120. static inline void gmc1_motion(MpegEncContext *s,
  1121. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  1122. int dest_offset,
  1123. UINT8 **ref_picture, int src_offset)
  1124. {
  1125. UINT8 *ptr;
  1126. int offset, src_x, src_y, linesize, uvlinesize;
  1127. int motion_x, motion_y;
  1128. int emu=0;
  1129. motion_x= s->sprite_offset[0][0];
  1130. motion_y= s->sprite_offset[0][1];
  1131. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  1132. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  1133. motion_x<<=(3-s->sprite_warping_accuracy);
  1134. motion_y<<=(3-s->sprite_warping_accuracy);
  1135. src_x = clip(src_x, -16, s->width);
  1136. if (src_x == s->width)
  1137. motion_x =0;
  1138. src_y = clip(src_y, -16, s->height);
  1139. if (src_y == s->height)
  1140. motion_y =0;
  1141. linesize = s->linesize;
  1142. uvlinesize = s->uvlinesize;
  1143. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  1144. dest_y+=dest_offset;
  1145. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1146. if(src_x<0 || src_y<0 || src_x + 17 >= s->h_edge_pos
  1147. || src_y + 17 >= s->v_edge_pos){
  1148. ff_emulated_edge_mc(s, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  1149. ptr= s->edge_emu_buffer;
  1150. }
  1151. }
  1152. if((motion_x|motion_y)&7){
  1153. s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1154. s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1155. }else{
  1156. int dxy;
  1157. dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
  1158. if (s->no_rounding){
  1159. s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
  1160. }else{
  1161. s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
  1162. }
  1163. }
  1164. if(s->flags&CODEC_FLAG_GRAY) return;
  1165. motion_x= s->sprite_offset[1][0];
  1166. motion_y= s->sprite_offset[1][1];
  1167. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  1168. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  1169. motion_x<<=(3-s->sprite_warping_accuracy);
  1170. motion_y<<=(3-s->sprite_warping_accuracy);
  1171. src_x = clip(src_x, -8, s->width>>1);
  1172. if (src_x == s->width>>1)
  1173. motion_x =0;
  1174. src_y = clip(src_y, -8, s->height>>1);
  1175. if (src_y == s->height>>1)
  1176. motion_y =0;
  1177. offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
  1178. ptr = ref_picture[1] + offset;
  1179. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1180. if(src_x<0 || src_y<0 || src_x + 9 >= s->h_edge_pos>>1
  1181. || src_y + 9 >= s->v_edge_pos>>1){
  1182. ff_emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1183. ptr= s->edge_emu_buffer;
  1184. emu=1;
  1185. }
  1186. }
  1187. s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1188. ptr = ref_picture[2] + offset;
  1189. if(emu){
  1190. ff_emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1191. ptr= s->edge_emu_buffer;
  1192. }
  1193. s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1194. return;
  1195. }
  1196. static inline void gmc_motion(MpegEncContext *s,
  1197. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  1198. int dest_offset,
  1199. UINT8 **ref_picture, int src_offset)
  1200. {
  1201. UINT8 *ptr;
  1202. int linesize, uvlinesize;
  1203. const int a= s->sprite_warping_accuracy;
  1204. int ox, oy;
  1205. linesize = s->linesize;
  1206. uvlinesize = s->uvlinesize;
  1207. ptr = ref_picture[0] + src_offset;
  1208. dest_y+=dest_offset;
  1209. ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
  1210. oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
  1211. s->dsp.gmc(dest_y, ptr, linesize, 16,
  1212. ox,
  1213. oy,
  1214. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1215. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1216. a+1, (1<<(2*a+1)) - s->no_rounding,
  1217. s->h_edge_pos, s->v_edge_pos);
  1218. s->dsp.gmc(dest_y+8, ptr, linesize, 16,
  1219. ox + s->sprite_delta[0][0]*8,
  1220. oy + s->sprite_delta[1][0]*8,
  1221. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1222. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1223. a+1, (1<<(2*a+1)) - s->no_rounding,
  1224. s->h_edge_pos, s->v_edge_pos);
  1225. if(s->flags&CODEC_FLAG_GRAY) return;
  1226. dest_cb+=dest_offset>>1;
  1227. dest_cr+=dest_offset>>1;
  1228. ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
  1229. oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
  1230. ptr = ref_picture[1] + (src_offset>>1);
  1231. s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
  1232. ox,
  1233. oy,
  1234. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1235. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1236. a+1, (1<<(2*a+1)) - s->no_rounding,
  1237. s->h_edge_pos>>1, s->v_edge_pos>>1);
  1238. ptr = ref_picture[2] + (src_offset>>1);
  1239. s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
  1240. ox,
  1241. oy,
  1242. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1243. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1244. a+1, (1<<(2*a+1)) - s->no_rounding,
  1245. s->h_edge_pos>>1, s->v_edge_pos>>1);
  1246. }
  1247. void ff_emulated_edge_mc(MpegEncContext *s, UINT8 *src, int linesize, int block_w, int block_h,
  1248. int src_x, int src_y, int w, int h){
  1249. int x, y;
  1250. int start_y, start_x, end_y, end_x;
  1251. UINT8 *buf= s->edge_emu_buffer;
  1252. if(src_y>= h){
  1253. src+= (h-1-src_y)*linesize;
  1254. src_y=h-1;
  1255. }else if(src_y<=-block_h){
  1256. src+= (1-block_h-src_y)*linesize;
  1257. src_y=1-block_h;
  1258. }
  1259. if(src_x>= w){
  1260. src+= (w-1-src_x);
  1261. src_x=w-1;
  1262. }else if(src_x<=-block_w){
  1263. src+= (1-block_w-src_x);
  1264. src_x=1-block_w;
  1265. }
  1266. start_y= FFMAX(0, -src_y);
  1267. start_x= FFMAX(0, -src_x);
  1268. end_y= FFMIN(block_h, h-src_y);
  1269. end_x= FFMIN(block_w, w-src_x);
  1270. // copy existing part
  1271. for(y=start_y; y<end_y; y++){
  1272. for(x=start_x; x<end_x; x++){
  1273. buf[x + y*linesize]= src[x + y*linesize];
  1274. }
  1275. }
  1276. //top
  1277. for(y=0; y<start_y; y++){
  1278. for(x=start_x; x<end_x; x++){
  1279. buf[x + y*linesize]= buf[x + start_y*linesize];
  1280. }
  1281. }
  1282. //bottom
  1283. for(y=end_y; y<block_h; y++){
  1284. for(x=start_x; x<end_x; x++){
  1285. buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
  1286. }
  1287. }
  1288. for(y=0; y<block_h; y++){
  1289. //left
  1290. for(x=0; x<start_x; x++){
  1291. buf[x + y*linesize]= buf[start_x + y*linesize];
  1292. }
  1293. //right
  1294. for(x=end_x; x<block_w; x++){
  1295. buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
  1296. }
  1297. }
  1298. }
  1299. /* apply one mpeg motion vector to the three components */
  1300. static inline void mpeg_motion(MpegEncContext *s,
  1301. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  1302. int dest_offset,
  1303. UINT8 **ref_picture, int src_offset,
  1304. int field_based, op_pixels_func (*pix_op)[4],
  1305. int motion_x, int motion_y, int h)
  1306. {
  1307. UINT8 *ptr;
  1308. int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
  1309. int emu=0;
  1310. #if 0
  1311. if(s->quarter_sample)
  1312. {
  1313. motion_x>>=1;
  1314. motion_y>>=1;
  1315. }
  1316. #endif
  1317. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1318. src_x = s->mb_x * 16 + (motion_x >> 1);
  1319. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
  1320. /* WARNING: do no forget half pels */
  1321. height = s->height >> field_based;
  1322. v_edge_pos = s->v_edge_pos >> field_based;
  1323. src_x = clip(src_x, -16, s->width);
  1324. if (src_x == s->width)
  1325. dxy &= ~1;
  1326. src_y = clip(src_y, -16, height);
  1327. if (src_y == height)
  1328. dxy &= ~2;
  1329. linesize = s->linesize << field_based;
  1330. uvlinesize = s->uvlinesize << field_based;
  1331. ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
  1332. dest_y += dest_offset;
  1333. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1334. if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->h_edge_pos
  1335. || src_y + (motion_y&1) + h > v_edge_pos){
  1336. ff_emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based,
  1337. src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
  1338. ptr= s->edge_emu_buffer + src_offset;
  1339. emu=1;
  1340. }
  1341. }
  1342. pix_op[0][dxy](dest_y, ptr, linesize, h);
  1343. if(s->flags&CODEC_FLAG_GRAY) return;
  1344. if (s->out_format == FMT_H263) {
  1345. dxy = 0;
  1346. if ((motion_x & 3) != 0)
  1347. dxy |= 1;
  1348. if ((motion_y & 3) != 0)
  1349. dxy |= 2;
  1350. mx = motion_x >> 2;
  1351. my = motion_y >> 2;
  1352. } else {
  1353. mx = motion_x / 2;
  1354. my = motion_y / 2;
  1355. dxy = ((my & 1) << 1) | (mx & 1);
  1356. mx >>= 1;
  1357. my >>= 1;
  1358. }
  1359. src_x = s->mb_x * 8 + mx;
  1360. src_y = s->mb_y * (8 >> field_based) + my;
  1361. src_x = clip(src_x, -8, s->width >> 1);
  1362. if (src_x == (s->width >> 1))
  1363. dxy &= ~1;
  1364. src_y = clip(src_y, -8, height >> 1);
  1365. if (src_y == (height >> 1))
  1366. dxy &= ~2;
  1367. offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
  1368. ptr = ref_picture[1] + offset;
  1369. if(emu){
  1370. ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
  1371. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1372. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1373. }
  1374. pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1375. ptr = ref_picture[2] + offset;
  1376. if(emu){
  1377. ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
  1378. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1379. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1380. }
  1381. pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1382. }
  1383. static inline void qpel_motion(MpegEncContext *s,
  1384. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  1385. int dest_offset,
  1386. UINT8 **ref_picture, int src_offset,
  1387. int field_based, op_pixels_func (*pix_op)[4],
  1388. qpel_mc_func (*qpix_op)[16],
  1389. int motion_x, int motion_y, int h)
  1390. {
  1391. UINT8 *ptr;
  1392. int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
  1393. int emu=0;
  1394. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  1395. src_x = s->mb_x * 16 + (motion_x >> 2);
  1396. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  1397. height = s->height >> field_based;
  1398. v_edge_pos = s->v_edge_pos >> field_based;
  1399. src_x = clip(src_x, -16, s->width);
  1400. if (src_x == s->width)
  1401. dxy &= ~3;
  1402. src_y = clip(src_y, -16, height);
  1403. if (src_y == height)
  1404. dxy &= ~12;
  1405. linesize = s->linesize << field_based;
  1406. uvlinesize = s->uvlinesize << field_based;
  1407. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  1408. dest_y += dest_offset;
  1409. //printf("%d %d %d\n", src_x, src_y, dxy);
  1410. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1411. if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->h_edge_pos
  1412. || src_y + (motion_y&3) + h > v_edge_pos){
  1413. ff_emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based,
  1414. src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
  1415. ptr= s->edge_emu_buffer + src_offset;
  1416. emu=1;
  1417. }
  1418. }
  1419. if(!field_based)
  1420. qpix_op[0][dxy](dest_y, ptr, linesize);
  1421. else{
  1422. //damn interlaced mode
  1423. //FIXME boundary mirroring is not exactly correct here
  1424. qpix_op[1][dxy](dest_y , ptr , linesize);
  1425. qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
  1426. }
  1427. if(s->flags&CODEC_FLAG_GRAY) return;
  1428. if(field_based){
  1429. mx= motion_x/2;
  1430. my= motion_y>>1;
  1431. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
  1432. mx= (motion_x>>1)|(motion_x&1);
  1433. my= (motion_y>>1)|(motion_y&1);
  1434. }else{
  1435. mx= motion_x/2;
  1436. my= motion_y/2;
  1437. }
  1438. mx= (mx>>1)|(mx&1);
  1439. my= (my>>1)|(my&1);
  1440. dxy= (mx&1) | ((my&1)<<1);
  1441. mx>>=1;
  1442. my>>=1;
  1443. src_x = s->mb_x * 8 + mx;
  1444. src_y = s->mb_y * (8 >> field_based) + my;
  1445. src_x = clip(src_x, -8, s->width >> 1);
  1446. if (src_x == (s->width >> 1))
  1447. dxy &= ~1;
  1448. src_y = clip(src_y, -8, height >> 1);
  1449. if (src_y == (height >> 1))
  1450. dxy &= ~2;
  1451. offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
  1452. ptr = ref_picture[1] + offset;
  1453. if(emu){
  1454. ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
  1455. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1456. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1457. }
  1458. pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1459. ptr = ref_picture[2] + offset;
  1460. if(emu){
  1461. ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
  1462. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1463. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1464. }
  1465. pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1466. }
  1467. inline int ff_h263_round_chroma(int x){
  1468. if (x >= 0)
  1469. return (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
  1470. else {
  1471. x = -x;
  1472. return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
  1473. }
  1474. }
  1475. static inline void MPV_motion(MpegEncContext *s,
  1476. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  1477. int dir, UINT8 **ref_picture,
  1478. op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
  1479. {
  1480. int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
  1481. int mb_x, mb_y, i;
  1482. UINT8 *ptr, *dest;
  1483. int emu=0;
  1484. mb_x = s->mb_x;
  1485. mb_y = s->mb_y;
  1486. switch(s->mv_type) {
  1487. case MV_TYPE_16X16:
  1488. #ifdef CONFIG_RISKY
  1489. if(s->mcsel){
  1490. if(s->real_sprite_warping_points==1){
  1491. gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
  1492. ref_picture, 0);
  1493. }else{
  1494. gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
  1495. ref_picture, 0);
  1496. }
  1497. }else if(s->quarter_sample){
  1498. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  1499. ref_picture, 0,
  1500. 0, pix_op, qpix_op,
  1501. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1502. }else if(s->mspel){
  1503. ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
  1504. ref_picture, pix_op,
  1505. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1506. }else
  1507. #endif
  1508. {
  1509. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1510. ref_picture, 0,
  1511. 0, pix_op,
  1512. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1513. }
  1514. break;
  1515. case MV_TYPE_8X8:
  1516. mx = 0;
  1517. my = 0;
  1518. if(s->quarter_sample){
  1519. for(i=0;i<4;i++) {
  1520. motion_x = s->mv[dir][i][0];
  1521. motion_y = s->mv[dir][i][1];
  1522. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  1523. src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
  1524. src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
  1525. /* WARNING: do no forget half pels */
  1526. src_x = clip(src_x, -16, s->width);
  1527. if (src_x == s->width)
  1528. dxy &= ~3;
  1529. src_y = clip(src_y, -16, s->height);
  1530. if (src_y == s->height)
  1531. dxy &= ~12;
  1532. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  1533. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1534. if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 8 > s->h_edge_pos
  1535. || src_y + (motion_y&3) + 8 > s->v_edge_pos){
  1536. ff_emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  1537. ptr= s->edge_emu_buffer;
  1538. }
  1539. }
  1540. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  1541. qpix_op[1][dxy](dest, ptr, s->linesize);
  1542. mx += s->mv[dir][i][0]/2;
  1543. my += s->mv[dir][i][1]/2;
  1544. }
  1545. }else{
  1546. for(i=0;i<4;i++) {
  1547. motion_x = s->mv[dir][i][0];
  1548. motion_y = s->mv[dir][i][1];
  1549. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1550. src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
  1551. src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
  1552. /* WARNING: do no forget half pels */
  1553. src_x = clip(src_x, -16, s->width);
  1554. if (src_x == s->width)
  1555. dxy &= ~1;
  1556. src_y = clip(src_y, -16, s->height);
  1557. if (src_y == s->height)
  1558. dxy &= ~2;
  1559. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  1560. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1561. if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->h_edge_pos
  1562. || src_y + (motion_y&1) + 8 > s->v_edge_pos){
  1563. ff_emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  1564. ptr= s->edge_emu_buffer;
  1565. }
  1566. }
  1567. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  1568. pix_op[1][dxy](dest, ptr, s->linesize, 8);
  1569. mx += s->mv[dir][i][0];
  1570. my += s->mv[dir][i][1];
  1571. }
  1572. }
  1573. if(s->flags&CODEC_FLAG_GRAY) break;
  1574. /* In case of 8X8, we construct a single chroma motion vector
  1575. with a special rounding */
  1576. mx= ff_h263_round_chroma(mx);
  1577. my= ff_h263_round_chroma(my);
  1578. dxy = ((my & 1) << 1) | (mx & 1);
  1579. mx >>= 1;
  1580. my >>= 1;
  1581. src_x = mb_x * 8 + mx;
  1582. src_y = mb_y * 8 + my;
  1583. src_x = clip(src_x, -8, s->width/2);
  1584. if (src_x == s->width/2)
  1585. dxy &= ~1;
  1586. src_y = clip(src_y, -8, s->height/2);
  1587. if (src_y == s->height/2)
  1588. dxy &= ~2;
  1589. offset = (src_y * (s->uvlinesize)) + src_x;
  1590. ptr = ref_picture[1] + offset;
  1591. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1592. if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->h_edge_pos>>1
  1593. || src_y + (dxy>>1) + 8 > s->v_edge_pos>>1){
  1594. ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1595. ptr= s->edge_emu_buffer;
  1596. emu=1;
  1597. }
  1598. }
  1599. pix_op[1][dxy](dest_cb, ptr, s->uvlinesize, 8);
  1600. ptr = ref_picture[2] + offset;
  1601. if(emu){
  1602. ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1603. ptr= s->edge_emu_buffer;
  1604. }
  1605. pix_op[1][dxy](dest_cr, ptr, s->uvlinesize, 8);
  1606. break;
  1607. case MV_TYPE_FIELD:
  1608. if (s->picture_structure == PICT_FRAME) {
  1609. if(s->quarter_sample){
  1610. /* top field */
  1611. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  1612. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  1613. 1, pix_op, qpix_op,
  1614. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  1615. /* bottom field */
  1616. qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  1617. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  1618. 1, pix_op, qpix_op,
  1619. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  1620. }else{
  1621. /* top field */
  1622. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1623. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  1624. 1, pix_op,
  1625. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  1626. /* bottom field */
  1627. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  1628. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  1629. 1, pix_op,
  1630. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  1631. }
  1632. } else {
  1633. }
  1634. break;
  1635. }
  1636. }
  1637. /* put block[] to dest[] */
  1638. static inline void put_dct(MpegEncContext *s,
  1639. DCTELEM *block, int i, UINT8 *dest, int line_size)
  1640. {
  1641. s->dct_unquantize(s, block, i, s->qscale);
  1642. s->idct_put (dest, line_size, block);
  1643. }
  1644. /* add block[] to dest[] */
  1645. static inline void add_dct(MpegEncContext *s,
  1646. DCTELEM *block, int i, UINT8 *dest, int line_size)
  1647. {
  1648. if (s->block_last_index[i] >= 0) {
  1649. s->idct_add (dest, line_size, block);
  1650. }
  1651. }
  1652. static inline void add_dequant_dct(MpegEncContext *s,
  1653. DCTELEM *block, int i, UINT8 *dest, int line_size)
  1654. {
  1655. if (s->block_last_index[i] >= 0) {
  1656. s->dct_unquantize(s, block, i, s->qscale);
  1657. s->idct_add (dest, line_size, block);
  1658. }
  1659. }
  1660. /**
  1661. * cleans dc, ac, coded_block for the current non intra MB
  1662. */
  1663. void ff_clean_intra_table_entries(MpegEncContext *s)
  1664. {
  1665. int wrap = s->block_wrap[0];
  1666. int xy = s->block_index[0];
  1667. s->dc_val[0][xy ] =
  1668. s->dc_val[0][xy + 1 ] =
  1669. s->dc_val[0][xy + wrap] =
  1670. s->dc_val[0][xy + 1 + wrap] = 1024;
  1671. /* ac pred */
  1672. memset(s->ac_val[0][xy ], 0, 32 * sizeof(INT16));
  1673. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(INT16));
  1674. if (s->msmpeg4_version>=3) {
  1675. s->coded_block[xy ] =
  1676. s->coded_block[xy + 1 ] =
  1677. s->coded_block[xy + wrap] =
  1678. s->coded_block[xy + 1 + wrap] = 0;
  1679. }
  1680. /* chroma */
  1681. wrap = s->block_wrap[4];
  1682. xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
  1683. s->dc_val[1][xy] =
  1684. s->dc_val[2][xy] = 1024;
  1685. /* ac pred */
  1686. memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
  1687. memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
  1688. s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
  1689. }
  1690. /* generic function called after a macroblock has been parsed by the
  1691. decoder or after it has been encoded by the encoder.
  1692. Important variables used:
  1693. s->mb_intra : true if intra macroblock
  1694. s->mv_dir : motion vector direction
  1695. s->mv_type : motion vector type
  1696. s->mv : motion vector
  1697. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1698. */
  1699. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  1700. {
  1701. int mb_x, mb_y;
  1702. const int mb_xy = s->mb_y * s->mb_width + s->mb_x;
  1703. mb_x = s->mb_x;
  1704. mb_y = s->mb_y;
  1705. s->current_picture.qscale_table[mb_xy]= s->qscale;
  1706. /* update DC predictors for P macroblocks */
  1707. if (!s->mb_intra) {
  1708. if (s->h263_pred || s->h263_aic) {
  1709. if(s->mbintra_table[mb_xy])
  1710. ff_clean_intra_table_entries(s);
  1711. } else {
  1712. s->last_dc[0] =
  1713. s->last_dc[1] =
  1714. s->last_dc[2] = 128 << s->intra_dc_precision;
  1715. }
  1716. }
  1717. else if (s->h263_pred || s->h263_aic)
  1718. s->mbintra_table[mb_xy]=1;
  1719. /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */
  1720. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here
  1721. //FIXME a lot of thet is only needed for !low_delay
  1722. const int wrap = s->block_wrap[0];
  1723. const int xy = s->block_index[0];
  1724. const int mb_index= s->mb_x + s->mb_y*s->mb_width;
  1725. if(s->mv_type == MV_TYPE_8X8){
  1726. s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_4MV;
  1727. } else {
  1728. int motion_x, motion_y;
  1729. if (s->mb_intra) {
  1730. motion_x = 0;
  1731. motion_y = 0;
  1732. if(s->co_located_type_table)
  1733. s->co_located_type_table[mb_index]= 0;
  1734. } else if (s->mv_type == MV_TYPE_16X16) {
  1735. motion_x = s->mv[0][0][0];
  1736. motion_y = s->mv[0][0][1];
  1737. if(s->co_located_type_table)
  1738. s->co_located_type_table[mb_index]= 0;
  1739. } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
  1740. int i;
  1741. motion_x = s->mv[0][0][0] + s->mv[0][1][0];
  1742. motion_y = s->mv[0][0][1] + s->mv[0][1][1];
  1743. motion_x = (motion_x>>1) | (motion_x&1);
  1744. for(i=0; i<2; i++){
  1745. s->field_mv_table[mb_index][i][0]= s->mv[0][i][0];
  1746. s->field_mv_table[mb_index][i][1]= s->mv[0][i][1];
  1747. s->field_select_table[mb_index][i]= s->field_select[0][i];
  1748. }
  1749. s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_FIELDMV;
  1750. }
  1751. /* no update if 8X8 because it has been done during parsing */
  1752. s->motion_val[xy][0] = motion_x;
  1753. s->motion_val[xy][1] = motion_y;
  1754. s->motion_val[xy + 1][0] = motion_x;
  1755. s->motion_val[xy + 1][1] = motion_y;
  1756. s->motion_val[xy + wrap][0] = motion_x;
  1757. s->motion_val[xy + wrap][1] = motion_y;
  1758. s->motion_val[xy + 1 + wrap][0] = motion_x;
  1759. s->motion_val[xy + 1 + wrap][1] = motion_y;
  1760. }
  1761. }
  1762. if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
  1763. UINT8 *dest_y, *dest_cb, *dest_cr;
  1764. int dct_linesize, dct_offset;
  1765. op_pixels_func (*op_pix)[4];
  1766. qpel_mc_func (*op_qpix)[16];
  1767. /* avoid copy if macroblock skipped in last frame too */
  1768. if (s->pict_type != B_TYPE) {
  1769. s->current_picture.mbskip_table[mb_xy]= s->mb_skiped;
  1770. }
  1771. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1772. if(!s->encoding){
  1773. UINT8 *mbskip_ptr = &s->mbskip_table[mb_xy];
  1774. const int age= s->current_picture.age;
  1775. assert(age);
  1776. if (s->mb_skiped) {
  1777. s->mb_skiped= 0;
  1778. assert(s->pict_type!=I_TYPE);
  1779. (*mbskip_ptr) ++; /* indicate that this time we skiped it */
  1780. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1781. /* if previous was skipped too, then nothing to do ! */
  1782. if (*mbskip_ptr >= age && s->current_picture.reference){
  1783. return;
  1784. }
  1785. } else if(!s->current_picture.reference){
  1786. (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
  1787. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1788. } else{
  1789. *mbskip_ptr = 0; /* not skipped */
  1790. }
  1791. }else
  1792. s->mb_skiped= 0;
  1793. if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band){
  1794. dest_y = s->current_picture.data[0] + mb_x * 16;
  1795. dest_cb = s->current_picture.data[1] + mb_x * 8;
  1796. dest_cr = s->current_picture.data[2] + mb_x * 8;
  1797. }else{
  1798. dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
  1799. dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  1800. dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
  1801. }
  1802. if (s->interlaced_dct) {
  1803. dct_linesize = s->linesize * 2;
  1804. dct_offset = s->linesize;
  1805. } else {
  1806. dct_linesize = s->linesize;
  1807. dct_offset = s->linesize * 8;
  1808. }
  1809. if (!s->mb_intra) {
  1810. /* motion handling */
  1811. /* decoding or more than one mb_type (MC was allready done otherwise) */
  1812. if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){
  1813. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  1814. op_pix = s->dsp.put_pixels_tab;
  1815. op_qpix= s->dsp.put_qpel_pixels_tab;
  1816. }else{
  1817. op_pix = s->dsp.put_no_rnd_pixels_tab;
  1818. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  1819. }
  1820. if (s->mv_dir & MV_DIR_FORWARD) {
  1821. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  1822. op_pix = s->dsp.avg_pixels_tab;
  1823. op_qpix= s->dsp.avg_qpel_pixels_tab;
  1824. }
  1825. if (s->mv_dir & MV_DIR_BACKWARD) {
  1826. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  1827. }
  1828. }
  1829. /* skip dequant / idct if we are really late ;) */
  1830. if(s->hurry_up>1) return;
  1831. /* add dct residue */
  1832. if(s->encoding || !( s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO
  1833. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1834. add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
  1835. add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1836. add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1837. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1838. if(!(s->flags&CODEC_FLAG_GRAY)){
  1839. add_dequant_dct(s, block[4], 4, dest_cb, s->uvlinesize);
  1840. add_dequant_dct(s, block[5], 5, dest_cr, s->uvlinesize);
  1841. }
  1842. } else if(s->codec_id != CODEC_ID_WMV2){
  1843. add_dct(s, block[0], 0, dest_y, dct_linesize);
  1844. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1845. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1846. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1847. if(!(s->flags&CODEC_FLAG_GRAY)){
  1848. add_dct(s, block[4], 4, dest_cb, s->uvlinesize);
  1849. add_dct(s, block[5], 5, dest_cr, s->uvlinesize);
  1850. }
  1851. }
  1852. #ifdef CONFIG_RISKY
  1853. else{
  1854. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1855. }
  1856. #endif
  1857. } else {
  1858. /* dct only in intra block */
  1859. if(s->encoding || !(s->mpeg2 || s->codec_id==CODEC_ID_MPEG1VIDEO)){
  1860. put_dct(s, block[0], 0, dest_y, dct_linesize);
  1861. put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  1862. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  1863. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  1864. if(!(s->flags&CODEC_FLAG_GRAY)){
  1865. put_dct(s, block[4], 4, dest_cb, s->uvlinesize);
  1866. put_dct(s, block[5], 5, dest_cr, s->uvlinesize);
  1867. }
  1868. }else{
  1869. s->idct_put(dest_y , dct_linesize, block[0]);
  1870. s->idct_put(dest_y + 8, dct_linesize, block[1]);
  1871. s->idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  1872. s->idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
  1873. if(!(s->flags&CODEC_FLAG_GRAY)){
  1874. s->idct_put(dest_cb, s->uvlinesize, block[4]);
  1875. s->idct_put(dest_cr, s->uvlinesize, block[5]);
  1876. }
  1877. }
  1878. }
  1879. }
  1880. }
  1881. static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
  1882. {
  1883. static const char tab[64]=
  1884. {3,2,2,1,1,1,1,1,
  1885. 1,1,1,1,1,1,1,1,
  1886. 1,1,1,1,1,1,1,1,
  1887. 0,0,0,0,0,0,0,0,
  1888. 0,0,0,0,0,0,0,0,
  1889. 0,0,0,0,0,0,0,0,
  1890. 0,0,0,0,0,0,0,0,
  1891. 0,0,0,0,0,0,0,0};
  1892. int score=0;
  1893. int run=0;
  1894. int i;
  1895. DCTELEM *block= s->block[n];
  1896. const int last_index= s->block_last_index[n];
  1897. int skip_dc;
  1898. if(threshold<0){
  1899. skip_dc=0;
  1900. threshold= -threshold;
  1901. }else
  1902. skip_dc=1;
  1903. /* are all which we could set to zero are allready zero? */
  1904. if(last_index<=skip_dc - 1) return;
  1905. for(i=0; i<=last_index; i++){
  1906. const int j = s->intra_scantable.permutated[i];
  1907. const int level = ABS(block[j]);
  1908. if(level==1){
  1909. if(skip_dc && i==0) continue;
  1910. score+= tab[run];
  1911. run=0;
  1912. }else if(level>1){
  1913. return;
  1914. }else{
  1915. run++;
  1916. }
  1917. }
  1918. if(score >= threshold) return;
  1919. for(i=skip_dc; i<=last_index; i++){
  1920. const int j = s->intra_scantable.permutated[i];
  1921. block[j]=0;
  1922. }
  1923. if(block[0]) s->block_last_index[n]= 0;
  1924. else s->block_last_index[n]= -1;
  1925. }
  1926. static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
  1927. {
  1928. int i;
  1929. const int maxlevel= s->max_qcoeff;
  1930. const int minlevel= s->min_qcoeff;
  1931. if(s->mb_intra){
  1932. i=1; //skip clipping of intra dc
  1933. }else
  1934. i=0;
  1935. for(;i<=last_index; i++){
  1936. const int j= s->intra_scantable.permutated[i];
  1937. int level = block[j];
  1938. if (level>maxlevel) level=maxlevel;
  1939. else if(level<minlevel) level=minlevel;
  1940. block[j]= level;
  1941. }
  1942. }
  1943. static inline void requantize_coeffs(MpegEncContext *s, DCTELEM block[64], int oldq, int newq, int n)
  1944. {
  1945. int i;
  1946. if(s->mb_intra){
  1947. i=1; //skip clipping of intra dc
  1948. //FIXME requantize, note (mpeg1/h263/h263p-aic dont need it,...)
  1949. }else
  1950. i=0;
  1951. for(;i<=s->block_last_index[n]; i++){
  1952. const int j = s->intra_scantable.permutated[i];
  1953. int level = block[j];
  1954. block[j]= ROUNDED_DIV(level*oldq, newq);
  1955. }
  1956. for(i=s->block_last_index[n]; i>=0; i--){
  1957. const int j = s->intra_scantable.permutated[i];
  1958. if(block[j]) break;
  1959. }
  1960. s->block_last_index[n]= i;
  1961. }
  1962. static inline void auto_requantize_coeffs(MpegEncContext *s, DCTELEM block[6][64])
  1963. {
  1964. int i,n, newq;
  1965. const int maxlevel= s->max_qcoeff;
  1966. const int minlevel= s->min_qcoeff;
  1967. int largest=0, smallest=0;
  1968. assert(s->adaptive_quant);
  1969. for(n=0; n<6; n++){
  1970. if(s->mb_intra){
  1971. i=1; //skip clipping of intra dc
  1972. //FIXME requantize, note (mpeg1/h263/h263p-aic dont need it,...)
  1973. }else
  1974. i=0;
  1975. for(;i<=s->block_last_index[n]; i++){
  1976. const int j = s->intra_scantable.permutated[i];
  1977. int level = block[n][j];
  1978. if(largest < level) largest = level;
  1979. if(smallest > level) smallest= level;
  1980. }
  1981. }
  1982. for(newq=s->qscale+1; newq<32; newq++){
  1983. if( ROUNDED_DIV(smallest*s->qscale, newq) >= minlevel
  1984. && ROUNDED_DIV(largest *s->qscale, newq) <= maxlevel)
  1985. break;
  1986. }
  1987. if(s->out_format==FMT_H263){
  1988. /* h263 like formats cannot change qscale by more than 2 easiely */
  1989. if(s->avctx->qmin + 2 < newq)
  1990. newq= s->avctx->qmin + 2;
  1991. }
  1992. for(n=0; n<6; n++){
  1993. requantize_coeffs(s, block[n], s->qscale, newq, n);
  1994. clip_coeffs(s, block[n], s->block_last_index[n]);
  1995. }
  1996. s->dquant+= newq - s->qscale;
  1997. s->qscale= newq;
  1998. }
  1999. #if 0
  2000. static int pix_vcmp16x8(UINT8 *s, int stride){ //FIXME move to dsputil & optimize
  2001. int score=0;
  2002. int x,y;
  2003. for(y=0; y<7; y++){
  2004. for(x=0; x<16; x+=4){
  2005. score+= ABS(s[x ] - s[x +stride]) + ABS(s[x+1] - s[x+1+stride])
  2006. +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
  2007. }
  2008. s+= stride;
  2009. }
  2010. return score;
  2011. }
  2012. static int pix_diff_vcmp16x8(UINT8 *s1, UINT8*s2, int stride){ //FIXME move to dsputil & optimize
  2013. int score=0;
  2014. int x,y;
  2015. for(y=0; y<7; y++){
  2016. for(x=0; x<16; x++){
  2017. score+= ABS(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
  2018. }
  2019. s1+= stride;
  2020. s2+= stride;
  2021. }
  2022. return score;
  2023. }
  2024. #else
  2025. #define SQ(a) ((a)*(a))
  2026. static int pix_vcmp16x8(UINT8 *s, int stride){ //FIXME move to dsputil & optimize
  2027. int score=0;
  2028. int x,y;
  2029. for(y=0; y<7; y++){
  2030. for(x=0; x<16; x+=4){
  2031. score+= SQ(s[x ] - s[x +stride]) + SQ(s[x+1] - s[x+1+stride])
  2032. +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
  2033. }
  2034. s+= stride;
  2035. }
  2036. return score;
  2037. }
  2038. static int pix_diff_vcmp16x8(UINT8 *s1, UINT8*s2, int stride){ //FIXME move to dsputil & optimize
  2039. int score=0;
  2040. int x,y;
  2041. for(y=0; y<7; y++){
  2042. for(x=0; x<16; x++){
  2043. score+= SQ(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
  2044. }
  2045. s1+= stride;
  2046. s2+= stride;
  2047. }
  2048. return score;
  2049. }
  2050. #endif
  2051. void ff_draw_horiz_band(MpegEncContext *s){
  2052. if ( s->avctx->draw_horiz_band
  2053. && (s->last_picture.data[0] || s->low_delay) ) {
  2054. UINT8 *src_ptr[3];
  2055. int y, h, offset;
  2056. y = s->mb_y * 16;
  2057. h = s->height - y;
  2058. if (h > 16)
  2059. h = 16;
  2060. if(s->pict_type==B_TYPE)
  2061. offset = 0;
  2062. else
  2063. offset = y * s->linesize;
  2064. if(s->pict_type==B_TYPE || s->low_delay){
  2065. src_ptr[0] = s->current_picture.data[0] + offset;
  2066. src_ptr[1] = s->current_picture.data[1] + (offset >> 2);
  2067. src_ptr[2] = s->current_picture.data[2] + (offset >> 2);
  2068. } else {
  2069. src_ptr[0] = s->last_picture.data[0] + offset;
  2070. src_ptr[1] = s->last_picture.data[1] + (offset >> 2);
  2071. src_ptr[2] = s->last_picture.data[2] + (offset >> 2);
  2072. }
  2073. emms_c();
  2074. s->avctx->draw_horiz_band(s->avctx, src_ptr, s->linesize,
  2075. y, s->width, h);
  2076. }
  2077. }
  2078. static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  2079. {
  2080. const int mb_x= s->mb_x;
  2081. const int mb_y= s->mb_y;
  2082. int i;
  2083. int skip_dct[6];
  2084. int dct_offset = s->linesize*8; //default for progressive frames
  2085. for(i=0; i<6; i++) skip_dct[i]=0;
  2086. if(s->adaptive_quant){
  2087. s->dquant= s->current_picture.qscale_table[mb_x + mb_y*s->mb_width] - s->qscale;
  2088. if(s->out_format==FMT_H263){
  2089. if (s->dquant> 2) s->dquant= 2;
  2090. else if(s->dquant<-2) s->dquant=-2;
  2091. }
  2092. if(s->codec_id==CODEC_ID_MPEG4){
  2093. if(!s->mb_intra){
  2094. assert(s->dquant==0 || s->mv_type!=MV_TYPE_8X8);
  2095. if(s->mv_dir&MV_DIRECT)
  2096. s->dquant=0;
  2097. }
  2098. }
  2099. s->qscale+= s->dquant;
  2100. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  2101. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  2102. }
  2103. if (s->mb_intra) {
  2104. UINT8 *ptr;
  2105. int wrap_y;
  2106. int emu=0;
  2107. wrap_y = s->linesize;
  2108. ptr = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  2109. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  2110. ff_emulated_edge_mc(s, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
  2111. ptr= s->edge_emu_buffer;
  2112. emu=1;
  2113. }
  2114. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  2115. int progressive_score, interlaced_score;
  2116. progressive_score= pix_vcmp16x8(ptr, wrap_y ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
  2117. interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y , wrap_y*2);
  2118. if(progressive_score > interlaced_score + 100){
  2119. s->interlaced_dct=1;
  2120. dct_offset= wrap_y;
  2121. wrap_y<<=1;
  2122. }else
  2123. s->interlaced_dct=0;
  2124. }
  2125. s->dsp.get_pixels(s->block[0], ptr , wrap_y);
  2126. s->dsp.get_pixels(s->block[1], ptr + 8, wrap_y);
  2127. s->dsp.get_pixels(s->block[2], ptr + dct_offset , wrap_y);
  2128. s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
  2129. if(s->flags&CODEC_FLAG_GRAY){
  2130. skip_dct[4]= 1;
  2131. skip_dct[5]= 1;
  2132. }else{
  2133. int wrap_c = s->uvlinesize;
  2134. ptr = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2135. if(emu){
  2136. ff_emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2137. ptr= s->edge_emu_buffer;
  2138. }
  2139. s->dsp.get_pixels(s->block[4], ptr, wrap_c);
  2140. ptr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2141. if(emu){
  2142. ff_emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2143. ptr= s->edge_emu_buffer;
  2144. }
  2145. s->dsp.get_pixels(s->block[5], ptr, wrap_c);
  2146. }
  2147. }else{
  2148. op_pixels_func (*op_pix)[4];
  2149. qpel_mc_func (*op_qpix)[16];
  2150. UINT8 *dest_y, *dest_cb, *dest_cr;
  2151. UINT8 *ptr_y, *ptr_cb, *ptr_cr;
  2152. int wrap_y, wrap_c;
  2153. int emu=0;
  2154. dest_y = s->current_picture.data[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
  2155. dest_cb = s->current_picture.data[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  2156. dest_cr = s->current_picture.data[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  2157. wrap_y = s->linesize;
  2158. wrap_c = s->uvlinesize;
  2159. ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  2160. ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2161. ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2162. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  2163. op_pix = s->dsp.put_pixels_tab;
  2164. op_qpix= s->dsp.put_qpel_pixels_tab;
  2165. }else{
  2166. op_pix = s->dsp.put_no_rnd_pixels_tab;
  2167. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  2168. }
  2169. if (s->mv_dir & MV_DIR_FORWARD) {
  2170. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  2171. op_pix = s->dsp.avg_pixels_tab;
  2172. op_qpix= s->dsp.avg_qpel_pixels_tab;
  2173. }
  2174. if (s->mv_dir & MV_DIR_BACKWARD) {
  2175. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  2176. }
  2177. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  2178. ff_emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
  2179. ptr_y= s->edge_emu_buffer;
  2180. emu=1;
  2181. }
  2182. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  2183. int progressive_score, interlaced_score;
  2184. progressive_score= pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y )
  2185. + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y );
  2186. interlaced_score = pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y*2)
  2187. + pix_diff_vcmp16x8(ptr_y + wrap_y , dest_y + wrap_y , wrap_y*2);
  2188. if(progressive_score > interlaced_score + 600){
  2189. s->interlaced_dct=1;
  2190. dct_offset= wrap_y;
  2191. wrap_y<<=1;
  2192. }else
  2193. s->interlaced_dct=0;
  2194. }
  2195. s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
  2196. s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  2197. s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
  2198. s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
  2199. if(s->flags&CODEC_FLAG_GRAY){
  2200. skip_dct[4]= 1;
  2201. skip_dct[5]= 1;
  2202. }else{
  2203. if(emu){
  2204. ff_emulated_edge_mc(s, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2205. ptr_cb= s->edge_emu_buffer;
  2206. }
  2207. s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  2208. if(emu){
  2209. ff_emulated_edge_mc(s, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2210. ptr_cr= s->edge_emu_buffer;
  2211. }
  2212. s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  2213. }
  2214. /* pre quantization */
  2215. if(s->current_picture.mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
  2216. //FIXME optimize
  2217. if(s->dsp.pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
  2218. if(s->dsp.pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
  2219. if(s->dsp.pix_abs8x8(ptr_y +dct_offset , dest_y +dct_offset , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
  2220. if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
  2221. if(s->dsp.pix_abs8x8(ptr_cb , dest_cb , wrap_c) < 20*s->qscale) skip_dct[4]= 1;
  2222. if(s->dsp.pix_abs8x8(ptr_cr , dest_cr , wrap_c) < 20*s->qscale) skip_dct[5]= 1;
  2223. #if 0
  2224. {
  2225. static int stat[7];
  2226. int num=0;
  2227. for(i=0; i<6; i++)
  2228. if(skip_dct[i]) num++;
  2229. stat[num]++;
  2230. if(s->mb_x==0 && s->mb_y==0){
  2231. for(i=0; i<7; i++){
  2232. printf("%6d %1d\n", stat[i], i);
  2233. }
  2234. }
  2235. }
  2236. #endif
  2237. }
  2238. }
  2239. #if 0
  2240. {
  2241. float adap_parm;
  2242. adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) /
  2243. ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
  2244. printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
  2245. (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P',
  2246. s->qscale, adap_parm, s->qscale*adap_parm,
  2247. s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var);
  2248. }
  2249. #endif
  2250. /* DCT & quantize */
  2251. if(s->out_format==FMT_MJPEG){
  2252. for(i=0;i<6;i++) {
  2253. int overflow;
  2254. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
  2255. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  2256. }
  2257. }else{
  2258. for(i=0;i<6;i++) {
  2259. if(!skip_dct[i]){
  2260. int overflow;
  2261. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  2262. // FIXME we could decide to change to quantizer instead of clipping
  2263. // JS: I don't think that would be a good idea it could lower quality instead
  2264. // of improve it. Just INTRADC clipping deserves changes in quantizer
  2265. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  2266. }else
  2267. s->block_last_index[i]= -1;
  2268. }
  2269. if(s->luma_elim_threshold && !s->mb_intra)
  2270. for(i=0; i<4; i++)
  2271. dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
  2272. if(s->chroma_elim_threshold && !s->mb_intra)
  2273. for(i=4; i<6; i++)
  2274. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
  2275. }
  2276. if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
  2277. s->block_last_index[4]=
  2278. s->block_last_index[5]= 0;
  2279. s->block[4][0]=
  2280. s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
  2281. }
  2282. #ifdef CONFIG_ENCODERS
  2283. /* huffman encode */
  2284. switch(s->codec_id){ //FIXME funct ptr could be slightly faster
  2285. case CODEC_ID_MPEG1VIDEO:
  2286. mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
  2287. #ifdef CONFIG_RISKY
  2288. case CODEC_ID_MPEG4:
  2289. mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  2290. case CODEC_ID_MSMPEG4V2:
  2291. case CODEC_ID_MSMPEG4V3:
  2292. case CODEC_ID_WMV1:
  2293. msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  2294. case CODEC_ID_WMV2:
  2295. ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
  2296. case CODEC_ID_H263:
  2297. case CODEC_ID_H263P:
  2298. case CODEC_ID_RV10:
  2299. h263_encode_mb(s, s->block, motion_x, motion_y); break;
  2300. #endif
  2301. case CODEC_ID_MJPEG:
  2302. mjpeg_encode_mb(s, s->block); break;
  2303. default:
  2304. assert(0);
  2305. }
  2306. #endif
  2307. }
  2308. /**
  2309. * combines the (truncated) bitstream to a complete frame
  2310. * @returns -1 if no complete frame could be created
  2311. */
  2312. int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size){
  2313. ParseContext *pc= &s->parse_context;
  2314. pc->last_index= pc->index;
  2315. if(next==-1){
  2316. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  2317. memcpy(&pc->buffer[pc->index], *buf, *buf_size);
  2318. pc->index += *buf_size;
  2319. return -1;
  2320. }
  2321. if(pc->index){
  2322. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  2323. memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
  2324. pc->index = 0;
  2325. *buf= pc->buffer;
  2326. *buf_size= pc->last_index + next;
  2327. }
  2328. return 0;
  2329. }
  2330. void ff_copy_bits(PutBitContext *pb, UINT8 *src, int length)
  2331. {
  2332. int bytes= length>>4;
  2333. int bits= length&15;
  2334. int i;
  2335. if(length==0) return;
  2336. for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
  2337. put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
  2338. }
  2339. static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  2340. int i;
  2341. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  2342. /* mpeg1 */
  2343. d->mb_incr= s->mb_incr;
  2344. for(i=0; i<3; i++)
  2345. d->last_dc[i]= s->last_dc[i];
  2346. /* statistics */
  2347. d->mv_bits= s->mv_bits;
  2348. d->i_tex_bits= s->i_tex_bits;
  2349. d->p_tex_bits= s->p_tex_bits;
  2350. d->i_count= s->i_count;
  2351. d->f_count= s->f_count;
  2352. d->b_count= s->b_count;
  2353. d->skip_count= s->skip_count;
  2354. d->misc_bits= s->misc_bits;
  2355. d->last_bits= 0;
  2356. d->mb_skiped= s->mb_skiped;
  2357. d->qscale= s->qscale;
  2358. }
  2359. static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  2360. int i;
  2361. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  2362. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  2363. /* mpeg1 */
  2364. d->mb_incr= s->mb_incr;
  2365. for(i=0; i<3; i++)
  2366. d->last_dc[i]= s->last_dc[i];
  2367. /* statistics */
  2368. d->mv_bits= s->mv_bits;
  2369. d->i_tex_bits= s->i_tex_bits;
  2370. d->p_tex_bits= s->p_tex_bits;
  2371. d->i_count= s->i_count;
  2372. d->f_count= s->f_count;
  2373. d->b_count= s->b_count;
  2374. d->skip_count= s->skip_count;
  2375. d->misc_bits= s->misc_bits;
  2376. d->mb_intra= s->mb_intra;
  2377. d->mb_skiped= s->mb_skiped;
  2378. d->mv_type= s->mv_type;
  2379. d->mv_dir= s->mv_dir;
  2380. d->pb= s->pb;
  2381. if(s->data_partitioning){
  2382. d->pb2= s->pb2;
  2383. d->tex_pb= s->tex_pb;
  2384. }
  2385. d->block= s->block;
  2386. for(i=0; i<6; i++)
  2387. d->block_last_index[i]= s->block_last_index[i];
  2388. d->interlaced_dct= s->interlaced_dct;
  2389. d->qscale= s->qscale;
  2390. }
  2391. static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
  2392. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  2393. int *dmin, int *next_block, int motion_x, int motion_y)
  2394. {
  2395. int bits_count;
  2396. copy_context_before_encode(s, backup, type);
  2397. s->block= s->blocks[*next_block];
  2398. s->pb= pb[*next_block];
  2399. if(s->data_partitioning){
  2400. s->pb2 = pb2 [*next_block];
  2401. s->tex_pb= tex_pb[*next_block];
  2402. }
  2403. encode_mb(s, motion_x, motion_y);
  2404. bits_count= get_bit_count(&s->pb);
  2405. if(s->data_partitioning){
  2406. bits_count+= get_bit_count(&s->pb2);
  2407. bits_count+= get_bit_count(&s->tex_pb);
  2408. }
  2409. if(bits_count<*dmin){
  2410. *dmin= bits_count;
  2411. *next_block^=1;
  2412. copy_context_after_encode(best, s, type);
  2413. }
  2414. }
  2415. static inline int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
  2416. uint32_t *sq = squareTbl + 256;
  2417. int acc=0;
  2418. int x,y;
  2419. if(w==16 && h==16)
  2420. return s->dsp.sse[0](NULL, src1, src2, stride);
  2421. else if(w==8 && h==8)
  2422. return s->dsp.sse[1](NULL, src1, src2, stride);
  2423. for(y=0; y<h; y++){
  2424. for(x=0; x<w; x++){
  2425. acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
  2426. }
  2427. }
  2428. assert(acc>=0);
  2429. return acc;
  2430. }
  2431. static void encode_picture(MpegEncContext *s, int picture_number)
  2432. {
  2433. int mb_x, mb_y, pdif = 0;
  2434. int i;
  2435. int bits;
  2436. MpegEncContext best_s, backup_s;
  2437. UINT8 bit_buf[2][3000];
  2438. UINT8 bit_buf2[2][3000];
  2439. UINT8 bit_buf_tex[2][3000];
  2440. PutBitContext pb[2], pb2[2], tex_pb[2];
  2441. for(i=0; i<2; i++){
  2442. init_put_bits(&pb [i], bit_buf [i], 3000, NULL, NULL);
  2443. init_put_bits(&pb2 [i], bit_buf2 [i], 3000, NULL, NULL);
  2444. init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
  2445. }
  2446. s->picture_number = picture_number;
  2447. s->block_wrap[0]=
  2448. s->block_wrap[1]=
  2449. s->block_wrap[2]=
  2450. s->block_wrap[3]= s->mb_width*2 + 2;
  2451. s->block_wrap[4]=
  2452. s->block_wrap[5]= s->mb_width + 2;
  2453. /* Reset the average MB variance */
  2454. s->current_picture.mb_var_sum = 0;
  2455. s->current_picture.mc_mb_var_sum = 0;
  2456. #ifdef CONFIG_RISKY
  2457. /* we need to initialize some time vars before we can encode b-frames */
  2458. if (s->h263_pred && !s->h263_msmpeg4)
  2459. ff_set_mpeg4_time(s, s->picture_number);
  2460. #endif
  2461. s->scene_change_score=0;
  2462. s->qscale= (int)(s->frame_qscale + 0.5); //FIXME qscale / ... stuff for ME ratedistoration
  2463. if(s->msmpeg4_version){
  2464. if(s->pict_type==I_TYPE)
  2465. s->no_rounding=1;
  2466. else if(s->flipflop_rounding)
  2467. s->no_rounding ^= 1;
  2468. }else if(s->out_format == FMT_H263){
  2469. if(s->pict_type==I_TYPE)
  2470. s->no_rounding=0;
  2471. else if(s->pict_type!=B_TYPE)
  2472. s->no_rounding ^= 1;
  2473. }
  2474. /* Estimate motion for every MB */
  2475. s->mb_intra=0; //for the rate distoration & bit compare functions
  2476. if(s->pict_type != I_TYPE){
  2477. if(s->pict_type != B_TYPE){
  2478. if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
  2479. s->me.pre_pass=1;
  2480. s->me.dia_size= s->avctx->pre_dia_size;
  2481. for(mb_y=s->mb_height-1; mb_y >=0 ; mb_y--) {
  2482. for(mb_x=s->mb_width-1; mb_x >=0 ; mb_x--) {
  2483. s->mb_x = mb_x;
  2484. s->mb_y = mb_y;
  2485. ff_pre_estimate_p_frame_motion(s, mb_x, mb_y);
  2486. }
  2487. }
  2488. s->me.pre_pass=0;
  2489. }
  2490. }
  2491. s->me.dia_size= s->avctx->dia_size;
  2492. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  2493. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  2494. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  2495. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  2496. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  2497. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2498. s->mb_x = mb_x;
  2499. s->mb_y = mb_y;
  2500. s->block_index[0]+=2;
  2501. s->block_index[1]+=2;
  2502. s->block_index[2]+=2;
  2503. s->block_index[3]+=2;
  2504. /* compute motion vector & mb_type and store in context */
  2505. if(s->pict_type==B_TYPE)
  2506. ff_estimate_b_frame_motion(s, mb_x, mb_y);
  2507. else
  2508. ff_estimate_p_frame_motion(s, mb_x, mb_y);
  2509. }
  2510. }
  2511. }else /* if(s->pict_type == I_TYPE) */{
  2512. /* I-Frame */
  2513. //FIXME do we need to zero them?
  2514. memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
  2515. memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
  2516. memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
  2517. if(!s->fixed_qscale){
  2518. /* finding spatial complexity for I-frame rate control */
  2519. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  2520. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2521. int xx = mb_x * 16;
  2522. int yy = mb_y * 16;
  2523. uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  2524. int varc;
  2525. int sum = s->dsp.pix_sum(pix, s->linesize);
  2526. varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
  2527. s->current_picture.mb_var [s->mb_width * mb_y + mb_x] = varc;
  2528. s->current_picture.mb_mean[s->mb_width * mb_y + mb_x] = (sum+128)>>8;
  2529. s->current_picture.mb_var_sum += varc;
  2530. }
  2531. }
  2532. }
  2533. }
  2534. emms_c();
  2535. if(s->scene_change_score > 0 && s->pict_type == P_TYPE){
  2536. s->pict_type= I_TYPE;
  2537. memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
  2538. //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
  2539. }
  2540. if(s->pict_type==P_TYPE || s->pict_type==S_TYPE)
  2541. s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
  2542. ff_fix_long_p_mvs(s);
  2543. if(s->pict_type==B_TYPE){
  2544. s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
  2545. s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
  2546. ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
  2547. ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
  2548. ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
  2549. ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
  2550. }
  2551. if (s->fixed_qscale)
  2552. s->frame_qscale = s->current_picture.quality;
  2553. else
  2554. s->frame_qscale = ff_rate_estimate_qscale(s);
  2555. if(s->adaptive_quant){
  2556. #ifdef CONFIG_RISKY
  2557. switch(s->codec_id){
  2558. case CODEC_ID_MPEG4:
  2559. ff_clean_mpeg4_qscales(s);
  2560. break;
  2561. case CODEC_ID_H263:
  2562. case CODEC_ID_H263P:
  2563. ff_clean_h263_qscales(s);
  2564. break;
  2565. }
  2566. #endif
  2567. s->qscale= s->current_picture.qscale_table[0];
  2568. }else
  2569. s->qscale= (int)(s->frame_qscale + 0.5);
  2570. if (s->out_format == FMT_MJPEG) {
  2571. /* for mjpeg, we do include qscale in the matrix */
  2572. s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
  2573. for(i=1;i<64;i++){
  2574. int j= s->idct_permutation[i];
  2575. s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
  2576. }
  2577. convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
  2578. s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias, 8, 8);
  2579. }
  2580. //FIXME var duplication
  2581. s->current_picture.key_frame= s->pict_type == I_TYPE;
  2582. s->current_picture.pict_type= s->pict_type;
  2583. if(s->current_picture.key_frame)
  2584. s->picture_in_gop_number=0;
  2585. s->last_bits= get_bit_count(&s->pb);
  2586. switch(s->out_format) {
  2587. case FMT_MJPEG:
  2588. mjpeg_picture_header(s);
  2589. break;
  2590. #ifdef CONFIG_RISKY
  2591. case FMT_H263:
  2592. if (s->codec_id == CODEC_ID_WMV2)
  2593. ff_wmv2_encode_picture_header(s, picture_number);
  2594. else if (s->h263_msmpeg4)
  2595. msmpeg4_encode_picture_header(s, picture_number);
  2596. else if (s->h263_pred)
  2597. mpeg4_encode_picture_header(s, picture_number);
  2598. else if (s->h263_rv10)
  2599. rv10_encode_picture_header(s, picture_number);
  2600. else
  2601. h263_encode_picture_header(s, picture_number);
  2602. break;
  2603. #endif
  2604. case FMT_MPEG1:
  2605. mpeg1_encode_picture_header(s, picture_number);
  2606. break;
  2607. }
  2608. bits= get_bit_count(&s->pb);
  2609. s->header_bits= bits - s->last_bits;
  2610. s->last_bits= bits;
  2611. s->mv_bits=0;
  2612. s->misc_bits=0;
  2613. s->i_tex_bits=0;
  2614. s->p_tex_bits=0;
  2615. s->i_count=0;
  2616. s->f_count=0;
  2617. s->b_count=0;
  2618. s->skip_count=0;
  2619. for(i=0; i<3; i++){
  2620. /* init last dc values */
  2621. /* note: quant matrix value (8) is implied here */
  2622. s->last_dc[i] = 128;
  2623. s->current_picture.error[i] = 0;
  2624. }
  2625. s->mb_incr = 1;
  2626. s->last_mv[0][0][0] = 0;
  2627. s->last_mv[0][0][1] = 0;
  2628. #ifdef CONFIG_RISKY
  2629. if (s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P)
  2630. s->gob_index = ff_h263_get_gob_height(s);
  2631. if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
  2632. ff_mpeg4_init_partitions(s);
  2633. #endif
  2634. s->resync_mb_x=0;
  2635. s->resync_mb_y=0;
  2636. s->first_slice_line = 1;
  2637. s->ptr_lastgob = s->pb.buf;
  2638. s->ptr_last_mb_line = s->pb.buf;
  2639. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  2640. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  2641. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  2642. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  2643. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  2644. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  2645. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  2646. s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
  2647. s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
  2648. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2649. int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
  2650. const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
  2651. // int d;
  2652. int dmin=10000000;
  2653. s->mb_x = mb_x;
  2654. s->mb_y = mb_y;
  2655. s->block_index[0]+=2;
  2656. s->block_index[1]+=2;
  2657. s->block_index[2]+=2;
  2658. s->block_index[3]+=2;
  2659. s->block_index[4]++;
  2660. s->block_index[5]++;
  2661. /* write gob / video packet header */
  2662. #ifdef CONFIG_RISKY
  2663. if(s->rtp_mode){
  2664. int current_packet_size, is_gob_start;
  2665. current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
  2666. is_gob_start=0;
  2667. if(s->codec_id==CODEC_ID_MPEG4){
  2668. if(current_packet_size + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size
  2669. && s->mb_y + s->mb_x>0){
  2670. if(s->partitioned_frame){
  2671. ff_mpeg4_merge_partitions(s);
  2672. ff_mpeg4_init_partitions(s);
  2673. }
  2674. ff_mpeg4_encode_video_packet_header(s);
  2675. if(s->flags&CODEC_FLAG_PASS1){
  2676. int bits= get_bit_count(&s->pb);
  2677. s->misc_bits+= bits - s->last_bits;
  2678. s->last_bits= bits;
  2679. }
  2680. ff_mpeg4_clean_buffers(s);
  2681. is_gob_start=1;
  2682. }
  2683. }else{
  2684. if(current_packet_size + s->mb_line_avgsize*s->gob_index >= s->rtp_payload_size
  2685. && s->mb_x==0 && s->mb_y>0 && s->mb_y%s->gob_index==0){
  2686. h263_encode_gob_header(s, mb_y);
  2687. is_gob_start=1;
  2688. }
  2689. }
  2690. if(is_gob_start){
  2691. s->ptr_lastgob = pbBufPtr(&s->pb);
  2692. s->first_slice_line=1;
  2693. s->resync_mb_x=mb_x;
  2694. s->resync_mb_y=mb_y;
  2695. }
  2696. }
  2697. #endif
  2698. if( (s->resync_mb_x == s->mb_x)
  2699. && s->resync_mb_y+1 == s->mb_y){
  2700. s->first_slice_line=0;
  2701. }
  2702. if(mb_type & (mb_type-1)){ // more than 1 MB type possible
  2703. int next_block=0;
  2704. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  2705. copy_context_before_encode(&backup_s, s, -1);
  2706. backup_s.pb= s->pb;
  2707. best_s.data_partitioning= s->data_partitioning;
  2708. best_s.partitioned_frame= s->partitioned_frame;
  2709. if(s->data_partitioning){
  2710. backup_s.pb2= s->pb2;
  2711. backup_s.tex_pb= s->tex_pb;
  2712. }
  2713. if(mb_type&MB_TYPE_INTER){
  2714. s->mv_dir = MV_DIR_FORWARD;
  2715. s->mv_type = MV_TYPE_16X16;
  2716. s->mb_intra= 0;
  2717. s->mv[0][0][0] = s->p_mv_table[xy][0];
  2718. s->mv[0][0][1] = s->p_mv_table[xy][1];
  2719. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb,
  2720. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  2721. }
  2722. if(mb_type&MB_TYPE_INTER4V){
  2723. s->mv_dir = MV_DIR_FORWARD;
  2724. s->mv_type = MV_TYPE_8X8;
  2725. s->mb_intra= 0;
  2726. for(i=0; i<4; i++){
  2727. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  2728. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  2729. }
  2730. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb,
  2731. &dmin, &next_block, 0, 0);
  2732. }
  2733. if(mb_type&MB_TYPE_FORWARD){
  2734. s->mv_dir = MV_DIR_FORWARD;
  2735. s->mv_type = MV_TYPE_16X16;
  2736. s->mb_intra= 0;
  2737. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  2738. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  2739. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb,
  2740. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  2741. }
  2742. if(mb_type&MB_TYPE_BACKWARD){
  2743. s->mv_dir = MV_DIR_BACKWARD;
  2744. s->mv_type = MV_TYPE_16X16;
  2745. s->mb_intra= 0;
  2746. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  2747. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  2748. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  2749. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  2750. }
  2751. if(mb_type&MB_TYPE_BIDIR){
  2752. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2753. s->mv_type = MV_TYPE_16X16;
  2754. s->mb_intra= 0;
  2755. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  2756. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  2757. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  2758. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  2759. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb,
  2760. &dmin, &next_block, 0, 0);
  2761. }
  2762. if(mb_type&MB_TYPE_DIRECT){
  2763. int mx= s->b_direct_mv_table[xy][0];
  2764. int my= s->b_direct_mv_table[xy][1];
  2765. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2766. s->mb_intra= 0;
  2767. #ifdef CONFIG_RISKY
  2768. ff_mpeg4_set_direct_mv(s, mx, my);
  2769. #endif
  2770. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb,
  2771. &dmin, &next_block, mx, my);
  2772. }
  2773. if(mb_type&MB_TYPE_INTRA){
  2774. s->mv_dir = MV_DIR_FORWARD;
  2775. s->mv_type = MV_TYPE_16X16;
  2776. s->mb_intra= 1;
  2777. s->mv[0][0][0] = 0;
  2778. s->mv[0][0][1] = 0;
  2779. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb,
  2780. &dmin, &next_block, 0, 0);
  2781. /* force cleaning of ac/dc pred stuff if needed ... */
  2782. if(s->h263_pred || s->h263_aic)
  2783. s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
  2784. }
  2785. copy_context_after_encode(s, &best_s, -1);
  2786. pb_bits_count= get_bit_count(&s->pb);
  2787. flush_put_bits(&s->pb);
  2788. ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  2789. s->pb= backup_s.pb;
  2790. if(s->data_partitioning){
  2791. pb2_bits_count= get_bit_count(&s->pb2);
  2792. flush_put_bits(&s->pb2);
  2793. ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  2794. s->pb2= backup_s.pb2;
  2795. tex_pb_bits_count= get_bit_count(&s->tex_pb);
  2796. flush_put_bits(&s->tex_pb);
  2797. ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  2798. s->tex_pb= backup_s.tex_pb;
  2799. }
  2800. s->last_bits= get_bit_count(&s->pb);
  2801. } else {
  2802. int motion_x, motion_y;
  2803. int intra_score;
  2804. int inter_score= s->current_picture.mb_cmp_score[mb_x + mb_y*s->mb_width];
  2805. if(!(s->flags&CODEC_FLAG_HQ) && s->pict_type==P_TYPE){
  2806. /* get luma score */
  2807. if((s->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
  2808. intra_score= (s->current_picture.mb_var[mb_x + mb_y*s->mb_width]<<8) - 500; //FIXME dont scale it down so we dont have to fix it
  2809. }else{
  2810. uint8_t *dest_y;
  2811. int mean= s->current_picture.mb_mean[mb_x + mb_y*s->mb_width]; //FIXME
  2812. mean*= 0x01010101;
  2813. dest_y = s->new_picture.data[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
  2814. for(i=0; i<16; i++){
  2815. *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 0]) = mean;
  2816. *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 4]) = mean;
  2817. *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 8]) = mean;
  2818. *(uint32_t*)(&s->me.scratchpad[i*s->linesize+12]) = mean;
  2819. }
  2820. s->mb_intra=1;
  2821. intra_score= s->dsp.mb_cmp[0](s, s->me.scratchpad, dest_y, s->linesize);
  2822. /* printf("intra:%7d inter:%7d var:%7d mc_var.%7d\n", intra_score>>8, inter_score>>8,
  2823. s->current_picture.mb_var[mb_x + mb_y*s->mb_width],
  2824. s->current_picture.mc_mb_var[mb_x + mb_y*s->mb_width]);*/
  2825. }
  2826. /* get chroma score */
  2827. if(s->avctx->mb_cmp&FF_CMP_CHROMA){
  2828. int i;
  2829. s->mb_intra=1;
  2830. for(i=1; i<3; i++){
  2831. uint8_t *dest_c;
  2832. int mean;
  2833. if(s->out_format == FMT_H263){
  2834. mean= (s->dc_val[i][mb_x + (mb_y+1)*(s->mb_width+2)] + 4)>>3; //FIXME not exact but simple ;)
  2835. }else{
  2836. mean= (s->last_dc[i] + 4)>>3;
  2837. }
  2838. dest_c = s->new_picture.data[i] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  2839. mean*= 0x01010101;
  2840. for(i=0; i<8; i++){
  2841. *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 0]) = mean;
  2842. *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 4]) = mean;
  2843. }
  2844. intra_score+= s->dsp.mb_cmp[1](s, s->me.scratchpad, dest_c, s->uvlinesize);
  2845. }
  2846. }
  2847. /* bias */
  2848. switch(s->avctx->mb_cmp&0xFF){
  2849. default:
  2850. case FF_CMP_SAD:
  2851. intra_score+= 32*s->qscale;
  2852. break;
  2853. case FF_CMP_SSE:
  2854. intra_score+= 24*s->qscale*s->qscale;
  2855. break;
  2856. case FF_CMP_SATD:
  2857. intra_score+= 96*s->qscale;
  2858. break;
  2859. case FF_CMP_DCT:
  2860. intra_score+= 48*s->qscale;
  2861. break;
  2862. case FF_CMP_BIT:
  2863. intra_score+= 16;
  2864. break;
  2865. case FF_CMP_PSNR:
  2866. case FF_CMP_RD:
  2867. intra_score+= (s->qscale*s->qscale*109*8 + 64)>>7;
  2868. break;
  2869. }
  2870. if(intra_score < inter_score)
  2871. mb_type= MB_TYPE_INTRA;
  2872. }
  2873. s->mv_type=MV_TYPE_16X16;
  2874. // only one MB-Type possible
  2875. switch(mb_type){
  2876. case MB_TYPE_INTRA:
  2877. s->mv_dir = MV_DIR_FORWARD;
  2878. s->mb_intra= 1;
  2879. motion_x= s->mv[0][0][0] = 0;
  2880. motion_y= s->mv[0][0][1] = 0;
  2881. break;
  2882. case MB_TYPE_INTER:
  2883. s->mv_dir = MV_DIR_FORWARD;
  2884. s->mb_intra= 0;
  2885. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  2886. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  2887. break;
  2888. case MB_TYPE_INTER4V:
  2889. s->mv_dir = MV_DIR_FORWARD;
  2890. s->mv_type = MV_TYPE_8X8;
  2891. s->mb_intra= 0;
  2892. for(i=0; i<4; i++){
  2893. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  2894. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  2895. }
  2896. motion_x= motion_y= 0;
  2897. break;
  2898. case MB_TYPE_DIRECT:
  2899. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  2900. s->mb_intra= 0;
  2901. motion_x=s->b_direct_mv_table[xy][0];
  2902. motion_y=s->b_direct_mv_table[xy][1];
  2903. #ifdef CONFIG_RISKY
  2904. ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
  2905. #endif
  2906. break;
  2907. case MB_TYPE_BIDIR:
  2908. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  2909. s->mb_intra= 0;
  2910. motion_x=0;
  2911. motion_y=0;
  2912. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  2913. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  2914. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  2915. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  2916. break;
  2917. case MB_TYPE_BACKWARD:
  2918. s->mv_dir = MV_DIR_BACKWARD;
  2919. s->mb_intra= 0;
  2920. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  2921. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  2922. break;
  2923. case MB_TYPE_FORWARD:
  2924. s->mv_dir = MV_DIR_FORWARD;
  2925. s->mb_intra= 0;
  2926. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  2927. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  2928. // printf(" %d %d ", motion_x, motion_y);
  2929. break;
  2930. default:
  2931. motion_x=motion_y=0; //gcc warning fix
  2932. printf("illegal MB type\n");
  2933. }
  2934. encode_mb(s, motion_x, motion_y);
  2935. }
  2936. /* clean the MV table in IPS frames for direct mode in B frames */
  2937. if(s->mb_intra /* && I,P,S_TYPE */){
  2938. s->p_mv_table[xy][0]=0;
  2939. s->p_mv_table[xy][1]=0;
  2940. }
  2941. MPV_decode_mb(s, s->block);
  2942. if(s->flags&CODEC_FLAG_PSNR){
  2943. int w= 16;
  2944. int h= 16;
  2945. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  2946. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  2947. s->current_picture.error[0] += sse(
  2948. s,
  2949. s->new_picture .data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
  2950. s->current_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
  2951. w, h, s->linesize);
  2952. s->current_picture.error[1] += sse(
  2953. s,
  2954. s->new_picture .data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  2955. s->current_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  2956. w>>1, h>>1, s->uvlinesize);
  2957. s->current_picture.error[2] += sse(
  2958. s,
  2959. s->new_picture .data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  2960. s->current_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  2961. w>>1, h>>1, s->uvlinesize);
  2962. }
  2963. //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb));
  2964. }
  2965. /* Obtain average mb_row size for RTP */
  2966. if (s->rtp_mode) {
  2967. if (mb_y==0)
  2968. s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line;
  2969. else {
  2970. s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1;
  2971. }
  2972. s->ptr_last_mb_line = pbBufPtr(&s->pb);
  2973. }
  2974. }
  2975. emms_c();
  2976. #ifdef CONFIG_RISKY
  2977. if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
  2978. ff_mpeg4_merge_partitions(s);
  2979. if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
  2980. msmpeg4_encode_ext_header(s);
  2981. if(s->codec_id==CODEC_ID_MPEG4)
  2982. ff_mpeg4_stuffing(&s->pb);
  2983. #endif
  2984. //if (s->gob_number)
  2985. // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
  2986. /* Send the last GOB if RTP */
  2987. if (s->rtp_mode) {
  2988. flush_put_bits(&s->pb);
  2989. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  2990. /* Call the RTP callback to send the last GOB */
  2991. if (s->rtp_callback)
  2992. s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
  2993. s->ptr_lastgob = pbBufPtr(&s->pb);
  2994. //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
  2995. }
  2996. }
  2997. static int dct_quantize_trellis_c(MpegEncContext *s,
  2998. DCTELEM *block, int n,
  2999. int qscale, int *overflow){
  3000. const int *qmat;
  3001. const UINT8 *scantable= s->intra_scantable.scantable;
  3002. int max=0;
  3003. unsigned int threshold1, threshold2;
  3004. int bias=0;
  3005. int run_tab[65];
  3006. int level_tab[65];
  3007. int score_tab[65];
  3008. int last_run=0;
  3009. int last_level=0;
  3010. int last_score= 0;
  3011. int last_i= 0;
  3012. int coeff[3][64];
  3013. int coeff_count[64];
  3014. int lambda, qmul, qadd, start_i, last_non_zero, i;
  3015. const int esc_length= s->ac_esc_length;
  3016. uint8_t * length;
  3017. uint8_t * last_length;
  3018. int score_limit=0;
  3019. int left_limit= 0;
  3020. s->fdct (block);
  3021. qmul= qscale*16;
  3022. qadd= ((qscale-1)|1)*8;
  3023. if (s->mb_intra) {
  3024. int q;
  3025. if (!s->h263_aic) {
  3026. if (n < 4)
  3027. q = s->y_dc_scale;
  3028. else
  3029. q = s->c_dc_scale;
  3030. q = q << 3;
  3031. } else{
  3032. /* For AIC we skip quant/dequant of INTRADC */
  3033. q = 1 << 3;
  3034. qadd=0;
  3035. }
  3036. /* note: block[0] is assumed to be positive */
  3037. block[0] = (block[0] + (q >> 1)) / q;
  3038. start_i = 1;
  3039. last_non_zero = 0;
  3040. qmat = s->q_intra_matrix[qscale];
  3041. if(s->mpeg_quant || s->codec_id== CODEC_ID_MPEG1VIDEO)
  3042. bias= 1<<(QMAT_SHIFT-1);
  3043. length = s->intra_ac_vlc_length;
  3044. last_length= s->intra_ac_vlc_last_length;
  3045. } else {
  3046. start_i = 0;
  3047. last_non_zero = -1;
  3048. qmat = s->q_inter_matrix[qscale];
  3049. length = s->inter_ac_vlc_length;
  3050. last_length= s->inter_ac_vlc_last_length;
  3051. }
  3052. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  3053. threshold2= (threshold1<<1);
  3054. for(i=start_i; i<64; i++) {
  3055. const int j = scantable[i];
  3056. const int k= i-start_i;
  3057. int level = block[j];
  3058. level = level * qmat[j];
  3059. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  3060. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  3061. if(((unsigned)(level+threshold1))>threshold2){
  3062. if(level>0){
  3063. level= (bias + level)>>QMAT_SHIFT;
  3064. coeff[0][k]= level;
  3065. coeff[1][k]= level-1;
  3066. // coeff[2][k]= level-2;
  3067. }else{
  3068. level= (bias - level)>>QMAT_SHIFT;
  3069. coeff[0][k]= -level;
  3070. coeff[1][k]= -level+1;
  3071. // coeff[2][k]= -level+2;
  3072. }
  3073. coeff_count[k]= FFMIN(level, 2);
  3074. max |=level;
  3075. last_non_zero = i;
  3076. }else{
  3077. coeff[0][k]= (level>>31)|1;
  3078. coeff_count[k]= 1;
  3079. }
  3080. }
  3081. *overflow= s->max_qcoeff < max; //overflow might have happend
  3082. if(last_non_zero < start_i){
  3083. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  3084. return last_non_zero;
  3085. }
  3086. lambda= (qscale*qscale*64*105 + 64)>>7; //FIXME finetune
  3087. score_tab[0]= 0;
  3088. for(i=0; i<=last_non_zero - start_i; i++){
  3089. int level_index, run, j;
  3090. const int dct_coeff= block[ scantable[i + start_i] ];
  3091. const int zero_distoration= dct_coeff*dct_coeff;
  3092. int best_score=256*256*256*120;
  3093. last_score += zero_distoration;
  3094. for(level_index=0; level_index < coeff_count[i]; level_index++){
  3095. int distoration;
  3096. int level= coeff[level_index][i];
  3097. int unquant_coeff;
  3098. assert(level);
  3099. if(s->out_format == FMT_H263){
  3100. if(level>0){
  3101. unquant_coeff= level*qmul + qadd;
  3102. }else{
  3103. unquant_coeff= level*qmul - qadd;
  3104. }
  3105. }else{ //MPEG1
  3106. j= s->idct_permutation[ scantable[i + start_i] ]; //FIXME optimize
  3107. if(s->mb_intra){
  3108. if (level < 0) {
  3109. unquant_coeff = (int)((-level) * qscale * s->intra_matrix[j]) >> 3;
  3110. unquant_coeff = -((unquant_coeff - 1) | 1);
  3111. } else {
  3112. unquant_coeff = (int)( level * qscale * s->intra_matrix[j]) >> 3;
  3113. unquant_coeff = (unquant_coeff - 1) | 1;
  3114. }
  3115. }else{
  3116. if (level < 0) {
  3117. unquant_coeff = ((((-level) << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
  3118. unquant_coeff = -((unquant_coeff - 1) | 1);
  3119. } else {
  3120. unquant_coeff = ((( level << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
  3121. unquant_coeff = (unquant_coeff - 1) | 1;
  3122. }
  3123. }
  3124. unquant_coeff<<= 3;
  3125. }
  3126. distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff);
  3127. level+=64;
  3128. if((level&(~127)) == 0){
  3129. for(run=0; run<=i - left_limit; run++){
  3130. int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3131. score += score_tab[i-run];
  3132. if(score < best_score){
  3133. best_score=
  3134. score_tab[i+1]= score;
  3135. run_tab[i+1]= run;
  3136. level_tab[i+1]= level-64;
  3137. }
  3138. }
  3139. if(s->out_format == FMT_H263){
  3140. for(run=0; run<=i - left_limit; run++){
  3141. int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3142. score += score_tab[i-run];
  3143. if(score < last_score){
  3144. last_score= score;
  3145. last_run= run;
  3146. last_level= level-64;
  3147. last_i= i+1;
  3148. }
  3149. }
  3150. }
  3151. }else{
  3152. distoration += esc_length*lambda;
  3153. for(run=0; run<=i - left_limit; run++){
  3154. int score= distoration + score_tab[i-run];
  3155. if(score < best_score){
  3156. best_score=
  3157. score_tab[i+1]= score;
  3158. run_tab[i+1]= run;
  3159. level_tab[i+1]= level-64;
  3160. }
  3161. }
  3162. if(s->out_format == FMT_H263){
  3163. for(run=0; run<=i - left_limit; run++){
  3164. int score= distoration + score_tab[i-run];
  3165. if(score < last_score){
  3166. last_score= score;
  3167. last_run= run;
  3168. last_level= level-64;
  3169. last_i= i+1;
  3170. }
  3171. }
  3172. }
  3173. }
  3174. }
  3175. for(j=left_limit; j<=i; j++){
  3176. score_tab[j] += zero_distoration;
  3177. }
  3178. score_limit+= zero_distoration;
  3179. if(score_tab[i+1] < score_limit)
  3180. score_limit= score_tab[i+1];
  3181. //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
  3182. while(score_tab[ left_limit ] > score_limit + lambda) left_limit++;
  3183. }
  3184. //FIXME add some cbp penalty
  3185. if(s->out_format != FMT_H263){
  3186. last_score= 256*256*256*120;
  3187. for(i= left_limit; i<=last_non_zero - start_i + 1; i++){
  3188. int score= score_tab[i];
  3189. if(i) score += lambda*2; //FIXME exacter?
  3190. if(score < last_score){
  3191. last_score= score;
  3192. last_i= i;
  3193. last_level= level_tab[i];
  3194. last_run= run_tab[i];
  3195. }
  3196. }
  3197. }
  3198. last_non_zero= last_i - 1 + start_i;
  3199. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  3200. if(last_non_zero < start_i)
  3201. return last_non_zero;
  3202. i= last_i;
  3203. assert(last_level);
  3204. //FIXME use permutated scantable
  3205. block[ s->idct_permutation[ scantable[last_non_zero] ] ]= last_level;
  3206. i -= last_run + 1;
  3207. for(;i>0 ; i -= run_tab[i] + 1){
  3208. const int j= s->idct_permutation[ scantable[i - 1 + start_i] ];
  3209. block[j]= level_tab[i];
  3210. assert(block[j]);
  3211. }
  3212. return last_non_zero;
  3213. }
  3214. static int dct_quantize_c(MpegEncContext *s,
  3215. DCTELEM *block, int n,
  3216. int qscale, int *overflow)
  3217. {
  3218. int i, j, level, last_non_zero, q;
  3219. const int *qmat;
  3220. const UINT8 *scantable= s->intra_scantable.scantable;
  3221. int bias;
  3222. int max=0;
  3223. unsigned int threshold1, threshold2;
  3224. s->fdct (block);
  3225. if (s->mb_intra) {
  3226. if (!s->h263_aic) {
  3227. if (n < 4)
  3228. q = s->y_dc_scale;
  3229. else
  3230. q = s->c_dc_scale;
  3231. q = q << 3;
  3232. } else
  3233. /* For AIC we skip quant/dequant of INTRADC */
  3234. q = 1 << 3;
  3235. /* note: block[0] is assumed to be positive */
  3236. block[0] = (block[0] + (q >> 1)) / q;
  3237. i = 1;
  3238. last_non_zero = 0;
  3239. qmat = s->q_intra_matrix[qscale];
  3240. bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  3241. } else {
  3242. i = 0;
  3243. last_non_zero = -1;
  3244. qmat = s->q_inter_matrix[qscale];
  3245. bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  3246. }
  3247. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  3248. threshold2= (threshold1<<1);
  3249. for(;i<64;i++) {
  3250. j = scantable[i];
  3251. level = block[j];
  3252. level = level * qmat[j];
  3253. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  3254. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  3255. if(((unsigned)(level+threshold1))>threshold2){
  3256. if(level>0){
  3257. level= (bias + level)>>QMAT_SHIFT;
  3258. block[j]= level;
  3259. }else{
  3260. level= (bias - level)>>QMAT_SHIFT;
  3261. block[j]= -level;
  3262. }
  3263. max |=level;
  3264. last_non_zero = i;
  3265. }else{
  3266. block[j]=0;
  3267. }
  3268. }
  3269. *overflow= s->max_qcoeff < max; //overflow might have happend
  3270. /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
  3271. if (s->idct_permutation_type != FF_NO_IDCT_PERM)
  3272. ff_block_permute(block, s->idct_permutation, scantable, last_non_zero);
  3273. return last_non_zero;
  3274. }
  3275. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  3276. DCTELEM *block, int n, int qscale)
  3277. {
  3278. int i, level, nCoeffs;
  3279. const UINT16 *quant_matrix;
  3280. nCoeffs= s->block_last_index[n];
  3281. if (s->mb_intra) {
  3282. if (n < 4)
  3283. block[0] = block[0] * s->y_dc_scale;
  3284. else
  3285. block[0] = block[0] * s->c_dc_scale;
  3286. /* XXX: only mpeg1 */
  3287. quant_matrix = s->intra_matrix;
  3288. for(i=1;i<=nCoeffs;i++) {
  3289. int j= s->intra_scantable.permutated[i];
  3290. level = block[j];
  3291. if (level) {
  3292. if (level < 0) {
  3293. level = -level;
  3294. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  3295. level = (level - 1) | 1;
  3296. level = -level;
  3297. } else {
  3298. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  3299. level = (level - 1) | 1;
  3300. }
  3301. #ifdef PARANOID
  3302. if (level < -2048 || level > 2047)
  3303. fprintf(stderr, "unquant error %d %d\n", i, level);
  3304. #endif
  3305. block[j] = level;
  3306. }
  3307. }
  3308. } else {
  3309. i = 0;
  3310. quant_matrix = s->inter_matrix;
  3311. for(;i<=nCoeffs;i++) {
  3312. int j= s->intra_scantable.permutated[i];
  3313. level = block[j];
  3314. if (level) {
  3315. if (level < 0) {
  3316. level = -level;
  3317. level = (((level << 1) + 1) * qscale *
  3318. ((int) (quant_matrix[j]))) >> 4;
  3319. level = (level - 1) | 1;
  3320. level = -level;
  3321. } else {
  3322. level = (((level << 1) + 1) * qscale *
  3323. ((int) (quant_matrix[j]))) >> 4;
  3324. level = (level - 1) | 1;
  3325. }
  3326. #ifdef PARANOID
  3327. if (level < -2048 || level > 2047)
  3328. fprintf(stderr, "unquant error %d %d\n", i, level);
  3329. #endif
  3330. block[j] = level;
  3331. }
  3332. }
  3333. }
  3334. }
  3335. static void dct_unquantize_mpeg2_c(MpegEncContext *s,
  3336. DCTELEM *block, int n, int qscale)
  3337. {
  3338. int i, level, nCoeffs;
  3339. const UINT16 *quant_matrix;
  3340. if(s->alternate_scan) nCoeffs= 63;
  3341. else nCoeffs= s->block_last_index[n];
  3342. if (s->mb_intra) {
  3343. if (n < 4)
  3344. block[0] = block[0] * s->y_dc_scale;
  3345. else
  3346. block[0] = block[0] * s->c_dc_scale;
  3347. quant_matrix = s->intra_matrix;
  3348. for(i=1;i<=nCoeffs;i++) {
  3349. int j= s->intra_scantable.permutated[i];
  3350. level = block[j];
  3351. if (level) {
  3352. if (level < 0) {
  3353. level = -level;
  3354. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  3355. level = -level;
  3356. } else {
  3357. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  3358. }
  3359. #ifdef PARANOID
  3360. if (level < -2048 || level > 2047)
  3361. fprintf(stderr, "unquant error %d %d\n", i, level);
  3362. #endif
  3363. block[j] = level;
  3364. }
  3365. }
  3366. } else {
  3367. int sum=-1;
  3368. i = 0;
  3369. quant_matrix = s->inter_matrix;
  3370. for(;i<=nCoeffs;i++) {
  3371. int j= s->intra_scantable.permutated[i];
  3372. level = block[j];
  3373. if (level) {
  3374. if (level < 0) {
  3375. level = -level;
  3376. level = (((level << 1) + 1) * qscale *
  3377. ((int) (quant_matrix[j]))) >> 4;
  3378. level = -level;
  3379. } else {
  3380. level = (((level << 1) + 1) * qscale *
  3381. ((int) (quant_matrix[j]))) >> 4;
  3382. }
  3383. #ifdef PARANOID
  3384. if (level < -2048 || level > 2047)
  3385. fprintf(stderr, "unquant error %d %d\n", i, level);
  3386. #endif
  3387. block[j] = level;
  3388. sum+=level;
  3389. }
  3390. }
  3391. block[63]^=sum&1;
  3392. }
  3393. }
  3394. static void dct_unquantize_h263_c(MpegEncContext *s,
  3395. DCTELEM *block, int n, int qscale)
  3396. {
  3397. int i, level, qmul, qadd;
  3398. int nCoeffs;
  3399. assert(s->block_last_index[n]>=0);
  3400. qadd = (qscale - 1) | 1;
  3401. qmul = qscale << 1;
  3402. if (s->mb_intra) {
  3403. if (!s->h263_aic) {
  3404. if (n < 4)
  3405. block[0] = block[0] * s->y_dc_scale;
  3406. else
  3407. block[0] = block[0] * s->c_dc_scale;
  3408. }else
  3409. qadd = 0;
  3410. i = 1;
  3411. nCoeffs= 63; //does not allways use zigzag table
  3412. } else {
  3413. i = 0;
  3414. nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
  3415. }
  3416. for(;i<=nCoeffs;i++) {
  3417. level = block[i];
  3418. if (level) {
  3419. if (level < 0) {
  3420. level = level * qmul - qadd;
  3421. } else {
  3422. level = level * qmul + qadd;
  3423. }
  3424. #ifdef PARANOID
  3425. if (level < -2048 || level > 2047)
  3426. fprintf(stderr, "unquant error %d %d\n", i, level);
  3427. #endif
  3428. block[i] = level;
  3429. }
  3430. }
  3431. }
  3432. char ff_get_pict_type_char(int pict_type){
  3433. switch(pict_type){
  3434. case I_TYPE: return 'I';
  3435. case P_TYPE: return 'P';
  3436. case B_TYPE: return 'B';
  3437. case S_TYPE: return 'S';
  3438. default: return '?';
  3439. }
  3440. }
  3441. AVCodec mpeg1video_encoder = {
  3442. "mpeg1video",
  3443. CODEC_TYPE_VIDEO,
  3444. CODEC_ID_MPEG1VIDEO,
  3445. sizeof(MpegEncContext),
  3446. MPV_encode_init,
  3447. MPV_encode_picture,
  3448. MPV_encode_end,
  3449. };
  3450. #ifdef CONFIG_RISKY
  3451. AVCodec h263_encoder = {
  3452. "h263",
  3453. CODEC_TYPE_VIDEO,
  3454. CODEC_ID_H263,
  3455. sizeof(MpegEncContext),
  3456. MPV_encode_init,
  3457. MPV_encode_picture,
  3458. MPV_encode_end,
  3459. };
  3460. AVCodec h263p_encoder = {
  3461. "h263p",
  3462. CODEC_TYPE_VIDEO,
  3463. CODEC_ID_H263P,
  3464. sizeof(MpegEncContext),
  3465. MPV_encode_init,
  3466. MPV_encode_picture,
  3467. MPV_encode_end,
  3468. };
  3469. AVCodec rv10_encoder = {
  3470. "rv10",
  3471. CODEC_TYPE_VIDEO,
  3472. CODEC_ID_RV10,
  3473. sizeof(MpegEncContext),
  3474. MPV_encode_init,
  3475. MPV_encode_picture,
  3476. MPV_encode_end,
  3477. };
  3478. AVCodec mpeg4_encoder = {
  3479. "mpeg4",
  3480. CODEC_TYPE_VIDEO,
  3481. CODEC_ID_MPEG4,
  3482. sizeof(MpegEncContext),
  3483. MPV_encode_init,
  3484. MPV_encode_picture,
  3485. MPV_encode_end,
  3486. };
  3487. AVCodec msmpeg4v1_encoder = {
  3488. "msmpeg4v1",
  3489. CODEC_TYPE_VIDEO,
  3490. CODEC_ID_MSMPEG4V1,
  3491. sizeof(MpegEncContext),
  3492. MPV_encode_init,
  3493. MPV_encode_picture,
  3494. MPV_encode_end,
  3495. };
  3496. AVCodec msmpeg4v2_encoder = {
  3497. "msmpeg4v2",
  3498. CODEC_TYPE_VIDEO,
  3499. CODEC_ID_MSMPEG4V2,
  3500. sizeof(MpegEncContext),
  3501. MPV_encode_init,
  3502. MPV_encode_picture,
  3503. MPV_encode_end,
  3504. };
  3505. AVCodec msmpeg4v3_encoder = {
  3506. "msmpeg4",
  3507. CODEC_TYPE_VIDEO,
  3508. CODEC_ID_MSMPEG4V3,
  3509. sizeof(MpegEncContext),
  3510. MPV_encode_init,
  3511. MPV_encode_picture,
  3512. MPV_encode_end,
  3513. };
  3514. AVCodec wmv1_encoder = {
  3515. "wmv1",
  3516. CODEC_TYPE_VIDEO,
  3517. CODEC_ID_WMV1,
  3518. sizeof(MpegEncContext),
  3519. MPV_encode_init,
  3520. MPV_encode_picture,
  3521. MPV_encode_end,
  3522. };
  3523. #endif
  3524. AVCodec mjpeg_encoder = {
  3525. "mjpeg",
  3526. CODEC_TYPE_VIDEO,
  3527. CODEC_ID_MJPEG,
  3528. sizeof(MpegEncContext),
  3529. MPV_encode_init,
  3530. MPV_encode_picture,
  3531. MPV_encode_end,
  3532. };