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.

5026 lines
175KB

  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. /**
  22. * @file mpegvideo.c
  23. * The simplest mpeg encoder (well, it was the simplest!).
  24. */
  25. #include <limits.h>
  26. #include "avcodec.h"
  27. #include "dsputil.h"
  28. #include "mpegvideo.h"
  29. #include "faandct.h"
  30. #ifdef USE_FASTMEMCPY
  31. #include "fastmemcpy.h"
  32. #endif
  33. //#undef NDEBUG
  34. //#include <assert.h>
  35. #ifdef CONFIG_ENCODERS
  36. static void encode_picture(MpegEncContext *s, int picture_number);
  37. #endif //CONFIG_ENCODERS
  38. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  39. DCTELEM *block, int n, int qscale);
  40. static void dct_unquantize_mpeg2_c(MpegEncContext *s,
  41. DCTELEM *block, int n, int qscale);
  42. static void dct_unquantize_h263_c(MpegEncContext *s,
  43. DCTELEM *block, int n, int qscale);
  44. static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
  45. #ifdef CONFIG_ENCODERS
  46. static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
  47. static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
  48. static int sse_mb(MpegEncContext *s);
  49. #endif //CONFIG_ENCODERS
  50. #ifdef HAVE_XVMC
  51. extern int XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx);
  52. extern void XVMC_field_end(MpegEncContext *s);
  53. extern void XVMC_decode_mb(MpegEncContext *s);
  54. #endif
  55. void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
  56. /* enable all paranoid tests for rounding, overflows, etc... */
  57. //#define PARANOID
  58. //#define DEBUG
  59. /* for jpeg fast DCT */
  60. #define CONST_BITS 14
  61. static const uint16_t aanscales[64] = {
  62. /* precomputed values scaled up by 14 bits */
  63. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  64. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  65. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  66. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  67. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  68. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  69. 8867 , 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  70. 4520 , 6270, 5906, 5315, 4520, 3552, 2446, 1247
  71. };
  72. static const uint8_t h263_chroma_roundtab[16] = {
  73. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  74. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
  75. };
  76. #ifdef CONFIG_ENCODERS
  77. static uint8_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
  78. static uint8_t default_fcode_tab[MAX_MV*2+1];
  79. enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
  80. static void convert_matrix(DSPContext *dsp, int (*qmat)[64], uint16_t (*qmat16)[2][64],
  81. const uint16_t *quant_matrix, int bias, int qmin, int qmax)
  82. {
  83. int qscale;
  84. for(qscale=qmin; qscale<=qmax; qscale++){
  85. int i;
  86. if (dsp->fdct == ff_jpeg_fdct_islow
  87. #ifdef FAAN_POSTSCALE
  88. || dsp->fdct == ff_faandct
  89. #endif
  90. ) {
  91. for(i=0;i<64;i++) {
  92. const int j= dsp->idct_permutation[i];
  93. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  94. /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  95. /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
  96. /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
  97. qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) /
  98. (qscale * quant_matrix[j]));
  99. }
  100. } else if (dsp->fdct == fdct_ifast
  101. #ifndef FAAN_POSTSCALE
  102. || dsp->fdct == ff_faandct
  103. #endif
  104. ) {
  105. for(i=0;i<64;i++) {
  106. const int j= dsp->idct_permutation[i];
  107. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  108. /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  109. /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
  110. /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
  111. qmat[qscale][i] = (int)((uint64_t_C(1) << (QMAT_SHIFT + 14)) /
  112. (aanscales[i] * qscale * quant_matrix[j]));
  113. }
  114. } else {
  115. for(i=0;i<64;i++) {
  116. const int j= dsp->idct_permutation[i];
  117. /* We can safely suppose that 16 <= quant_matrix[i] <= 255
  118. So 16 <= qscale * quant_matrix[i] <= 7905
  119. so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
  120. so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
  121. */
  122. qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
  123. // qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
  124. qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
  125. if(qmat16[qscale][0][i]==0 || qmat16[qscale][0][i]==128*256) qmat16[qscale][0][i]=128*256-1;
  126. qmat16[qscale][1][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][0][i]);
  127. }
  128. }
  129. }
  130. }
  131. static inline void update_qscale(MpegEncContext *s){
  132. s->qscale= (s->lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
  133. s->qscale= clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
  134. s->lambda2= (s->lambda*s->lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
  135. }
  136. #endif //CONFIG_ENCODERS
  137. void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){
  138. int i;
  139. int end;
  140. st->scantable= src_scantable;
  141. for(i=0; i<64; i++){
  142. int j;
  143. j = src_scantable[i];
  144. st->permutated[i] = permutation[j];
  145. #ifdef ARCH_POWERPC
  146. st->inverse[j] = i;
  147. #endif
  148. }
  149. end=-1;
  150. for(i=0; i<64; i++){
  151. int j;
  152. j = st->permutated[i];
  153. if(j>end) end=j;
  154. st->raster_end[i]= end;
  155. }
  156. }
  157. #ifdef CONFIG_ENCODERS
  158. void ff_write_quant_matrix(PutBitContext *pb, int16_t *matrix){
  159. int i;
  160. if(matrix){
  161. put_bits(pb, 1, 1);
  162. for(i=0;i<64;i++) {
  163. put_bits(pb, 8, matrix[ ff_zigzag_direct[i] ]);
  164. }
  165. }else
  166. put_bits(pb, 1, 0);
  167. }
  168. #endif //CONFIG_ENCODERS
  169. /* init common dct for both encoder and decoder */
  170. int DCT_common_init(MpegEncContext *s)
  171. {
  172. s->dct_unquantize_h263 = dct_unquantize_h263_c;
  173. s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c;
  174. s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c;
  175. #ifdef CONFIG_ENCODERS
  176. s->dct_quantize= dct_quantize_c;
  177. #endif
  178. #ifdef HAVE_MMX
  179. MPV_common_init_mmx(s);
  180. #endif
  181. #ifdef ARCH_ALPHA
  182. MPV_common_init_axp(s);
  183. #endif
  184. #ifdef HAVE_MLIB
  185. MPV_common_init_mlib(s);
  186. #endif
  187. #ifdef HAVE_MMI
  188. MPV_common_init_mmi(s);
  189. #endif
  190. #ifdef ARCH_ARMV4L
  191. MPV_common_init_armv4l(s);
  192. #endif
  193. #ifdef ARCH_POWERPC
  194. MPV_common_init_ppc(s);
  195. #endif
  196. #ifdef CONFIG_ENCODERS
  197. s->fast_dct_quantize= s->dct_quantize;
  198. if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
  199. s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_*
  200. }
  201. #endif //CONFIG_ENCODERS
  202. /* load & permutate scantables
  203. note: only wmv uses differnt ones
  204. */
  205. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  206. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  207. ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  208. ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  209. s->picture_structure= PICT_FRAME;
  210. return 0;
  211. }
  212. static void copy_picture(Picture *dst, Picture *src){
  213. *dst = *src;
  214. dst->type= FF_BUFFER_TYPE_COPY;
  215. }
  216. /**
  217. * allocates a Picture
  218. * The pixels are allocated/set by calling get_buffer() if shared=0
  219. */
  220. static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
  221. const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) doesnt sig11
  222. const int mb_array_size= s->mb_stride*s->mb_height;
  223. int i;
  224. if(shared){
  225. assert(pic->data[0]);
  226. assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
  227. pic->type= FF_BUFFER_TYPE_SHARED;
  228. }else{
  229. int r;
  230. assert(!pic->data[0]);
  231. r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
  232. if(r<0 || !pic->age || !pic->type || !pic->data[0]){
  233. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
  234. return -1;
  235. }
  236. if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
  237. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
  238. return -1;
  239. }
  240. if(pic->linesize[1] != pic->linesize[2]){
  241. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride missmatch)\n");
  242. return -1;
  243. }
  244. s->linesize = pic->linesize[0];
  245. s->uvlinesize= pic->linesize[1];
  246. }
  247. if(pic->qscale_table==NULL){
  248. if (s->encoding) {
  249. CHECKED_ALLOCZ(pic->mb_var , mb_array_size * sizeof(int16_t))
  250. CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t))
  251. CHECKED_ALLOCZ(pic->mb_mean , mb_array_size * sizeof(int8_t))
  252. }
  253. CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2) //the +2 is for the slice end check
  254. CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
  255. CHECKED_ALLOCZ(pic->mb_type_base , big_mb_num * sizeof(int))
  256. pic->mb_type= pic->mb_type_base + s->mb_stride+1;
  257. if(s->out_format == FMT_H264){
  258. for(i=0; i<2; i++){
  259. CHECKED_ALLOCZ(pic->motion_val[i], 2 * 16 * s->mb_num * sizeof(uint16_t))
  260. CHECKED_ALLOCZ(pic->ref_index[i] , 4 * s->mb_num * sizeof(uint8_t))
  261. }
  262. }
  263. pic->qstride= s->mb_stride;
  264. CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan))
  265. }
  266. //it might be nicer if the application would keep track of these but it would require a API change
  267. memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
  268. s->prev_pict_types[0]= s->pict_type;
  269. if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
  270. pic->age= INT_MAX; // skiped MBs in b frames are quite rare in mpeg1/2 and its a bit tricky to skip them anyway
  271. return 0;
  272. fail: //for the CHECKED_ALLOCZ macro
  273. return -1;
  274. }
  275. /**
  276. * deallocates a picture
  277. */
  278. static void free_picture(MpegEncContext *s, Picture *pic){
  279. int i;
  280. if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
  281. s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
  282. }
  283. av_freep(&pic->mb_var);
  284. av_freep(&pic->mc_mb_var);
  285. av_freep(&pic->mb_mean);
  286. av_freep(&pic->mbskip_table);
  287. av_freep(&pic->qscale_table);
  288. av_freep(&pic->mb_type_base);
  289. av_freep(&pic->pan_scan);
  290. pic->mb_type= NULL;
  291. for(i=0; i<2; i++){
  292. av_freep(&pic->motion_val[i]);
  293. av_freep(&pic->ref_index[i]);
  294. }
  295. if(pic->type == FF_BUFFER_TYPE_SHARED){
  296. for(i=0; i<4; i++){
  297. pic->base[i]=
  298. pic->data[i]= NULL;
  299. }
  300. pic->type= 0;
  301. }
  302. }
  303. /* init common structure for both encoder and decoder */
  304. int MPV_common_init(MpegEncContext *s)
  305. {
  306. int y_size, c_size, yc_size, i, mb_array_size, x, y;
  307. dsputil_init(&s->dsp, s->avctx);
  308. DCT_common_init(s);
  309. s->flags= s->avctx->flags;
  310. s->mb_width = (s->width + 15) / 16;
  311. s->mb_height = (s->height + 15) / 16;
  312. s->mb_stride = s->mb_width + 1;
  313. mb_array_size= s->mb_height * s->mb_stride;
  314. /* set default edge pos, will be overriden in decode_header if needed */
  315. s->h_edge_pos= s->mb_width*16;
  316. s->v_edge_pos= s->mb_height*16;
  317. s->mb_num = s->mb_width * s->mb_height;
  318. s->block_wrap[0]=
  319. s->block_wrap[1]=
  320. s->block_wrap[2]=
  321. s->block_wrap[3]= s->mb_width*2 + 2;
  322. s->block_wrap[4]=
  323. s->block_wrap[5]= s->mb_width + 2;
  324. y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  325. c_size = (s->mb_width + 2) * (s->mb_height + 2);
  326. yc_size = y_size + 2 * c_size;
  327. /* convert fourcc to upper case */
  328. s->avctx->codec_tag= toupper( s->avctx->codec_tag &0xFF)
  329. + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
  330. + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)
  331. + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
  332. s->avctx->stream_codec_tag= toupper( s->avctx->stream_codec_tag &0xFF)
  333. + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 )
  334. + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16)
  335. + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24);
  336. CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance
  337. s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
  338. s->avctx->coded_frame= (AVFrame*)&s->current_picture;
  339. CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int)) //error ressilience code looks cleaner with this
  340. for(y=0; y<s->mb_height; y++){
  341. for(x=0; x<s->mb_width; x++){
  342. s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
  343. }
  344. }
  345. s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed?
  346. if (s->encoding) {
  347. int mv_table_size= s->mb_stride * (s->mb_height+2) + 1;
  348. /* Allocate MV tables */
  349. CHECKED_ALLOCZ(s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  350. CHECKED_ALLOCZ(s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  351. CHECKED_ALLOCZ(s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  352. CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  353. CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  354. CHECKED_ALLOCZ(s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
  355. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  356. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  357. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  358. s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  359. s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  360. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  361. //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer()
  362. CHECKED_ALLOCZ(s->me.scratchpad, s->width*2*16*3*sizeof(uint8_t))
  363. CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t))
  364. CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
  365. if(s->codec_id==CODEC_ID_MPEG4){
  366. CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
  367. CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE);
  368. }
  369. if(s->msmpeg4_version){
  370. CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
  371. }
  372. CHECKED_ALLOCZ(s->avctx->stats_out, 256);
  373. /* Allocate MB type table */
  374. CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint8_t)) //needed for encoding
  375. CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int))
  376. CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int))
  377. CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))
  378. CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))
  379. CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))
  380. CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
  381. CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
  382. if(s->avctx->noise_reduction){
  383. CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int))
  384. CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))
  385. }
  386. }
  387. CHECKED_ALLOCZ(s->blocks, 64*6*2 * sizeof(DCTELEM))
  388. CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))
  389. CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
  390. if (s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_VIS_MV)) {
  391. int size;
  392. /* MV prediction */
  393. size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  394. CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(int16_t));
  395. }
  396. if(s->codec_id==CODEC_ID_MPEG4){
  397. /* interlaced direct mode decoding tables */
  398. CHECKED_ALLOCZ(s->field_mv_table, mb_array_size*2*2 * sizeof(int16_t))
  399. CHECKED_ALLOCZ(s->field_select_table, mb_array_size*2* sizeof(int8_t))
  400. }
  401. if (s->out_format == FMT_H263) {
  402. /* ac values */
  403. CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(int16_t) * 16);
  404. s->ac_val[1] = s->ac_val[0] + y_size;
  405. s->ac_val[2] = s->ac_val[1] + c_size;
  406. /* cbp values */
  407. CHECKED_ALLOCZ(s->coded_block, y_size);
  408. /* divx501 bitstream reorder buffer */
  409. CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
  410. /* cbp, ac_pred, pred_dir */
  411. CHECKED_ALLOCZ(s->cbp_table , mb_array_size * sizeof(uint8_t))
  412. CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
  413. }
  414. if (s->h263_pred || s->h263_plus || !s->encoding) {
  415. /* dc values */
  416. //MN: we need these for error resilience of intra-frames
  417. CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(int16_t));
  418. s->dc_val[1] = s->dc_val[0] + y_size;
  419. s->dc_val[2] = s->dc_val[1] + c_size;
  420. for(i=0;i<yc_size;i++)
  421. s->dc_val[0][i] = 1024;
  422. }
  423. /* which mb is a intra block */
  424. CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
  425. memset(s->mbintra_table, 1, mb_array_size);
  426. /* default structure is frame */
  427. s->picture_structure = PICT_FRAME;
  428. /* init macroblock skip table */
  429. CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
  430. //Note the +1 is for a quicker mpeg4 slice_end detection
  431. CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
  432. s->block= s->blocks[0];
  433. for(i=0;i<12;i++){
  434. s->pblocks[i] = (short *)(&s->block[i]);
  435. }
  436. s->parse_context.state= -1;
  437. s->context_initialized = 1;
  438. return 0;
  439. fail:
  440. MPV_common_end(s);
  441. return -1;
  442. }
  443. //extern int sads;
  444. /* init common structure for both encoder and decoder */
  445. void MPV_common_end(MpegEncContext *s)
  446. {
  447. int i;
  448. av_freep(&s->parse_context.buffer);
  449. s->parse_context.buffer_size=0;
  450. av_freep(&s->mb_type);
  451. av_freep(&s->p_mv_table_base);
  452. av_freep(&s->b_forw_mv_table_base);
  453. av_freep(&s->b_back_mv_table_base);
  454. av_freep(&s->b_bidir_forw_mv_table_base);
  455. av_freep(&s->b_bidir_back_mv_table_base);
  456. av_freep(&s->b_direct_mv_table_base);
  457. s->p_mv_table= NULL;
  458. s->b_forw_mv_table= NULL;
  459. s->b_back_mv_table= NULL;
  460. s->b_bidir_forw_mv_table= NULL;
  461. s->b_bidir_back_mv_table= NULL;
  462. s->b_direct_mv_table= NULL;
  463. av_freep(&s->motion_val);
  464. av_freep(&s->dc_val[0]);
  465. av_freep(&s->ac_val[0]);
  466. av_freep(&s->coded_block);
  467. av_freep(&s->mbintra_table);
  468. av_freep(&s->cbp_table);
  469. av_freep(&s->pred_dir_table);
  470. av_freep(&s->me.scratchpad);
  471. av_freep(&s->me.map);
  472. av_freep(&s->me.score_map);
  473. av_freep(&s->mbskip_table);
  474. av_freep(&s->prev_pict_types);
  475. av_freep(&s->bitstream_buffer);
  476. av_freep(&s->tex_pb_buffer);
  477. av_freep(&s->pb2_buffer);
  478. av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
  479. av_freep(&s->field_mv_table);
  480. av_freep(&s->field_select_table);
  481. av_freep(&s->avctx->stats_out);
  482. av_freep(&s->ac_stats);
  483. av_freep(&s->error_status_table);
  484. av_freep(&s->mb_index2xy);
  485. av_freep(&s->lambda_table);
  486. av_freep(&s->q_intra_matrix);
  487. av_freep(&s->q_inter_matrix);
  488. av_freep(&s->q_intra_matrix16);
  489. av_freep(&s->q_inter_matrix16);
  490. av_freep(&s->blocks);
  491. av_freep(&s->input_picture);
  492. av_freep(&s->reordered_input_picture);
  493. av_freep(&s->dct_error_sum);
  494. av_freep(&s->dct_offset);
  495. if(s->picture){
  496. for(i=0; i<MAX_PICTURE_COUNT; i++){
  497. free_picture(s, &s->picture[i]);
  498. }
  499. }
  500. av_freep(&s->picture);
  501. avcodec_default_free_buffers(s->avctx);
  502. s->context_initialized = 0;
  503. s->last_picture_ptr=
  504. s->next_picture_ptr=
  505. s->current_picture_ptr= NULL;
  506. }
  507. #ifdef CONFIG_ENCODERS
  508. /* init video encoder */
  509. int MPV_encode_init(AVCodecContext *avctx)
  510. {
  511. MpegEncContext *s = avctx->priv_data;
  512. int i, dummy;
  513. int chroma_h_shift, chroma_v_shift;
  514. avctx->pix_fmt = PIX_FMT_YUV420P; // FIXME
  515. s->bit_rate = avctx->bit_rate;
  516. s->bit_rate_tolerance = avctx->bit_rate_tolerance;
  517. s->width = avctx->width;
  518. s->height = avctx->height;
  519. if(avctx->gop_size > 600){
  520. av_log(avctx, AV_LOG_ERROR, "Warning keyframe interval too large! reducing it ...\n");
  521. avctx->gop_size=600;
  522. }
  523. s->gop_size = avctx->gop_size;
  524. s->rtp_mode = avctx->rtp_mode;
  525. s->rtp_payload_size = avctx->rtp_payload_size;
  526. if (avctx->rtp_callback)
  527. s->rtp_callback = avctx->rtp_callback;
  528. s->max_qdiff= avctx->max_qdiff;
  529. s->qcompress= avctx->qcompress;
  530. s->qblur= avctx->qblur;
  531. s->avctx = avctx;
  532. s->flags= avctx->flags;
  533. s->max_b_frames= avctx->max_b_frames;
  534. s->b_frame_strategy= avctx->b_frame_strategy;
  535. s->codec_id= avctx->codec->id;
  536. s->luma_elim_threshold = avctx->luma_elim_threshold;
  537. s->chroma_elim_threshold= avctx->chroma_elim_threshold;
  538. s->strict_std_compliance= avctx->strict_std_compliance;
  539. s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
  540. s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
  541. s->mpeg_quant= avctx->mpeg_quant;
  542. if (s->gop_size <= 1) {
  543. s->intra_only = 1;
  544. s->gop_size = 12;
  545. } else {
  546. s->intra_only = 0;
  547. }
  548. s->me_method = avctx->me_method;
  549. /* Fixed QSCALE */
  550. s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
  551. s->adaptive_quant= ( s->avctx->lumi_masking
  552. || s->avctx->dark_masking
  553. || s->avctx->temporal_cplx_masking
  554. || s->avctx->spatial_cplx_masking
  555. || s->avctx->p_masking
  556. || (s->flags&CODEC_FLAG_QP_RD))
  557. && !s->fixed_qscale;
  558. s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT);
  559. s->obmc= (s->codec_id == CODEC_ID_H263 || s->codec_id == CODEC_ID_H263P)
  560. && (s->flags & CODEC_FLAG_4MV);
  561. if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4
  562. && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){
  563. av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
  564. return -1;
  565. }
  566. if(s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE){
  567. av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with simple mb decission\n");
  568. return -1;
  569. }
  570. if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
  571. av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
  572. return -1;
  573. }
  574. if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
  575. av_log(avctx, AV_LOG_ERROR, "data partitioning not supported by codec\n");
  576. return -1;
  577. }
  578. if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){
  579. av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
  580. return -1;
  581. }
  582. if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too
  583. av_log(avctx, AV_LOG_ERROR, "mpeg2 style quantization not supporetd by codec\n");
  584. return -1;
  585. }
  586. if((s->flags & CODEC_FLAG_CBP_RD) && !(s->flags & CODEC_FLAG_TRELLIS_QUANT)){
  587. av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
  588. return -1;
  589. }
  590. if((s->flags & CODEC_FLAG_QP_RD) && s->avctx->mb_decision != FF_MB_DECISION_RD){
  591. av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
  592. return -1;
  593. }
  594. if(s->codec_id==CODEC_ID_MJPEG){
  595. s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
  596. s->inter_quant_bias= 0;
  597. }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO){
  598. s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
  599. s->inter_quant_bias= 0;
  600. }else{
  601. s->intra_quant_bias=0;
  602. s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
  603. }
  604. if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
  605. s->intra_quant_bias= avctx->intra_quant_bias;
  606. if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
  607. s->inter_quant_bias= avctx->inter_quant_bias;
  608. avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
  609. av_reduce(&s->time_increment_resolution, &dummy, s->avctx->frame_rate, s->avctx->frame_rate_base, (1<<16)-1);
  610. s->time_increment_bits = av_log2(s->time_increment_resolution - 1) + 1;
  611. switch(avctx->codec->id) {
  612. case CODEC_ID_MPEG1VIDEO:
  613. s->out_format = FMT_MPEG1;
  614. s->low_delay= 0; //s->max_b_frames ? 0 : 1;
  615. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  616. break;
  617. case CODEC_ID_MPEG2VIDEO:
  618. s->out_format = FMT_MPEG1;
  619. s->low_delay= 0; //s->max_b_frames ? 0 : 1;
  620. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  621. s->rtp_mode= 1; // mpeg2 must have slices
  622. if(s->rtp_payload_size == 0) s->rtp_payload_size= 256*256*256;
  623. break;
  624. case CODEC_ID_LJPEG:
  625. case CODEC_ID_MJPEG:
  626. s->out_format = FMT_MJPEG;
  627. s->intra_only = 1; /* force intra only for jpeg */
  628. s->mjpeg_write_tables = 1; /* write all tables */
  629. s->mjpeg_data_only_frames = 0; /* write all the needed headers */
  630. s->mjpeg_vsample[0] = 1<<chroma_v_shift;
  631. s->mjpeg_vsample[1] = 1;
  632. s->mjpeg_vsample[2] = 1;
  633. s->mjpeg_hsample[0] = 1<<chroma_h_shift;
  634. s->mjpeg_hsample[1] = 1;
  635. s->mjpeg_hsample[2] = 1;
  636. if (mjpeg_init(s) < 0)
  637. return -1;
  638. avctx->delay=0;
  639. s->low_delay=1;
  640. break;
  641. #ifdef CONFIG_RISKY
  642. case CODEC_ID_H263:
  643. if (h263_get_picture_format(s->width, s->height) == 7) {
  644. av_log(avctx, AV_LOG_INFO, "Input picture size isn't suitable for h263 codec! try h263+\n");
  645. return -1;
  646. }
  647. s->out_format = FMT_H263;
  648. avctx->delay=0;
  649. s->low_delay=1;
  650. break;
  651. case CODEC_ID_H263P:
  652. s->out_format = FMT_H263;
  653. s->h263_plus = 1;
  654. /* Fx */
  655. s->unrestricted_mv=(avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
  656. s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0;
  657. /* /Fx */
  658. /* These are just to be sure */
  659. s->umvplus = 1;
  660. avctx->delay=0;
  661. s->low_delay=1;
  662. break;
  663. case CODEC_ID_FLV1:
  664. s->out_format = FMT_H263;
  665. s->h263_flv = 2; /* format = 1; 11-bit codes */
  666. s->unrestricted_mv = 1;
  667. s->rtp_mode=0; /* don't allow GOB */
  668. avctx->delay=0;
  669. s->low_delay=1;
  670. break;
  671. case CODEC_ID_RV10:
  672. s->out_format = FMT_H263;
  673. s->h263_rv10 = 1;
  674. avctx->delay=0;
  675. s->low_delay=1;
  676. break;
  677. case CODEC_ID_MPEG4:
  678. s->out_format = FMT_H263;
  679. s->h263_pred = 1;
  680. s->unrestricted_mv = 1;
  681. s->low_delay= s->max_b_frames ? 0 : 1;
  682. avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
  683. break;
  684. case CODEC_ID_MSMPEG4V1:
  685. s->out_format = FMT_H263;
  686. s->h263_msmpeg4 = 1;
  687. s->h263_pred = 1;
  688. s->unrestricted_mv = 1;
  689. s->msmpeg4_version= 1;
  690. avctx->delay=0;
  691. s->low_delay=1;
  692. break;
  693. case CODEC_ID_MSMPEG4V2:
  694. s->out_format = FMT_H263;
  695. s->h263_msmpeg4 = 1;
  696. s->h263_pred = 1;
  697. s->unrestricted_mv = 1;
  698. s->msmpeg4_version= 2;
  699. avctx->delay=0;
  700. s->low_delay=1;
  701. break;
  702. case CODEC_ID_MSMPEG4V3:
  703. s->out_format = FMT_H263;
  704. s->h263_msmpeg4 = 1;
  705. s->h263_pred = 1;
  706. s->unrestricted_mv = 1;
  707. s->msmpeg4_version= 3;
  708. s->flipflop_rounding=1;
  709. avctx->delay=0;
  710. s->low_delay=1;
  711. break;
  712. case CODEC_ID_WMV1:
  713. s->out_format = FMT_H263;
  714. s->h263_msmpeg4 = 1;
  715. s->h263_pred = 1;
  716. s->unrestricted_mv = 1;
  717. s->msmpeg4_version= 4;
  718. s->flipflop_rounding=1;
  719. avctx->delay=0;
  720. s->low_delay=1;
  721. break;
  722. case CODEC_ID_WMV2:
  723. s->out_format = FMT_H263;
  724. s->h263_msmpeg4 = 1;
  725. s->h263_pred = 1;
  726. s->unrestricted_mv = 1;
  727. s->msmpeg4_version= 5;
  728. s->flipflop_rounding=1;
  729. avctx->delay=0;
  730. s->low_delay=1;
  731. break;
  732. #endif
  733. default:
  734. return -1;
  735. }
  736. { /* set up some save defaults, some codecs might override them later */
  737. static int done=0;
  738. if(!done){
  739. int i;
  740. done=1;
  741. default_mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
  742. memset(default_mv_penalty, 0, sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1));
  743. memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
  744. for(i=-16; i<16; i++){
  745. default_fcode_tab[i + MAX_MV]= 1;
  746. }
  747. }
  748. }
  749. s->me.mv_penalty= default_mv_penalty;
  750. s->fcode_tab= default_fcode_tab;
  751. s->y_dc_scale_table=
  752. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  753. /* dont use mv_penalty table for crap MV as it would be confused */
  754. //FIXME remove after fixing / removing old ME
  755. if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty;
  756. s->encoding = 1;
  757. /* init */
  758. if (MPV_common_init(s) < 0)
  759. return -1;
  760. ff_init_me(s);
  761. #ifdef CONFIG_ENCODERS
  762. #ifdef CONFIG_RISKY
  763. if (s->out_format == FMT_H263)
  764. h263_encode_init(s);
  765. if(s->msmpeg4_version)
  766. ff_msmpeg4_encode_init(s);
  767. #endif
  768. if (s->out_format == FMT_MPEG1)
  769. ff_mpeg1_encode_init(s);
  770. #endif
  771. /* init default q matrix */
  772. for(i=0;i<64;i++) {
  773. int j= s->dsp.idct_permutation[i];
  774. #ifdef CONFIG_RISKY
  775. if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
  776. s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
  777. s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
  778. }else if(s->out_format == FMT_H263){
  779. s->intra_matrix[j] =
  780. s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
  781. }else
  782. #endif
  783. { /* mpeg1/2 */
  784. s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
  785. s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
  786. }
  787. if(s->avctx->intra_matrix)
  788. s->intra_matrix[j] = s->avctx->intra_matrix[i];
  789. if(s->avctx->inter_matrix)
  790. s->inter_matrix[j] = s->avctx->inter_matrix[i];
  791. }
  792. /* precompute matrix */
  793. /* for mjpeg, we do include qscale in the matrix */
  794. if (s->out_format != FMT_MJPEG) {
  795. convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
  796. s->intra_matrix, s->intra_quant_bias, 1, 31);
  797. convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
  798. s->inter_matrix, s->inter_quant_bias, 1, 31);
  799. }
  800. if(ff_rate_control_init(s) < 0)
  801. return -1;
  802. s->picture_number = 0;
  803. s->picture_in_gop_number = 0;
  804. s->fake_picture_number = 0;
  805. /* motion detector init */
  806. s->f_code = 1;
  807. s->b_code = 1;
  808. return 0;
  809. }
  810. int MPV_encode_end(AVCodecContext *avctx)
  811. {
  812. MpegEncContext *s = avctx->priv_data;
  813. #ifdef STATS
  814. print_stats();
  815. #endif
  816. ff_rate_control_uninit(s);
  817. MPV_common_end(s);
  818. if (s->out_format == FMT_MJPEG)
  819. mjpeg_close(s);
  820. av_freep(&avctx->extradata);
  821. return 0;
  822. }
  823. #endif //CONFIG_ENCODERS
  824. void init_rl(RLTable *rl)
  825. {
  826. int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
  827. uint8_t index_run[MAX_RUN+1];
  828. int last, run, level, start, end, i;
  829. /* compute max_level[], max_run[] and index_run[] */
  830. for(last=0;last<2;last++) {
  831. if (last == 0) {
  832. start = 0;
  833. end = rl->last;
  834. } else {
  835. start = rl->last;
  836. end = rl->n;
  837. }
  838. memset(max_level, 0, MAX_RUN + 1);
  839. memset(max_run, 0, MAX_LEVEL + 1);
  840. memset(index_run, rl->n, MAX_RUN + 1);
  841. for(i=start;i<end;i++) {
  842. run = rl->table_run[i];
  843. level = rl->table_level[i];
  844. if (index_run[run] == rl->n)
  845. index_run[run] = i;
  846. if (level > max_level[run])
  847. max_level[run] = level;
  848. if (run > max_run[level])
  849. max_run[level] = run;
  850. }
  851. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  852. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  853. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  854. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  855. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  856. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  857. }
  858. }
  859. /* draw the edges of width 'w' of an image of size width, height */
  860. //FIXME check that this is ok for mpeg4 interlaced
  861. static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
  862. {
  863. uint8_t *ptr, *last_line;
  864. int i;
  865. last_line = buf + (height - 1) * wrap;
  866. for(i=0;i<w;i++) {
  867. /* top and bottom */
  868. memcpy(buf - (i + 1) * wrap, buf, width);
  869. memcpy(last_line + (i + 1) * wrap, last_line, width);
  870. }
  871. /* left and right */
  872. ptr = buf;
  873. for(i=0;i<height;i++) {
  874. memset(ptr - w, ptr[0], w);
  875. memset(ptr + width, ptr[width-1], w);
  876. ptr += wrap;
  877. }
  878. /* corners */
  879. for(i=0;i<w;i++) {
  880. memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
  881. memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
  882. memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
  883. memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
  884. }
  885. }
  886. int ff_find_unused_picture(MpegEncContext *s, int shared){
  887. int i;
  888. if(shared){
  889. for(i=0; i<MAX_PICTURE_COUNT; i++){
  890. if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
  891. }
  892. }else{
  893. for(i=0; i<MAX_PICTURE_COUNT; i++){
  894. if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME
  895. }
  896. for(i=0; i<MAX_PICTURE_COUNT; i++){
  897. if(s->picture[i].data[0]==NULL) return i;
  898. }
  899. }
  900. assert(0);
  901. return -1;
  902. }
  903. static void update_noise_reduction(MpegEncContext *s){
  904. int intra, i;
  905. for(intra=0; intra<2; intra++){
  906. if(s->dct_count[intra] > (1<<16)){
  907. for(i=0; i<64; i++){
  908. s->dct_error_sum[intra][i] >>=1;
  909. }
  910. s->dct_count[intra] >>= 1;
  911. }
  912. for(i=0; i<64; i++){
  913. s->dct_offset[intra][i]= (s->avctx->noise_reduction * s->dct_count[intra] + s->dct_error_sum[intra][i]/2) / (s->dct_error_sum[intra][i]+1);
  914. }
  915. }
  916. }
  917. /**
  918. * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
  919. */
  920. int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  921. {
  922. int i;
  923. AVFrame *pic;
  924. s->mb_skiped = 0;
  925. assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
  926. /* mark&release old frames */
  927. if (s->pict_type != B_TYPE && s->last_picture_ptr && s->last_picture_ptr->data[0]) {
  928. avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
  929. /* release forgotten pictures */
  930. /* if(mpeg124/h263) */
  931. if(!s->encoding){
  932. for(i=0; i<MAX_PICTURE_COUNT; i++){
  933. if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
  934. av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
  935. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  936. }
  937. }
  938. }
  939. }
  940. alloc:
  941. if(!s->encoding){
  942. /* release non refernce frames */
  943. for(i=0; i<MAX_PICTURE_COUNT; i++){
  944. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  945. s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
  946. }
  947. }
  948. if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)
  949. pic= (AVFrame*)s->current_picture_ptr; //we allready have a unused image (maybe it was set before reading the header)
  950. else{
  951. i= ff_find_unused_picture(s, 0);
  952. pic= (AVFrame*)&s->picture[i];
  953. }
  954. pic->reference= s->pict_type != B_TYPE ? 3 : 0;
  955. if(s->current_picture_ptr) //FIXME broken, we need a coded_picture_number in MpegEncContext
  956. pic->coded_picture_number= s->current_picture_ptr->coded_picture_number+1;
  957. if( alloc_picture(s, (Picture*)pic, 0) < 0)
  958. return -1;
  959. s->current_picture_ptr= (Picture*)pic;
  960. }
  961. s->current_picture_ptr->pict_type= s->pict_type;
  962. // if(s->flags && CODEC_FLAG_QSCALE)
  963. // s->current_picture_ptr->quality= s->new_picture_ptr->quality;
  964. s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
  965. copy_picture(&s->current_picture, s->current_picture_ptr);
  966. if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
  967. if (s->pict_type != B_TYPE) {
  968. s->last_picture_ptr= s->next_picture_ptr;
  969. s->next_picture_ptr= s->current_picture_ptr;
  970. }
  971. if(s->last_picture_ptr) copy_picture(&s->last_picture, s->last_picture_ptr);
  972. if(s->next_picture_ptr) copy_picture(&s->next_picture, s->next_picture_ptr);
  973. if(s->pict_type != I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL)){
  974. av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
  975. assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
  976. goto alloc;
  977. }
  978. assert(s->pict_type == I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0]));
  979. if(s->picture_structure!=PICT_FRAME){
  980. int i;
  981. for(i=0; i<4; i++){
  982. if(s->picture_structure == PICT_BOTTOM_FIELD){
  983. s->current_picture.data[i] += s->current_picture.linesize[i];
  984. }
  985. s->current_picture.linesize[i] *= 2;
  986. s->last_picture.linesize[i] *=2;
  987. s->next_picture.linesize[i] *=2;
  988. }
  989. }
  990. }
  991. s->hurry_up= s->avctx->hurry_up;
  992. s->error_resilience= avctx->error_resilience;
  993. /* set dequantizer, we cant do it during init as it might change for mpeg4
  994. and we cant do it in the header decode as init isnt called for mpeg4 there yet */
  995. if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO)
  996. s->dct_unquantize = s->dct_unquantize_mpeg2;
  997. else if(s->out_format == FMT_H263)
  998. s->dct_unquantize = s->dct_unquantize_h263;
  999. else
  1000. s->dct_unquantize = s->dct_unquantize_mpeg1;
  1001. if(s->dct_error_sum){
  1002. assert(s->avctx->noise_reduction && s->encoding);
  1003. update_noise_reduction(s);
  1004. }
  1005. #ifdef HAVE_XVMC
  1006. if(s->avctx->xvmc_acceleration)
  1007. return XVMC_field_start(s, avctx);
  1008. #endif
  1009. return 0;
  1010. }
  1011. /* generic function for encode/decode called after a frame has been coded/decoded */
  1012. void MPV_frame_end(MpegEncContext *s)
  1013. {
  1014. int i;
  1015. /* draw edge for correct motion prediction if outside */
  1016. #ifdef HAVE_XVMC
  1017. //just to make sure that all data is rendered.
  1018. if(s->avctx->xvmc_acceleration){
  1019. XVMC_field_end(s);
  1020. }else
  1021. #endif
  1022. if(s->unrestricted_mv && s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  1023. draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
  1024. draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  1025. draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
  1026. }
  1027. emms_c();
  1028. s->last_pict_type = s->pict_type;
  1029. if(s->pict_type!=B_TYPE){
  1030. s->last_non_b_pict_type= s->pict_type;
  1031. }
  1032. #if 0
  1033. /* copy back current_picture variables */
  1034. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1035. if(s->picture[i].data[0] == s->current_picture.data[0]){
  1036. s->picture[i]= s->current_picture;
  1037. break;
  1038. }
  1039. }
  1040. assert(i<MAX_PICTURE_COUNT);
  1041. #endif
  1042. if(s->encoding){
  1043. /* release non refernce frames */
  1044. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1045. if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  1046. s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
  1047. }
  1048. }
  1049. }
  1050. // clear copies, to avoid confusion
  1051. #if 0
  1052. memset(&s->last_picture, 0, sizeof(Picture));
  1053. memset(&s->next_picture, 0, sizeof(Picture));
  1054. memset(&s->current_picture, 0, sizeof(Picture));
  1055. #endif
  1056. }
  1057. /**
  1058. * draws an line from (ex, ey) -> (sx, sy).
  1059. * @param w width of the image
  1060. * @param h height of the image
  1061. * @param stride stride/linesize of the image
  1062. * @param color color of the arrow
  1063. */
  1064. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  1065. int t, x, y, f;
  1066. sx= clip(sx, 0, w-1);
  1067. sy= clip(sy, 0, h-1);
  1068. ex= clip(ex, 0, w-1);
  1069. ey= clip(ey, 0, h-1);
  1070. buf[sy*stride + sx]+= color;
  1071. if(ABS(ex - sx) > ABS(ey - sy)){
  1072. if(sx > ex){
  1073. t=sx; sx=ex; ex=t;
  1074. t=sy; sy=ey; ey=t;
  1075. }
  1076. buf+= sx + sy*stride;
  1077. ex-= sx;
  1078. f= ((ey-sy)<<16)/ex;
  1079. for(x= 0; x <= ex; x++){
  1080. y= ((x*f) + (1<<15))>>16;
  1081. buf[y*stride + x]+= color;
  1082. }
  1083. }else{
  1084. if(sy > ey){
  1085. t=sx; sx=ex; ex=t;
  1086. t=sy; sy=ey; ey=t;
  1087. }
  1088. buf+= sx + sy*stride;
  1089. ey-= sy;
  1090. if(ey) f= ((ex-sx)<<16)/ey;
  1091. else f= 0;
  1092. for(y= 0; y <= ey; y++){
  1093. x= ((y*f) + (1<<15))>>16;
  1094. buf[y*stride + x]+= color;
  1095. }
  1096. }
  1097. }
  1098. /**
  1099. * draws an arrow from (ex, ey) -> (sx, sy).
  1100. * @param w width of the image
  1101. * @param h height of the image
  1102. * @param stride stride/linesize of the image
  1103. * @param color color of the arrow
  1104. */
  1105. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  1106. int dx,dy;
  1107. sx= clip(sx, -100, w+100);
  1108. sy= clip(sy, -100, h+100);
  1109. ex= clip(ex, -100, w+100);
  1110. ey= clip(ey, -100, h+100);
  1111. dx= ex - sx;
  1112. dy= ey - sy;
  1113. if(dx*dx + dy*dy > 3*3){
  1114. int rx= dx + dy;
  1115. int ry= -dx + dy;
  1116. int length= ff_sqrt((rx*rx + ry*ry)<<8);
  1117. //FIXME subpixel accuracy
  1118. rx= ROUNDED_DIV(rx*3<<4, length);
  1119. ry= ROUNDED_DIV(ry*3<<4, length);
  1120. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1121. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1122. }
  1123. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1124. }
  1125. /**
  1126. * prints debuging info for the given picture.
  1127. */
  1128. void ff_print_debug_info(MpegEncContext *s, Picture *pict){
  1129. if(!pict || !pict->mb_type) return;
  1130. if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
  1131. int x,y;
  1132. for(y=0; y<s->mb_height; y++){
  1133. for(x=0; x<s->mb_width; x++){
  1134. if(s->avctx->debug&FF_DEBUG_SKIP){
  1135. int count= s->mbskip_table[x + y*s->mb_stride];
  1136. if(count>9) count=9;
  1137. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1138. }
  1139. if(s->avctx->debug&FF_DEBUG_QP){
  1140. av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
  1141. }
  1142. if(s->avctx->debug&FF_DEBUG_MB_TYPE){
  1143. int mb_type= pict->mb_type[x + y*s->mb_stride];
  1144. //Type & MV direction
  1145. if(IS_PCM(mb_type))
  1146. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1147. else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1148. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1149. else if(IS_INTRA4x4(mb_type))
  1150. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1151. else if(IS_INTRA16x16(mb_type))
  1152. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1153. else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1154. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1155. else if(IS_DIRECT(mb_type))
  1156. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1157. else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
  1158. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1159. else if(IS_GMC(mb_type))
  1160. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1161. else if(IS_SKIP(mb_type))
  1162. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1163. else if(!USES_LIST(mb_type, 1))
  1164. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1165. else if(!USES_LIST(mb_type, 0))
  1166. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1167. else{
  1168. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1169. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1170. }
  1171. //segmentation
  1172. if(IS_8X8(mb_type))
  1173. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1174. else if(IS_16X8(mb_type))
  1175. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1176. else if(IS_8X16(mb_type))
  1177. av_log(s->avctx, AV_LOG_DEBUG, "¦");
  1178. else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
  1179. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1180. else
  1181. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1182. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264)
  1183. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1184. else
  1185. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1186. }
  1187. // av_log(s->avctx, AV_LOG_DEBUG, " ");
  1188. }
  1189. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1190. }
  1191. }
  1192. if((s->avctx->debug&FF_DEBUG_VIS_MV) && s->motion_val){
  1193. const int shift= 1 + s->quarter_sample;
  1194. int mb_y;
  1195. uint8_t *ptr= pict->data[0];
  1196. s->low_delay=0; //needed to see the vectors without trashing the buffers
  1197. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  1198. int mb_x;
  1199. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  1200. const int mb_index= mb_x + mb_y*s->mb_stride;
  1201. if(IS_8X8(s->current_picture.mb_type[mb_index])){
  1202. int i;
  1203. for(i=0; i<4; i++){
  1204. int sx= mb_x*16 + 4 + 8*(i&1);
  1205. int sy= mb_y*16 + 4 + 8*(i>>1);
  1206. int xy= 1 + mb_x*2 + (i&1) + (mb_y*2 + 1 + (i>>1))*(s->mb_width*2 + 2);
  1207. int mx= (s->motion_val[xy][0]>>shift) + sx;
  1208. int my= (s->motion_val[xy][1]>>shift) + sy;
  1209. draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
  1210. }
  1211. }else if(IS_16X8(s->current_picture.mb_type[mb_index])){
  1212. int i;
  1213. for(i=0; i<2; i++){
  1214. int sx=mb_x*16 + 8;
  1215. int sy=mb_y*16 + 4 + 8*i;
  1216. int xy=1 + mb_x*2 + (mb_y*2 + 1 + i)*(s->mb_width*2 + 2);
  1217. int mx=(s->motion_val[xy][0]>>shift) + sx;
  1218. int my=(s->motion_val[xy][1]>>shift) + sy;
  1219. draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
  1220. }
  1221. }else{
  1222. int sx= mb_x*16 + 8;
  1223. int sy= mb_y*16 + 8;
  1224. int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
  1225. int mx= (s->motion_val[xy][0]>>shift) + sx;
  1226. int my= (s->motion_val[xy][1]>>shift) + sy;
  1227. draw_arrow(ptr, sx, sy, mx, my, s->width, s->height, s->linesize, 100);
  1228. }
  1229. s->mbskip_table[mb_index]=0;
  1230. }
  1231. }
  1232. }
  1233. }
  1234. #ifdef CONFIG_ENCODERS
  1235. static int get_sae(uint8_t *src, int ref, int stride){
  1236. int x,y;
  1237. int acc=0;
  1238. for(y=0; y<16; y++){
  1239. for(x=0; x<16; x++){
  1240. acc+= ABS(src[x+y*stride] - ref);
  1241. }
  1242. }
  1243. return acc;
  1244. }
  1245. static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
  1246. int x, y, w, h;
  1247. int acc=0;
  1248. w= s->width &~15;
  1249. h= s->height&~15;
  1250. for(y=0; y<h; y+=16){
  1251. for(x=0; x<w; x+=16){
  1252. int offset= x + y*stride;
  1253. int sad = s->dsp.pix_abs16x16(src + offset, ref + offset, stride);
  1254. int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
  1255. int sae = get_sae(src + offset, mean, stride);
  1256. acc+= sae + 500 < sad;
  1257. }
  1258. }
  1259. return acc;
  1260. }
  1261. static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
  1262. AVFrame *pic=NULL;
  1263. int i;
  1264. const int encoding_delay= s->max_b_frames;
  1265. int direct=1;
  1266. if(pic_arg){
  1267. if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
  1268. if(pic_arg->linesize[0] != s->linesize) direct=0;
  1269. if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
  1270. if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
  1271. // av_log(AV_LOG_DEBUG, "%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
  1272. if(direct){
  1273. i= ff_find_unused_picture(s, 1);
  1274. pic= (AVFrame*)&s->picture[i];
  1275. pic->reference= 3;
  1276. for(i=0; i<4; i++){
  1277. pic->data[i]= pic_arg->data[i];
  1278. pic->linesize[i]= pic_arg->linesize[i];
  1279. }
  1280. alloc_picture(s, (Picture*)pic, 1);
  1281. }else{
  1282. int offset= 16;
  1283. i= ff_find_unused_picture(s, 0);
  1284. pic= (AVFrame*)&s->picture[i];
  1285. pic->reference= 3;
  1286. alloc_picture(s, (Picture*)pic, 0);
  1287. if( pic->data[0] + offset == pic_arg->data[0]
  1288. && pic->data[1] + offset == pic_arg->data[1]
  1289. && pic->data[2] + offset == pic_arg->data[2]){
  1290. // empty
  1291. }else{
  1292. int h_chroma_shift, v_chroma_shift;
  1293. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1294. for(i=0; i<3; i++){
  1295. int src_stride= pic_arg->linesize[i];
  1296. int dst_stride= i ? s->uvlinesize : s->linesize;
  1297. int h_shift= i ? h_chroma_shift : 0;
  1298. int v_shift= i ? v_chroma_shift : 0;
  1299. int w= s->width >>h_shift;
  1300. int h= s->height>>v_shift;
  1301. uint8_t *src= pic_arg->data[i];
  1302. uint8_t *dst= pic->data[i] + offset;
  1303. if(src_stride==dst_stride)
  1304. memcpy(dst, src, src_stride*h);
  1305. else{
  1306. while(h--){
  1307. memcpy(dst, src, w);
  1308. dst += dst_stride;
  1309. src += src_stride;
  1310. }
  1311. }
  1312. }
  1313. }
  1314. }
  1315. pic->quality= pic_arg->quality;
  1316. pic->pict_type= pic_arg->pict_type;
  1317. pic->pts = pic_arg->pts;
  1318. if(s->input_picture[encoding_delay])
  1319. pic->display_picture_number= s->input_picture[encoding_delay]->display_picture_number + 1;
  1320. }
  1321. /* shift buffer entries */
  1322. for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
  1323. s->input_picture[i-1]= s->input_picture[i];
  1324. s->input_picture[encoding_delay]= (Picture*)pic;
  1325. return 0;
  1326. }
  1327. static void select_input_picture(MpegEncContext *s){
  1328. int i;
  1329. int coded_pic_num=0;
  1330. if(s->reordered_input_picture[0])
  1331. coded_pic_num= s->reordered_input_picture[0]->coded_picture_number + 1;
  1332. for(i=1; i<MAX_PICTURE_COUNT; i++)
  1333. s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
  1334. s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
  1335. /* set next picture types & ordering */
  1336. if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
  1337. if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){
  1338. s->reordered_input_picture[0]= s->input_picture[0];
  1339. s->reordered_input_picture[0]->pict_type= I_TYPE;
  1340. s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
  1341. }else{
  1342. int b_frames;
  1343. if(s->flags&CODEC_FLAG_PASS2){
  1344. for(i=0; i<s->max_b_frames+1; i++){
  1345. int pict_num= s->input_picture[0]->display_picture_number + i;
  1346. int pict_type= s->rc_context.entry[pict_num].new_pict_type;
  1347. s->input_picture[i]->pict_type= pict_type;
  1348. if(i + 1 >= s->rc_context.num_entries) break;
  1349. }
  1350. }
  1351. if(s->input_picture[0]->pict_type){
  1352. /* user selected pict_type */
  1353. for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){
  1354. if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;
  1355. }
  1356. if(b_frames > s->max_b_frames){
  1357. av_log(s->avctx, AV_LOG_ERROR, "warning, too many bframes in a row\n");
  1358. b_frames = s->max_b_frames;
  1359. }
  1360. }else if(s->b_frame_strategy==0){
  1361. b_frames= s->max_b_frames;
  1362. while(b_frames && !s->input_picture[b_frames]) b_frames--;
  1363. }else if(s->b_frame_strategy==1){
  1364. for(i=1; i<s->max_b_frames+1; i++){
  1365. if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){
  1366. s->input_picture[i]->b_frame_score=
  1367. get_intra_count(s, s->input_picture[i ]->data[0],
  1368. s->input_picture[i-1]->data[0], s->linesize) + 1;
  1369. }
  1370. }
  1371. for(i=0; i<s->max_b_frames; i++){
  1372. if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
  1373. }
  1374. b_frames= FFMAX(0, i-1);
  1375. /* reset scores */
  1376. for(i=0; i<b_frames+1; i++){
  1377. s->input_picture[i]->b_frame_score=0;
  1378. }
  1379. }else{
  1380. av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
  1381. b_frames=0;
  1382. }
  1383. emms_c();
  1384. //static int b_count=0;
  1385. //b_count+= b_frames;
  1386. //av_log(s->avctx, AV_LOG_DEBUG, "b_frames: %d\n", b_count);
  1387. s->reordered_input_picture[0]= s->input_picture[b_frames];
  1388. if( s->picture_in_gop_number + b_frames >= s->gop_size
  1389. || s->reordered_input_picture[0]->pict_type== I_TYPE)
  1390. s->reordered_input_picture[0]->pict_type= I_TYPE;
  1391. else
  1392. s->reordered_input_picture[0]->pict_type= P_TYPE;
  1393. s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
  1394. for(i=0; i<b_frames; i++){
  1395. coded_pic_num++;
  1396. s->reordered_input_picture[i+1]= s->input_picture[i];
  1397. s->reordered_input_picture[i+1]->pict_type= B_TYPE;
  1398. s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num;
  1399. }
  1400. }
  1401. }
  1402. if(s->reordered_input_picture[0]){
  1403. s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE ? 3 : 0;
  1404. copy_picture(&s->new_picture, s->reordered_input_picture[0]);
  1405. if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
  1406. // input is a shared pix, so we cant modifiy it -> alloc a new one & ensure that the shared one is reuseable
  1407. int i= ff_find_unused_picture(s, 0);
  1408. Picture *pic= &s->picture[i];
  1409. /* mark us unused / free shared pic */
  1410. for(i=0; i<4; i++)
  1411. s->reordered_input_picture[0]->data[i]= NULL;
  1412. s->reordered_input_picture[0]->type= 0;
  1413. //FIXME bad, copy * except
  1414. pic->pict_type = s->reordered_input_picture[0]->pict_type;
  1415. pic->quality = s->reordered_input_picture[0]->quality;
  1416. pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number;
  1417. pic->reference = s->reordered_input_picture[0]->reference;
  1418. pic->pts = s->reordered_input_picture[0]->pts;
  1419. alloc_picture(s, pic, 0);
  1420. s->current_picture_ptr= pic;
  1421. }else{
  1422. // input is not a shared pix -> reuse buffer for current_pix
  1423. assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
  1424. || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
  1425. s->current_picture_ptr= s->reordered_input_picture[0];
  1426. for(i=0; i<4; i++){
  1427. s->new_picture.data[i]+=16;
  1428. }
  1429. }
  1430. copy_picture(&s->current_picture, s->current_picture_ptr);
  1431. s->picture_number= s->new_picture.display_picture_number;
  1432. //printf("dpn:%d\n", s->picture_number);
  1433. }else{
  1434. memset(&s->new_picture, 0, sizeof(Picture));
  1435. }
  1436. }
  1437. int MPV_encode_picture(AVCodecContext *avctx,
  1438. unsigned char *buf, int buf_size, void *data)
  1439. {
  1440. MpegEncContext *s = avctx->priv_data;
  1441. AVFrame *pic_arg = data;
  1442. int i;
  1443. if(avctx->pix_fmt != PIX_FMT_YUV420P){
  1444. av_log(avctx, AV_LOG_ERROR, "this codec supports only YUV420P\n");
  1445. return -1;
  1446. }
  1447. init_put_bits(&s->pb, buf, buf_size);
  1448. s->picture_in_gop_number++;
  1449. load_input_picture(s, pic_arg);
  1450. select_input_picture(s);
  1451. /* output? */
  1452. if(s->new_picture.data[0]){
  1453. s->pict_type= s->new_picture.pict_type;
  1454. //emms_c();
  1455. //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
  1456. MPV_frame_start(s, avctx);
  1457. encode_picture(s, s->picture_number);
  1458. avctx->real_pict_num = s->picture_number;
  1459. avctx->header_bits = s->header_bits;
  1460. avctx->mv_bits = s->mv_bits;
  1461. avctx->misc_bits = s->misc_bits;
  1462. avctx->i_tex_bits = s->i_tex_bits;
  1463. avctx->p_tex_bits = s->p_tex_bits;
  1464. avctx->i_count = s->i_count;
  1465. avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
  1466. avctx->skip_count = s->skip_count;
  1467. MPV_frame_end(s);
  1468. if (s->out_format == FMT_MJPEG)
  1469. mjpeg_picture_trailer(s);
  1470. if(s->flags&CODEC_FLAG_PASS1)
  1471. ff_write_pass1_stats(s);
  1472. for(i=0; i<4; i++){
  1473. avctx->error[i] += s->current_picture_ptr->error[i];
  1474. }
  1475. }
  1476. s->input_picture_number++;
  1477. flush_put_bits(&s->pb);
  1478. s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
  1479. s->total_bits += s->frame_bits;
  1480. avctx->frame_bits = s->frame_bits;
  1481. return pbBufPtr(&s->pb) - s->pb.buf;
  1482. }
  1483. #endif //CONFIG_ENCODERS
  1484. static inline void gmc1_motion(MpegEncContext *s,
  1485. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1486. int dest_offset,
  1487. uint8_t **ref_picture, int src_offset)
  1488. {
  1489. uint8_t *ptr;
  1490. int offset, src_x, src_y, linesize, uvlinesize;
  1491. int motion_x, motion_y;
  1492. int emu=0;
  1493. motion_x= s->sprite_offset[0][0];
  1494. motion_y= s->sprite_offset[0][1];
  1495. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  1496. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  1497. motion_x<<=(3-s->sprite_warping_accuracy);
  1498. motion_y<<=(3-s->sprite_warping_accuracy);
  1499. src_x = clip(src_x, -16, s->width);
  1500. if (src_x == s->width)
  1501. motion_x =0;
  1502. src_y = clip(src_y, -16, s->height);
  1503. if (src_y == s->height)
  1504. motion_y =0;
  1505. linesize = s->linesize;
  1506. uvlinesize = s->uvlinesize;
  1507. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  1508. dest_y+=dest_offset;
  1509. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1510. if( (unsigned)src_x >= s->h_edge_pos - 17
  1511. || (unsigned)src_y >= s->v_edge_pos - 17){
  1512. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  1513. ptr= s->edge_emu_buffer;
  1514. }
  1515. }
  1516. if((motion_x|motion_y)&7){
  1517. s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1518. s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1519. }else{
  1520. int dxy;
  1521. dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
  1522. if (s->no_rounding){
  1523. s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
  1524. }else{
  1525. s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
  1526. }
  1527. }
  1528. if(s->flags&CODEC_FLAG_GRAY) return;
  1529. motion_x= s->sprite_offset[1][0];
  1530. motion_y= s->sprite_offset[1][1];
  1531. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  1532. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  1533. motion_x<<=(3-s->sprite_warping_accuracy);
  1534. motion_y<<=(3-s->sprite_warping_accuracy);
  1535. src_x = clip(src_x, -8, s->width>>1);
  1536. if (src_x == s->width>>1)
  1537. motion_x =0;
  1538. src_y = clip(src_y, -8, s->height>>1);
  1539. if (src_y == s->height>>1)
  1540. motion_y =0;
  1541. offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
  1542. ptr = ref_picture[1] + offset;
  1543. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1544. if( (unsigned)src_x >= (s->h_edge_pos>>1) - 9
  1545. || (unsigned)src_y >= (s->v_edge_pos>>1) - 9){
  1546. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1547. ptr= s->edge_emu_buffer;
  1548. emu=1;
  1549. }
  1550. }
  1551. s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1552. ptr = ref_picture[2] + offset;
  1553. if(emu){
  1554. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1555. ptr= s->edge_emu_buffer;
  1556. }
  1557. s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1558. return;
  1559. }
  1560. static inline void gmc_motion(MpegEncContext *s,
  1561. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1562. int dest_offset,
  1563. uint8_t **ref_picture, int src_offset)
  1564. {
  1565. uint8_t *ptr;
  1566. int linesize, uvlinesize;
  1567. const int a= s->sprite_warping_accuracy;
  1568. int ox, oy;
  1569. linesize = s->linesize;
  1570. uvlinesize = s->uvlinesize;
  1571. ptr = ref_picture[0] + src_offset;
  1572. dest_y+=dest_offset;
  1573. ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
  1574. oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
  1575. s->dsp.gmc(dest_y, ptr, linesize, 16,
  1576. ox,
  1577. oy,
  1578. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1579. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1580. a+1, (1<<(2*a+1)) - s->no_rounding,
  1581. s->h_edge_pos, s->v_edge_pos);
  1582. s->dsp.gmc(dest_y+8, ptr, linesize, 16,
  1583. ox + s->sprite_delta[0][0]*8,
  1584. oy + s->sprite_delta[1][0]*8,
  1585. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1586. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1587. a+1, (1<<(2*a+1)) - s->no_rounding,
  1588. s->h_edge_pos, s->v_edge_pos);
  1589. if(s->flags&CODEC_FLAG_GRAY) return;
  1590. dest_cb+=dest_offset>>1;
  1591. dest_cr+=dest_offset>>1;
  1592. ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
  1593. oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
  1594. ptr = ref_picture[1] + (src_offset>>1);
  1595. s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
  1596. ox,
  1597. oy,
  1598. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1599. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1600. a+1, (1<<(2*a+1)) - s->no_rounding,
  1601. s->h_edge_pos>>1, s->v_edge_pos>>1);
  1602. ptr = ref_picture[2] + (src_offset>>1);
  1603. s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
  1604. ox,
  1605. oy,
  1606. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1607. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1608. a+1, (1<<(2*a+1)) - s->no_rounding,
  1609. s->h_edge_pos>>1, s->v_edge_pos>>1);
  1610. }
  1611. /**
  1612. * Copies a rectangular area of samples to a temporary buffer and replicates the boarder samples.
  1613. * @param buf destination buffer
  1614. * @param src source buffer
  1615. * @param linesize number of bytes between 2 vertically adjacent samples in both the source and destination buffers
  1616. * @param block_w width of block
  1617. * @param block_h height of block
  1618. * @param src_x x coordinate of the top left sample of the block in the source buffer
  1619. * @param src_y y coordinate of the top left sample of the block in the source buffer
  1620. * @param w width of the source buffer
  1621. * @param h height of the source buffer
  1622. */
  1623. void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h,
  1624. int src_x, int src_y, int w, int h){
  1625. int x, y;
  1626. int start_y, start_x, end_y, end_x;
  1627. if(src_y>= h){
  1628. src+= (h-1-src_y)*linesize;
  1629. src_y=h-1;
  1630. }else if(src_y<=-block_h){
  1631. src+= (1-block_h-src_y)*linesize;
  1632. src_y=1-block_h;
  1633. }
  1634. if(src_x>= w){
  1635. src+= (w-1-src_x);
  1636. src_x=w-1;
  1637. }else if(src_x<=-block_w){
  1638. src+= (1-block_w-src_x);
  1639. src_x=1-block_w;
  1640. }
  1641. start_y= FFMAX(0, -src_y);
  1642. start_x= FFMAX(0, -src_x);
  1643. end_y= FFMIN(block_h, h-src_y);
  1644. end_x= FFMIN(block_w, w-src_x);
  1645. // copy existing part
  1646. for(y=start_y; y<end_y; y++){
  1647. for(x=start_x; x<end_x; x++){
  1648. buf[x + y*linesize]= src[x + y*linesize];
  1649. }
  1650. }
  1651. //top
  1652. for(y=0; y<start_y; y++){
  1653. for(x=start_x; x<end_x; x++){
  1654. buf[x + y*linesize]= buf[x + start_y*linesize];
  1655. }
  1656. }
  1657. //bottom
  1658. for(y=end_y; y<block_h; y++){
  1659. for(x=start_x; x<end_x; x++){
  1660. buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
  1661. }
  1662. }
  1663. for(y=0; y<block_h; y++){
  1664. //left
  1665. for(x=0; x<start_x; x++){
  1666. buf[x + y*linesize]= buf[start_x + y*linesize];
  1667. }
  1668. //right
  1669. for(x=end_x; x<block_w; x++){
  1670. buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
  1671. }
  1672. }
  1673. }
  1674. static inline int hpel_motion(MpegEncContext *s,
  1675. uint8_t *dest, uint8_t *src,
  1676. int src_x, int src_y,
  1677. int width, int height, int stride,
  1678. int h_edge_pos, int v_edge_pos,
  1679. int w, int h, op_pixels_func *pix_op,
  1680. int motion_x, int motion_y)
  1681. {
  1682. int dxy;
  1683. int emu=0;
  1684. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1685. src_x += motion_x >> 1;
  1686. src_y += motion_y >> 1;
  1687. /* WARNING: do no forget half pels */
  1688. src_x = clip(src_x, -16, width); //FIXME unneeded for emu?
  1689. if (src_x == width)
  1690. dxy &= ~1;
  1691. src_y = clip(src_y, -16, height);
  1692. if (src_y == height)
  1693. dxy &= ~2;
  1694. src += src_y * stride + src_x;
  1695. if(s->unrestricted_mv && (s->flags&CODEC_FLAG_EMU_EDGE)){
  1696. if( (unsigned)src_x > h_edge_pos - (motion_x&1) - w
  1697. || (unsigned)src_y > v_edge_pos - (motion_y&1) - h){
  1698. ff_emulated_edge_mc(s->edge_emu_buffer, src, stride, w+1, h+1,
  1699. src_x, src_y, h_edge_pos, v_edge_pos);
  1700. src= s->edge_emu_buffer;
  1701. emu=1;
  1702. }
  1703. }
  1704. pix_op[dxy](dest, src, stride, h);
  1705. return emu;
  1706. }
  1707. /* apply one mpeg motion vector to the three components */
  1708. static inline void mpeg_motion(MpegEncContext *s,
  1709. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1710. int dest_offset,
  1711. uint8_t **ref_picture, int src_offset,
  1712. int field_based, op_pixels_func (*pix_op)[4],
  1713. int motion_x, int motion_y, int h)
  1714. {
  1715. uint8_t *ptr;
  1716. int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, uvlinesize;
  1717. int emu=0;
  1718. #if 0
  1719. if(s->quarter_sample)
  1720. {
  1721. motion_x>>=1;
  1722. motion_y>>=1;
  1723. }
  1724. #endif
  1725. height = s->height >> field_based;
  1726. v_edge_pos = s->v_edge_pos >> field_based;
  1727. uvlinesize = s->current_picture.linesize[1] << field_based;
  1728. emu= hpel_motion(s,
  1729. dest_y + dest_offset, ref_picture[0] + src_offset,
  1730. s->mb_x * 16, s->mb_y * (16 >> field_based),
  1731. s->width, height, s->current_picture.linesize[0] << field_based,
  1732. s->h_edge_pos, v_edge_pos,
  1733. 16, h, pix_op[0],
  1734. motion_x, motion_y);
  1735. if(s->flags&CODEC_FLAG_GRAY) return;
  1736. if (s->out_format == FMT_H263) {
  1737. dxy = 0;
  1738. if ((motion_x & 3) != 0)
  1739. dxy |= 1;
  1740. if ((motion_y & 3) != 0)
  1741. dxy |= 2;
  1742. mx = motion_x >> 2;
  1743. my = motion_y >> 2;
  1744. } else {
  1745. mx = motion_x / 2;
  1746. my = motion_y / 2;
  1747. dxy = ((my & 1) << 1) | (mx & 1);
  1748. mx >>= 1;
  1749. my >>= 1;
  1750. }
  1751. src_x = s->mb_x * 8 + mx;
  1752. src_y = s->mb_y * (8 >> field_based) + my;
  1753. src_x = clip(src_x, -8, s->width >> 1);
  1754. if (src_x == (s->width >> 1))
  1755. dxy &= ~1;
  1756. src_y = clip(src_y, -8, height >> 1);
  1757. if (src_y == (height >> 1))
  1758. dxy &= ~2;
  1759. offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
  1760. ptr = ref_picture[1] + offset;
  1761. if(emu){
  1762. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
  1763. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1764. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1765. }
  1766. pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1767. ptr = ref_picture[2] + offset;
  1768. if(emu){
  1769. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
  1770. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1771. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1772. }
  1773. pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1774. }
  1775. //FIXME move to dsputil, avg variant, 16x16 version
  1776. static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
  1777. int x;
  1778. uint8_t * const top = src[1];
  1779. uint8_t * const left = src[2];
  1780. uint8_t * const mid = src[0];
  1781. uint8_t * const right = src[3];
  1782. uint8_t * const bottom= src[4];
  1783. #define OBMC_FILTER(x, t, l, m, r, b)\
  1784. dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
  1785. #define OBMC_FILTER4(x, t, l, m, r, b)\
  1786. OBMC_FILTER(x , t, l, m, r, b);\
  1787. OBMC_FILTER(x+1 , t, l, m, r, b);\
  1788. OBMC_FILTER(x +stride, t, l, m, r, b);\
  1789. OBMC_FILTER(x+1+stride, t, l, m, r, b);
  1790. x=0;
  1791. OBMC_FILTER (x , 2, 2, 4, 0, 0);
  1792. OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
  1793. OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
  1794. OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
  1795. OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
  1796. OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
  1797. x+= stride;
  1798. OBMC_FILTER (x , 1, 2, 5, 0, 0);
  1799. OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
  1800. OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
  1801. OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
  1802. x+= stride;
  1803. OBMC_FILTER4(x , 1, 2, 5, 0, 0);
  1804. OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
  1805. OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
  1806. OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
  1807. x+= 2*stride;
  1808. OBMC_FILTER4(x , 0, 2, 5, 0, 1);
  1809. OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
  1810. OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
  1811. OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
  1812. x+= 2*stride;
  1813. OBMC_FILTER (x , 0, 2, 5, 0, 1);
  1814. OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
  1815. OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
  1816. OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
  1817. OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
  1818. OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
  1819. x+= stride;
  1820. OBMC_FILTER (x , 0, 2, 4, 0, 2);
  1821. OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
  1822. OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
  1823. OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
  1824. }
  1825. /* obmc for 1 8x8 luma block */
  1826. static inline void obmc_motion(MpegEncContext *s,
  1827. uint8_t *dest, uint8_t *src,
  1828. int src_x, int src_y,
  1829. op_pixels_func *pix_op,
  1830. int16_t mv[5][2]/* mid top left right bottom*/)
  1831. #define MID 0
  1832. {
  1833. int i;
  1834. uint8_t *ptr[5];
  1835. assert(s->quarter_sample==0);
  1836. for(i=0; i<5; i++){
  1837. if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
  1838. ptr[i]= ptr[MID];
  1839. }else{
  1840. ptr[i]= s->edge_emu_buffer + 16 + 8*(i&1) + s->linesize*8*(i>>1);
  1841. hpel_motion(s, ptr[i], src,
  1842. src_x, src_y,
  1843. s->width, s->height, s->linesize,
  1844. s->h_edge_pos, s->v_edge_pos,
  1845. 8, 8, pix_op,
  1846. mv[i][0], mv[i][1]);
  1847. }
  1848. }
  1849. put_obmc(dest, ptr, s->linesize);
  1850. }
  1851. static inline void qpel_motion(MpegEncContext *s,
  1852. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1853. int dest_offset,
  1854. uint8_t **ref_picture, int src_offset,
  1855. int field_based, op_pixels_func (*pix_op)[4],
  1856. qpel_mc_func (*qpix_op)[16],
  1857. int motion_x, int motion_y, int h)
  1858. {
  1859. uint8_t *ptr;
  1860. int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
  1861. int emu=0;
  1862. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  1863. src_x = s->mb_x * 16 + (motion_x >> 2);
  1864. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  1865. height = s->height >> field_based;
  1866. v_edge_pos = s->v_edge_pos >> field_based;
  1867. src_x = clip(src_x, -16, s->width);
  1868. if (src_x == s->width)
  1869. dxy &= ~3;
  1870. src_y = clip(src_y, -16, height);
  1871. if (src_y == height)
  1872. dxy &= ~12;
  1873. linesize = s->linesize << field_based;
  1874. uvlinesize = s->uvlinesize << field_based;
  1875. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  1876. dest_y += dest_offset;
  1877. //printf("%d %d %d\n", src_x, src_y, dxy);
  1878. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1879. if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 16
  1880. || (unsigned)src_y > v_edge_pos - (motion_y&3) - h ){
  1881. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - src_offset, s->linesize, 17, 17+field_based,
  1882. src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
  1883. ptr= s->edge_emu_buffer + src_offset;
  1884. emu=1;
  1885. }
  1886. }
  1887. if(!field_based)
  1888. qpix_op[0][dxy](dest_y, ptr, linesize);
  1889. else{
  1890. //damn interlaced mode
  1891. //FIXME boundary mirroring is not exactly correct here
  1892. qpix_op[1][dxy](dest_y , ptr , linesize);
  1893. qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
  1894. }
  1895. if(s->flags&CODEC_FLAG_GRAY) return;
  1896. if(field_based){
  1897. mx= motion_x/2;
  1898. my= motion_y>>1;
  1899. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
  1900. static const int rtab[8]= {0,0,1,1,0,0,0,1};
  1901. mx= (motion_x>>1) + rtab[motion_x&7];
  1902. my= (motion_y>>1) + rtab[motion_y&7];
  1903. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
  1904. mx= (motion_x>>1)|(motion_x&1);
  1905. my= (motion_y>>1)|(motion_y&1);
  1906. }else{
  1907. mx= motion_x/2;
  1908. my= motion_y/2;
  1909. }
  1910. mx= (mx>>1)|(mx&1);
  1911. my= (my>>1)|(my&1);
  1912. dxy= (mx&1) | ((my&1)<<1);
  1913. mx>>=1;
  1914. my>>=1;
  1915. src_x = s->mb_x * 8 + mx;
  1916. src_y = s->mb_y * (8 >> field_based) + my;
  1917. src_x = clip(src_x, -8, s->width >> 1);
  1918. if (src_x == (s->width >> 1))
  1919. dxy &= ~1;
  1920. src_y = clip(src_y, -8, height >> 1);
  1921. if (src_y == (height >> 1))
  1922. dxy &= ~2;
  1923. offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
  1924. ptr = ref_picture[1] + offset;
  1925. if(emu){
  1926. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
  1927. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1928. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1929. }
  1930. pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1931. ptr = ref_picture[2] + offset;
  1932. if(emu){
  1933. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
  1934. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1935. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1936. }
  1937. pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1938. }
  1939. inline int ff_h263_round_chroma(int x){
  1940. if (x >= 0)
  1941. return (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
  1942. else {
  1943. x = -x;
  1944. return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
  1945. }
  1946. }
  1947. /**
  1948. * h263 chorma 4mv motion compensation.
  1949. */
  1950. static inline void chroma_4mv_motion(MpegEncContext *s,
  1951. uint8_t *dest_cb, uint8_t *dest_cr,
  1952. uint8_t **ref_picture,
  1953. op_pixels_func *pix_op,
  1954. int mx, int my){
  1955. int dxy, emu=0, src_x, src_y, offset;
  1956. uint8_t *ptr;
  1957. /* In case of 8X8, we construct a single chroma motion vector
  1958. with a special rounding */
  1959. mx= ff_h263_round_chroma(mx);
  1960. my= ff_h263_round_chroma(my);
  1961. dxy = ((my & 1) << 1) | (mx & 1);
  1962. mx >>= 1;
  1963. my >>= 1;
  1964. src_x = s->mb_x * 8 + mx;
  1965. src_y = s->mb_y * 8 + my;
  1966. src_x = clip(src_x, -8, s->width/2);
  1967. if (src_x == s->width/2)
  1968. dxy &= ~1;
  1969. src_y = clip(src_y, -8, s->height/2);
  1970. if (src_y == s->height/2)
  1971. dxy &= ~2;
  1972. offset = (src_y * (s->uvlinesize)) + src_x;
  1973. ptr = ref_picture[1] + offset;
  1974. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1975. if( (unsigned)src_x > (s->h_edge_pos>>1) - (dxy &1) - 8
  1976. || (unsigned)src_y > (s->v_edge_pos>>1) - (dxy>>1) - 8){
  1977. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1978. ptr= s->edge_emu_buffer;
  1979. emu=1;
  1980. }
  1981. }
  1982. pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
  1983. ptr = ref_picture[2] + offset;
  1984. if(emu){
  1985. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1986. ptr= s->edge_emu_buffer;
  1987. }
  1988. pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
  1989. }
  1990. /**
  1991. * motion compesation of a single macroblock
  1992. * @param s context
  1993. * @param dest_y luma destination pointer
  1994. * @param dest_cb chroma cb/u destination pointer
  1995. * @param dest_cr chroma cr/v destination pointer
  1996. * @param dir direction (0->forward, 1->backward)
  1997. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  1998. * @param pic_op halfpel motion compensation function (average or put normally)
  1999. * @param pic_op qpel motion compensation function (average or put normally)
  2000. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  2001. */
  2002. static inline void MPV_motion(MpegEncContext *s,
  2003. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2004. int dir, uint8_t **ref_picture,
  2005. op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
  2006. {
  2007. int dxy, mx, my, src_x, src_y, motion_x, motion_y;
  2008. int mb_x, mb_y, i;
  2009. uint8_t *ptr, *dest;
  2010. mb_x = s->mb_x;
  2011. mb_y = s->mb_y;
  2012. if(s->obmc){
  2013. int16_t mv_cache[4][4][2];
  2014. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  2015. const int mot_stride= s->mb_width*2 + 2;
  2016. const int mot_xy= 1 + mb_x*2 + (mb_y*2 + 1)*mot_stride;
  2017. assert(!s->mb_skiped);
  2018. memcpy(mv_cache[1][1], s->motion_val[mot_xy ], sizeof(int16_t)*4);
  2019. memcpy(mv_cache[2][1], s->motion_val[mot_xy+mot_stride], sizeof(int16_t)*4);
  2020. memcpy(mv_cache[3][1], s->motion_val[mot_xy+mot_stride], sizeof(int16_t)*4);
  2021. if(mb_y==0 || IS_INTRA(s->current_picture.mb_type[xy-s->mb_stride])){
  2022. memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4);
  2023. }else{
  2024. memcpy(mv_cache[0][1], s->motion_val[mot_xy-mot_stride], sizeof(int16_t)*4);
  2025. }
  2026. if(mb_x==0 || IS_INTRA(s->current_picture.mb_type[xy-1])){
  2027. *(int32_t*)mv_cache[1][0]= *(int32_t*)mv_cache[1][1];
  2028. *(int32_t*)mv_cache[2][0]= *(int32_t*)mv_cache[2][1];
  2029. }else{
  2030. *(int32_t*)mv_cache[1][0]= *(int32_t*)s->motion_val[mot_xy-1];
  2031. *(int32_t*)mv_cache[2][0]= *(int32_t*)s->motion_val[mot_xy-1+mot_stride];
  2032. }
  2033. if(mb_x+1>=s->mb_width || IS_INTRA(s->current_picture.mb_type[xy+1])){
  2034. *(int32_t*)mv_cache[1][3]= *(int32_t*)mv_cache[1][2];
  2035. *(int32_t*)mv_cache[2][3]= *(int32_t*)mv_cache[2][2];
  2036. }else{
  2037. *(int32_t*)mv_cache[1][3]= *(int32_t*)s->motion_val[mot_xy+2];
  2038. *(int32_t*)mv_cache[2][3]= *(int32_t*)s->motion_val[mot_xy+2+mot_stride];
  2039. }
  2040. mx = 0;
  2041. my = 0;
  2042. for(i=0;i<4;i++) {
  2043. const int x= (i&1)+1;
  2044. const int y= (i>>1)+1;
  2045. int16_t mv[5][2]= {
  2046. {mv_cache[y][x ][0], mv_cache[y][x ][1]},
  2047. {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
  2048. {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
  2049. {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
  2050. {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
  2051. //FIXME cleanup
  2052. obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  2053. ref_picture[0],
  2054. mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
  2055. pix_op[1],
  2056. mv);
  2057. mx += mv[0][0];
  2058. my += mv[0][1];
  2059. }
  2060. if(!(s->flags&CODEC_FLAG_GRAY))
  2061. chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
  2062. return;
  2063. }
  2064. switch(s->mv_type) {
  2065. case MV_TYPE_16X16:
  2066. #ifdef CONFIG_RISKY
  2067. if(s->mcsel){
  2068. if(s->real_sprite_warping_points==1){
  2069. gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
  2070. ref_picture, 0);
  2071. }else{
  2072. gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
  2073. ref_picture, 0);
  2074. }
  2075. }else if(s->quarter_sample){
  2076. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  2077. ref_picture, 0,
  2078. 0, pix_op, qpix_op,
  2079. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2080. }else if(s->mspel){
  2081. ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
  2082. ref_picture, pix_op,
  2083. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2084. }else
  2085. #endif
  2086. {
  2087. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2088. ref_picture, 0,
  2089. 0, pix_op,
  2090. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2091. }
  2092. break;
  2093. case MV_TYPE_8X8:
  2094. mx = 0;
  2095. my = 0;
  2096. if(s->quarter_sample){
  2097. for(i=0;i<4;i++) {
  2098. motion_x = s->mv[dir][i][0];
  2099. motion_y = s->mv[dir][i][1];
  2100. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  2101. src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
  2102. src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
  2103. /* WARNING: do no forget half pels */
  2104. src_x = clip(src_x, -16, s->width);
  2105. if (src_x == s->width)
  2106. dxy &= ~3;
  2107. src_y = clip(src_y, -16, s->height);
  2108. if (src_y == s->height)
  2109. dxy &= ~12;
  2110. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  2111. if(s->flags&CODEC_FLAG_EMU_EDGE){
  2112. if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 8
  2113. || (unsigned)src_y > s->v_edge_pos - (motion_y&3) - 8 ){
  2114. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  2115. ptr= s->edge_emu_buffer;
  2116. }
  2117. }
  2118. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  2119. qpix_op[1][dxy](dest, ptr, s->linesize);
  2120. mx += s->mv[dir][i][0]/2;
  2121. my += s->mv[dir][i][1]/2;
  2122. }
  2123. }else{
  2124. for(i=0;i<4;i++) {
  2125. hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  2126. ref_picture[0],
  2127. mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
  2128. s->width, s->height, s->linesize,
  2129. s->h_edge_pos, s->v_edge_pos,
  2130. 8, 8, pix_op[1],
  2131. s->mv[dir][i][0], s->mv[dir][i][1]);
  2132. mx += s->mv[dir][i][0];
  2133. my += s->mv[dir][i][1];
  2134. }
  2135. }
  2136. if(!(s->flags&CODEC_FLAG_GRAY))
  2137. chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
  2138. break;
  2139. case MV_TYPE_FIELD:
  2140. if (s->picture_structure == PICT_FRAME) {
  2141. if(s->quarter_sample){
  2142. /* top field */
  2143. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  2144. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  2145. 1, pix_op, qpix_op,
  2146. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  2147. /* bottom field */
  2148. qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  2149. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  2150. 1, pix_op, qpix_op,
  2151. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  2152. }else{
  2153. /* top field */
  2154. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2155. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  2156. 1, pix_op,
  2157. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  2158. /* bottom field */
  2159. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  2160. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  2161. 1, pix_op,
  2162. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  2163. }
  2164. } else {
  2165. int offset;
  2166. if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
  2167. offset= s->field_select[dir][0] ? s->linesize : 0;
  2168. }else{
  2169. ref_picture= s->current_picture.data;
  2170. offset= s->field_select[dir][0] ? s->linesize : -s->linesize;
  2171. }
  2172. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2173. ref_picture, offset,
  2174. 0, pix_op,
  2175. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2176. }
  2177. break;
  2178. case MV_TYPE_16X8:{
  2179. int offset;
  2180. uint8_t ** ref2picture;
  2181. if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
  2182. ref2picture= ref_picture;
  2183. offset= s->field_select[dir][0] ? s->linesize : 0;
  2184. }else{
  2185. ref2picture= s->current_picture.data;
  2186. offset= s->field_select[dir][0] ? s->linesize : -s->linesize;
  2187. }
  2188. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2189. ref2picture, offset,
  2190. 0, pix_op,
  2191. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  2192. if(s->picture_structure == s->field_select[dir][1] + 1 || s->pict_type == B_TYPE || s->first_field){
  2193. ref2picture= ref_picture;
  2194. offset= s->field_select[dir][1] ? s->linesize : 0;
  2195. }else{
  2196. ref2picture= s->current_picture.data;
  2197. offset= s->field_select[dir][1] ? s->linesize : -s->linesize;
  2198. }
  2199. // I know it is ugly but this is the only way to fool emu_edge without rewrite mpeg_motion
  2200. mpeg_motion(s, dest_y+16*s->linesize, dest_cb+8*s->uvlinesize, dest_cr+8*s->uvlinesize,
  2201. 0,
  2202. ref2picture, offset,
  2203. 0, pix_op,
  2204. s->mv[dir][1][0], s->mv[dir][1][1]+16, 8);
  2205. }
  2206. break;
  2207. case MV_TYPE_DMV:
  2208. {
  2209. op_pixels_func (*dmv_pix_op)[4];
  2210. int offset;
  2211. dmv_pix_op = s->dsp.put_pixels_tab;
  2212. if(s->picture_structure == PICT_FRAME){
  2213. //put top field from top field
  2214. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2215. ref_picture, 0,
  2216. 1, dmv_pix_op,
  2217. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  2218. //put bottom field from bottom field
  2219. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  2220. ref_picture, s->linesize,
  2221. 1, dmv_pix_op,
  2222. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  2223. dmv_pix_op = s->dsp.avg_pixels_tab;
  2224. //avg top field from bottom field
  2225. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2226. ref_picture, s->linesize,
  2227. 1, dmv_pix_op,
  2228. s->mv[dir][2][0], s->mv[dir][2][1], 8);
  2229. //avg bottom field from top field
  2230. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  2231. ref_picture, 0,
  2232. 1, dmv_pix_op,
  2233. s->mv[dir][3][0], s->mv[dir][3][1], 8);
  2234. }else{
  2235. offset=(s->picture_structure == PICT_BOTTOM_FIELD)?
  2236. s->linesize : 0;
  2237. //put field from the same parity
  2238. //same parity is never in the same frame
  2239. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2240. ref_picture,offset,
  2241. 0,dmv_pix_op,
  2242. s->mv[dir][0][0],s->mv[dir][0][1],16);
  2243. // after put we make avg of the same block
  2244. dmv_pix_op=s->dsp.avg_pixels_tab;
  2245. //opposite parity is always in the same frame if this is second field
  2246. if(!s->first_field){
  2247. ref_picture = s->current_picture.data;
  2248. //top field is one linesize from frame beginig
  2249. offset=(s->picture_structure == PICT_BOTTOM_FIELD)?
  2250. -s->linesize : s->linesize;
  2251. }else
  2252. offset=(s->picture_structure == PICT_BOTTOM_FIELD)?
  2253. 0 : s->linesize;
  2254. //avg field from the opposite parity
  2255. mpeg_motion(s, dest_y, dest_cb, dest_cr,0,
  2256. ref_picture, offset,
  2257. 0,dmv_pix_op,
  2258. s->mv[dir][2][0],s->mv[dir][2][1],16);
  2259. }
  2260. }
  2261. break;
  2262. default: assert(0);
  2263. }
  2264. }
  2265. /* put block[] to dest[] */
  2266. static inline void put_dct(MpegEncContext *s,
  2267. DCTELEM *block, int i, uint8_t *dest, int line_size)
  2268. {
  2269. s->dct_unquantize(s, block, i, s->qscale);
  2270. s->dsp.idct_put (dest, line_size, block);
  2271. }
  2272. /* add block[] to dest[] */
  2273. static inline void add_dct(MpegEncContext *s,
  2274. DCTELEM *block, int i, uint8_t *dest, int line_size)
  2275. {
  2276. if (s->block_last_index[i] >= 0) {
  2277. s->dsp.idct_add (dest, line_size, block);
  2278. }
  2279. }
  2280. static inline void add_dequant_dct(MpegEncContext *s,
  2281. DCTELEM *block, int i, uint8_t *dest, int line_size)
  2282. {
  2283. if (s->block_last_index[i] >= 0) {
  2284. s->dct_unquantize(s, block, i, s->qscale);
  2285. s->dsp.idct_add (dest, line_size, block);
  2286. }
  2287. }
  2288. /**
  2289. * cleans dc, ac, coded_block for the current non intra MB
  2290. */
  2291. void ff_clean_intra_table_entries(MpegEncContext *s)
  2292. {
  2293. int wrap = s->block_wrap[0];
  2294. int xy = s->block_index[0];
  2295. s->dc_val[0][xy ] =
  2296. s->dc_val[0][xy + 1 ] =
  2297. s->dc_val[0][xy + wrap] =
  2298. s->dc_val[0][xy + 1 + wrap] = 1024;
  2299. /* ac pred */
  2300. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  2301. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  2302. if (s->msmpeg4_version>=3) {
  2303. s->coded_block[xy ] =
  2304. s->coded_block[xy + 1 ] =
  2305. s->coded_block[xy + wrap] =
  2306. s->coded_block[xy + 1 + wrap] = 0;
  2307. }
  2308. /* chroma */
  2309. wrap = s->block_wrap[4];
  2310. xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
  2311. s->dc_val[1][xy] =
  2312. s->dc_val[2][xy] = 1024;
  2313. /* ac pred */
  2314. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  2315. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  2316. s->mbintra_table[s->mb_x + s->mb_y*s->mb_stride]= 0;
  2317. }
  2318. /* generic function called after a macroblock has been parsed by the
  2319. decoder or after it has been encoded by the encoder.
  2320. Important variables used:
  2321. s->mb_intra : true if intra macroblock
  2322. s->mv_dir : motion vector direction
  2323. s->mv_type : motion vector type
  2324. s->mv : motion vector
  2325. s->interlaced_dct : true if interlaced dct used (mpeg2)
  2326. */
  2327. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  2328. {
  2329. int mb_x, mb_y;
  2330. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  2331. #ifdef HAVE_XVMC
  2332. if(s->avctx->xvmc_acceleration){
  2333. XVMC_decode_mb(s);//xvmc uses pblocks
  2334. return;
  2335. }
  2336. #endif
  2337. mb_x = s->mb_x;
  2338. mb_y = s->mb_y;
  2339. s->current_picture.qscale_table[mb_xy]= s->qscale;
  2340. /* update DC predictors for P macroblocks */
  2341. if (!s->mb_intra) {
  2342. if (s->h263_pred || s->h263_aic) {
  2343. if(s->mbintra_table[mb_xy])
  2344. ff_clean_intra_table_entries(s);
  2345. } else {
  2346. s->last_dc[0] =
  2347. s->last_dc[1] =
  2348. s->last_dc[2] = 128 << s->intra_dc_precision;
  2349. }
  2350. }
  2351. else if (s->h263_pred || s->h263_aic)
  2352. s->mbintra_table[mb_xy]=1;
  2353. if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
  2354. uint8_t *dest_y, *dest_cb, *dest_cr;
  2355. int dct_linesize, dct_offset;
  2356. op_pixels_func (*op_pix)[4];
  2357. qpel_mc_func (*op_qpix)[16];
  2358. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
  2359. const int uvlinesize= s->current_picture.linesize[1];
  2360. const int readable= s->pict_type != B_TYPE || s->encoding || s->avctx->draw_horiz_band;
  2361. /* avoid copy if macroblock skipped in last frame too */
  2362. /* skip only during decoding as we might trash the buffers during encoding a bit */
  2363. if(!s->encoding){
  2364. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  2365. const int age= s->current_picture.age;
  2366. assert(age);
  2367. if (s->mb_skiped) {
  2368. s->mb_skiped= 0;
  2369. assert(s->pict_type!=I_TYPE);
  2370. (*mbskip_ptr) ++; /* indicate that this time we skiped it */
  2371. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  2372. /* if previous was skipped too, then nothing to do ! */
  2373. if (*mbskip_ptr >= age && s->current_picture.reference){
  2374. return;
  2375. }
  2376. } else if(!s->current_picture.reference){
  2377. (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
  2378. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  2379. } else{
  2380. *mbskip_ptr = 0; /* not skipped */
  2381. }
  2382. }
  2383. if (s->interlaced_dct) {
  2384. dct_linesize = linesize * 2;
  2385. dct_offset = linesize;
  2386. } else {
  2387. dct_linesize = linesize;
  2388. dct_offset = linesize * 8;
  2389. }
  2390. if(readable){
  2391. dest_y= s->dest[0];
  2392. dest_cb= s->dest[1];
  2393. dest_cr= s->dest[2];
  2394. }else{
  2395. dest_y = s->edge_emu_buffer+32; //FIXME cleanup scratchpad pointers
  2396. dest_cb= s->edge_emu_buffer+48;
  2397. dest_cr= s->edge_emu_buffer+56;
  2398. }
  2399. if (!s->mb_intra) {
  2400. /* motion handling */
  2401. /* decoding or more than one mb_type (MC was allready done otherwise) */
  2402. if(!s->encoding){
  2403. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  2404. op_pix = s->dsp.put_pixels_tab;
  2405. op_qpix= s->dsp.put_qpel_pixels_tab;
  2406. }else{
  2407. op_pix = s->dsp.put_no_rnd_pixels_tab;
  2408. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  2409. }
  2410. if (s->mv_dir & MV_DIR_FORWARD) {
  2411. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  2412. op_pix = s->dsp.avg_pixels_tab;
  2413. op_qpix= s->dsp.avg_qpel_pixels_tab;
  2414. }
  2415. if (s->mv_dir & MV_DIR_BACKWARD) {
  2416. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  2417. }
  2418. }
  2419. /* skip dequant / idct if we are really late ;) */
  2420. if(s->hurry_up>1) return;
  2421. /* add dct residue */
  2422. if(s->encoding || !( s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
  2423. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  2424. add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
  2425. add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  2426. add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  2427. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  2428. if(!(s->flags&CODEC_FLAG_GRAY)){
  2429. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize);
  2430. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize);
  2431. }
  2432. } else if(s->codec_id != CODEC_ID_WMV2){
  2433. add_dct(s, block[0], 0, dest_y, dct_linesize);
  2434. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  2435. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  2436. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  2437. if(!(s->flags&CODEC_FLAG_GRAY)){
  2438. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  2439. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  2440. }
  2441. }
  2442. #ifdef CONFIG_RISKY
  2443. else{
  2444. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  2445. }
  2446. #endif
  2447. } else {
  2448. /* dct only in intra block */
  2449. if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
  2450. put_dct(s, block[0], 0, dest_y, dct_linesize);
  2451. put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  2452. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  2453. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  2454. if(!(s->flags&CODEC_FLAG_GRAY)){
  2455. put_dct(s, block[4], 4, dest_cb, uvlinesize);
  2456. put_dct(s, block[5], 5, dest_cr, uvlinesize);
  2457. }
  2458. }else{
  2459. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  2460. s->dsp.idct_put(dest_y + 8, dct_linesize, block[1]);
  2461. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  2462. s->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
  2463. if(!(s->flags&CODEC_FLAG_GRAY)){
  2464. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  2465. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  2466. }
  2467. }
  2468. }
  2469. if(!readable){
  2470. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2471. s->dsp.put_pixels_tab[1][0](s->dest[1], dest_cb, uvlinesize, 8);
  2472. s->dsp.put_pixels_tab[1][0](s->dest[2], dest_cr, uvlinesize, 8);
  2473. }
  2474. }
  2475. }
  2476. #ifdef CONFIG_ENCODERS
  2477. static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
  2478. {
  2479. static const char tab[64]=
  2480. {3,2,2,1,1,1,1,1,
  2481. 1,1,1,1,1,1,1,1,
  2482. 1,1,1,1,1,1,1,1,
  2483. 0,0,0,0,0,0,0,0,
  2484. 0,0,0,0,0,0,0,0,
  2485. 0,0,0,0,0,0,0,0,
  2486. 0,0,0,0,0,0,0,0,
  2487. 0,0,0,0,0,0,0,0};
  2488. int score=0;
  2489. int run=0;
  2490. int i;
  2491. DCTELEM *block= s->block[n];
  2492. const int last_index= s->block_last_index[n];
  2493. int skip_dc;
  2494. if(threshold<0){
  2495. skip_dc=0;
  2496. threshold= -threshold;
  2497. }else
  2498. skip_dc=1;
  2499. /* are all which we could set to zero are allready zero? */
  2500. if(last_index<=skip_dc - 1) return;
  2501. for(i=0; i<=last_index; i++){
  2502. const int j = s->intra_scantable.permutated[i];
  2503. const int level = ABS(block[j]);
  2504. if(level==1){
  2505. if(skip_dc && i==0) continue;
  2506. score+= tab[run];
  2507. run=0;
  2508. }else if(level>1){
  2509. return;
  2510. }else{
  2511. run++;
  2512. }
  2513. }
  2514. if(score >= threshold) return;
  2515. for(i=skip_dc; i<=last_index; i++){
  2516. const int j = s->intra_scantable.permutated[i];
  2517. block[j]=0;
  2518. }
  2519. if(block[0]) s->block_last_index[n]= 0;
  2520. else s->block_last_index[n]= -1;
  2521. }
  2522. static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
  2523. {
  2524. int i;
  2525. const int maxlevel= s->max_qcoeff;
  2526. const int minlevel= s->min_qcoeff;
  2527. if(s->mb_intra){
  2528. i=1; //skip clipping of intra dc
  2529. }else
  2530. i=0;
  2531. for(;i<=last_index; i++){
  2532. const int j= s->intra_scantable.permutated[i];
  2533. int level = block[j];
  2534. if (level>maxlevel) level=maxlevel;
  2535. else if(level<minlevel) level=minlevel;
  2536. block[j]= level;
  2537. }
  2538. }
  2539. #if 0
  2540. static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
  2541. int score=0;
  2542. int x,y;
  2543. for(y=0; y<7; y++){
  2544. for(x=0; x<16; x+=4){
  2545. score+= ABS(s[x ] - s[x +stride]) + ABS(s[x+1] - s[x+1+stride])
  2546. +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
  2547. }
  2548. s+= stride;
  2549. }
  2550. return score;
  2551. }
  2552. static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
  2553. int score=0;
  2554. int x,y;
  2555. for(y=0; y<7; y++){
  2556. for(x=0; x<16; x++){
  2557. score+= ABS(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
  2558. }
  2559. s1+= stride;
  2560. s2+= stride;
  2561. }
  2562. return score;
  2563. }
  2564. #else
  2565. #define SQ(a) ((a)*(a))
  2566. static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
  2567. int score=0;
  2568. int x,y;
  2569. for(y=0; y<7; y++){
  2570. for(x=0; x<16; x+=4){
  2571. score+= SQ(s[x ] - s[x +stride]) + SQ(s[x+1] - s[x+1+stride])
  2572. +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
  2573. }
  2574. s+= stride;
  2575. }
  2576. return score;
  2577. }
  2578. static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
  2579. int score=0;
  2580. int x,y;
  2581. for(y=0; y<7; y++){
  2582. for(x=0; x<16; x++){
  2583. score+= SQ(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
  2584. }
  2585. s1+= stride;
  2586. s2+= stride;
  2587. }
  2588. return score;
  2589. }
  2590. #endif
  2591. #endif //CONFIG_ENCODERS
  2592. /**
  2593. *
  2594. * @param h is the normal height, this will be reduced automatically if needed for the last row
  2595. */
  2596. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  2597. if (s->avctx->draw_horiz_band) {
  2598. AVFrame *src;
  2599. int offset[4];
  2600. if(s->picture_structure != PICT_FRAME){
  2601. h <<= 1;
  2602. y <<= 1;
  2603. if(s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  2604. }
  2605. h= FFMIN(h, s->height - y);
  2606. if(s->pict_type==B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  2607. src= (AVFrame*)s->current_picture_ptr;
  2608. else if(s->last_picture_ptr)
  2609. src= (AVFrame*)s->last_picture_ptr;
  2610. else
  2611. return;
  2612. if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  2613. offset[0]=
  2614. offset[1]=
  2615. offset[2]=
  2616. offset[3]= 0;
  2617. }else{
  2618. offset[0]= y * s->linesize;;
  2619. offset[1]=
  2620. offset[2]= (y>>1) * s->uvlinesize;;
  2621. offset[3]= 0;
  2622. }
  2623. emms_c();
  2624. s->avctx->draw_horiz_band(s->avctx, src, offset,
  2625. y, s->picture_structure, h);
  2626. }
  2627. }
  2628. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2629. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
  2630. const int uvlinesize= s->current_picture.linesize[1];
  2631. s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2632. s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1) + s->mb_x*2;
  2633. s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1 + s->mb_x*2;
  2634. s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2) + s->mb_x*2;
  2635. s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
  2636. s->block_index[5]= s->block_wrap[4]*(s->mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
  2637. if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME){
  2638. s->dest[0] = s->current_picture.data[0] + s->mb_x * 16 - 16;
  2639. s->dest[1] = s->current_picture.data[1] + s->mb_x * 8 - 8;
  2640. s->dest[2] = s->current_picture.data[2] + s->mb_x * 8 - 8;
  2641. }else{
  2642. s->dest[0] = s->current_picture.data[0] + (s->mb_y * 16* linesize ) + s->mb_x * 16 - 16;
  2643. s->dest[1] = s->current_picture.data[1] + (s->mb_y * 8 * uvlinesize) + s->mb_x * 8 - 8;
  2644. s->dest[2] = s->current_picture.data[2] + (s->mb_y * 8 * uvlinesize) + s->mb_x * 8 - 8;
  2645. }
  2646. }
  2647. #ifdef CONFIG_ENCODERS
  2648. static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  2649. {
  2650. const int mb_x= s->mb_x;
  2651. const int mb_y= s->mb_y;
  2652. int i;
  2653. int skip_dct[6];
  2654. int dct_offset = s->linesize*8; //default for progressive frames
  2655. for(i=0; i<6; i++) skip_dct[i]=0;
  2656. if(s->adaptive_quant){
  2657. const int last_qp= s->qscale;
  2658. const int mb_xy= mb_x + mb_y*s->mb_stride;
  2659. s->lambda= s->lambda_table[mb_xy];
  2660. update_qscale(s);
  2661. if(!(s->flags&CODEC_FLAG_QP_RD)){
  2662. s->dquant= s->qscale - last_qp;
  2663. if(s->out_format==FMT_H263)
  2664. s->dquant= clip(s->dquant, -2, 2); //FIXME RD
  2665. if(s->codec_id==CODEC_ID_MPEG4){
  2666. if(!s->mb_intra){
  2667. if((s->mv_dir&MV_DIRECT) || s->mv_type==MV_TYPE_8X8)
  2668. s->dquant=0;
  2669. }
  2670. }
  2671. }
  2672. s->qscale= last_qp + s->dquant;
  2673. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  2674. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  2675. }
  2676. if (s->mb_intra) {
  2677. uint8_t *ptr;
  2678. int wrap_y;
  2679. int emu=0;
  2680. wrap_y = s->linesize;
  2681. ptr = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  2682. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  2683. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
  2684. ptr= s->edge_emu_buffer;
  2685. emu=1;
  2686. }
  2687. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  2688. int progressive_score, interlaced_score;
  2689. progressive_score= pix_vcmp16x8(ptr, wrap_y ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
  2690. interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y , wrap_y*2);
  2691. if(progressive_score > interlaced_score + 100){
  2692. s->interlaced_dct=1;
  2693. dct_offset= wrap_y;
  2694. wrap_y<<=1;
  2695. }else
  2696. s->interlaced_dct=0;
  2697. }
  2698. s->dsp.get_pixels(s->block[0], ptr , wrap_y);
  2699. s->dsp.get_pixels(s->block[1], ptr + 8, wrap_y);
  2700. s->dsp.get_pixels(s->block[2], ptr + dct_offset , wrap_y);
  2701. s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
  2702. if(s->flags&CODEC_FLAG_GRAY){
  2703. skip_dct[4]= 1;
  2704. skip_dct[5]= 1;
  2705. }else{
  2706. int wrap_c = s->uvlinesize;
  2707. ptr = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2708. if(emu){
  2709. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2710. ptr= s->edge_emu_buffer;
  2711. }
  2712. s->dsp.get_pixels(s->block[4], ptr, wrap_c);
  2713. ptr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2714. if(emu){
  2715. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2716. ptr= s->edge_emu_buffer;
  2717. }
  2718. s->dsp.get_pixels(s->block[5], ptr, wrap_c);
  2719. }
  2720. }else{
  2721. op_pixels_func (*op_pix)[4];
  2722. qpel_mc_func (*op_qpix)[16];
  2723. uint8_t *dest_y, *dest_cb, *dest_cr;
  2724. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  2725. int wrap_y, wrap_c;
  2726. int emu=0;
  2727. dest_y = s->dest[0];
  2728. dest_cb = s->dest[1];
  2729. dest_cr = s->dest[2];
  2730. wrap_y = s->linesize;
  2731. wrap_c = s->uvlinesize;
  2732. ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  2733. ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2734. ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2735. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  2736. op_pix = s->dsp.put_pixels_tab;
  2737. op_qpix= s->dsp.put_qpel_pixels_tab;
  2738. }else{
  2739. op_pix = s->dsp.put_no_rnd_pixels_tab;
  2740. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  2741. }
  2742. if (s->mv_dir & MV_DIR_FORWARD) {
  2743. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  2744. op_pix = s->dsp.avg_pixels_tab;
  2745. op_qpix= s->dsp.avg_qpel_pixels_tab;
  2746. }
  2747. if (s->mv_dir & MV_DIR_BACKWARD) {
  2748. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  2749. }
  2750. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  2751. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
  2752. ptr_y= s->edge_emu_buffer;
  2753. emu=1;
  2754. }
  2755. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  2756. int progressive_score, interlaced_score;
  2757. progressive_score= pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y )
  2758. + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y );
  2759. interlaced_score = pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y*2)
  2760. + pix_diff_vcmp16x8(ptr_y + wrap_y , dest_y + wrap_y , wrap_y*2);
  2761. if(progressive_score > interlaced_score + 600){
  2762. s->interlaced_dct=1;
  2763. dct_offset= wrap_y;
  2764. wrap_y<<=1;
  2765. }else
  2766. s->interlaced_dct=0;
  2767. }
  2768. s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
  2769. s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  2770. s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
  2771. s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
  2772. if(s->flags&CODEC_FLAG_GRAY){
  2773. skip_dct[4]= 1;
  2774. skip_dct[5]= 1;
  2775. }else{
  2776. if(emu){
  2777. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2778. ptr_cb= s->edge_emu_buffer;
  2779. }
  2780. s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  2781. if(emu){
  2782. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
  2783. ptr_cr= s->edge_emu_buffer;
  2784. }
  2785. s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  2786. }
  2787. /* pre quantization */
  2788. if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){
  2789. //FIXME optimize
  2790. if(s->dsp.pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
  2791. if(s->dsp.pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
  2792. if(s->dsp.pix_abs8x8(ptr_y +dct_offset , dest_y +dct_offset , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
  2793. if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
  2794. if(s->dsp.pix_abs8x8(ptr_cb , dest_cb , wrap_c) < 20*s->qscale) skip_dct[4]= 1;
  2795. if(s->dsp.pix_abs8x8(ptr_cr , dest_cr , wrap_c) < 20*s->qscale) skip_dct[5]= 1;
  2796. #if 0
  2797. {
  2798. static int stat[7];
  2799. int num=0;
  2800. for(i=0; i<6; i++)
  2801. if(skip_dct[i]) num++;
  2802. stat[num]++;
  2803. if(s->mb_x==0 && s->mb_y==0){
  2804. for(i=0; i<7; i++){
  2805. printf("%6d %1d\n", stat[i], i);
  2806. }
  2807. }
  2808. }
  2809. #endif
  2810. }
  2811. }
  2812. /* DCT & quantize */
  2813. if(s->out_format==FMT_MJPEG){
  2814. for(i=0;i<6;i++) {
  2815. int overflow;
  2816. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
  2817. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  2818. }
  2819. }else{
  2820. for(i=0;i<6;i++) {
  2821. if(!skip_dct[i]){
  2822. int overflow;
  2823. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  2824. // FIXME we could decide to change to quantizer instead of clipping
  2825. // JS: I don't think that would be a good idea it could lower quality instead
  2826. // of improve it. Just INTRADC clipping deserves changes in quantizer
  2827. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  2828. }else
  2829. s->block_last_index[i]= -1;
  2830. }
  2831. if(s->luma_elim_threshold && !s->mb_intra)
  2832. for(i=0; i<4; i++)
  2833. dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
  2834. if(s->chroma_elim_threshold && !s->mb_intra)
  2835. for(i=4; i<6; i++)
  2836. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
  2837. if(s->flags & CODEC_FLAG_CBP_RD){
  2838. for(i=0;i<6;i++) {
  2839. if(s->block_last_index[i] == -1)
  2840. s->coded_score[i]= INT_MAX/256;
  2841. }
  2842. }
  2843. }
  2844. if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
  2845. s->block_last_index[4]=
  2846. s->block_last_index[5]= 0;
  2847. s->block[4][0]=
  2848. s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
  2849. }
  2850. /* huffman encode */
  2851. switch(s->codec_id){ //FIXME funct ptr could be slightly faster
  2852. case CODEC_ID_MPEG1VIDEO:
  2853. case CODEC_ID_MPEG2VIDEO:
  2854. mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
  2855. #ifdef CONFIG_RISKY
  2856. case CODEC_ID_MPEG4:
  2857. mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  2858. case CODEC_ID_MSMPEG4V2:
  2859. case CODEC_ID_MSMPEG4V3:
  2860. case CODEC_ID_WMV1:
  2861. msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  2862. case CODEC_ID_WMV2:
  2863. ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
  2864. case CODEC_ID_H263:
  2865. case CODEC_ID_H263P:
  2866. case CODEC_ID_FLV1:
  2867. case CODEC_ID_RV10:
  2868. h263_encode_mb(s, s->block, motion_x, motion_y); break;
  2869. #endif
  2870. case CODEC_ID_MJPEG:
  2871. mjpeg_encode_mb(s, s->block); break;
  2872. default:
  2873. assert(0);
  2874. }
  2875. }
  2876. #endif //CONFIG_ENCODERS
  2877. /**
  2878. * combines the (truncated) bitstream to a complete frame
  2879. * @returns -1 if no complete frame could be created
  2880. */
  2881. int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size){
  2882. ParseContext *pc= &s->parse_context;
  2883. #if 0
  2884. if(pc->overread){
  2885. printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
  2886. printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
  2887. }
  2888. #endif
  2889. /* copy overreaded byes from last frame into buffer */
  2890. for(; pc->overread>0; pc->overread--){
  2891. pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
  2892. }
  2893. pc->last_index= pc->index;
  2894. /* copy into buffer end return */
  2895. if(next == END_NOT_FOUND){
  2896. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  2897. memcpy(&pc->buffer[pc->index], *buf, *buf_size);
  2898. pc->index += *buf_size;
  2899. return -1;
  2900. }
  2901. *buf_size=
  2902. pc->overread_index= pc->index + next;
  2903. /* append to buffer */
  2904. if(pc->index){
  2905. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  2906. memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
  2907. pc->index = 0;
  2908. *buf= pc->buffer;
  2909. }
  2910. /* store overread bytes */
  2911. for(;next < 0; next++){
  2912. pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
  2913. pc->overread++;
  2914. }
  2915. #if 0
  2916. if(pc->overread){
  2917. printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
  2918. printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
  2919. }
  2920. #endif
  2921. return 0;
  2922. }
  2923. void ff_mpeg_flush(AVCodecContext *avctx){
  2924. int i;
  2925. MpegEncContext *s = avctx->priv_data;
  2926. for(i=0; i<MAX_PICTURE_COUNT; i++){
  2927. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  2928. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  2929. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  2930. }
  2931. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2932. s->parse_context.state= -1;
  2933. s->parse_context.frame_start_found= 0;
  2934. s->parse_context.overread= 0;
  2935. s->parse_context.overread_index= 0;
  2936. s->parse_context.index= 0;
  2937. s->parse_context.last_index= 0;
  2938. }
  2939. #ifdef CONFIG_ENCODERS
  2940. void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
  2941. {
  2942. int bytes= length>>4;
  2943. int bits= length&15;
  2944. int i;
  2945. if(length==0) return;
  2946. for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
  2947. put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
  2948. }
  2949. static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  2950. int i;
  2951. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  2952. /* mpeg1 */
  2953. d->mb_skip_run= s->mb_skip_run;
  2954. for(i=0; i<3; i++)
  2955. d->last_dc[i]= s->last_dc[i];
  2956. /* statistics */
  2957. d->mv_bits= s->mv_bits;
  2958. d->i_tex_bits= s->i_tex_bits;
  2959. d->p_tex_bits= s->p_tex_bits;
  2960. d->i_count= s->i_count;
  2961. d->f_count= s->f_count;
  2962. d->b_count= s->b_count;
  2963. d->skip_count= s->skip_count;
  2964. d->misc_bits= s->misc_bits;
  2965. d->last_bits= 0;
  2966. d->mb_skiped= 0;
  2967. d->qscale= s->qscale;
  2968. d->dquant= s->dquant;
  2969. }
  2970. static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  2971. int i;
  2972. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  2973. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  2974. /* mpeg1 */
  2975. d->mb_skip_run= s->mb_skip_run;
  2976. for(i=0; i<3; i++)
  2977. d->last_dc[i]= s->last_dc[i];
  2978. /* statistics */
  2979. d->mv_bits= s->mv_bits;
  2980. d->i_tex_bits= s->i_tex_bits;
  2981. d->p_tex_bits= s->p_tex_bits;
  2982. d->i_count= s->i_count;
  2983. d->f_count= s->f_count;
  2984. d->b_count= s->b_count;
  2985. d->skip_count= s->skip_count;
  2986. d->misc_bits= s->misc_bits;
  2987. d->mb_intra= s->mb_intra;
  2988. d->mb_skiped= s->mb_skiped;
  2989. d->mv_type= s->mv_type;
  2990. d->mv_dir= s->mv_dir;
  2991. d->pb= s->pb;
  2992. if(s->data_partitioning){
  2993. d->pb2= s->pb2;
  2994. d->tex_pb= s->tex_pb;
  2995. }
  2996. d->block= s->block;
  2997. for(i=0; i<6; i++)
  2998. d->block_last_index[i]= s->block_last_index[i];
  2999. d->interlaced_dct= s->interlaced_dct;
  3000. d->qscale= s->qscale;
  3001. }
  3002. static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
  3003. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  3004. int *dmin, int *next_block, int motion_x, int motion_y)
  3005. {
  3006. int score;
  3007. uint8_t *dest_backup[3];
  3008. copy_context_before_encode(s, backup, type);
  3009. s->block= s->blocks[*next_block];
  3010. s->pb= pb[*next_block];
  3011. if(s->data_partitioning){
  3012. s->pb2 = pb2 [*next_block];
  3013. s->tex_pb= tex_pb[*next_block];
  3014. }
  3015. if(*next_block){
  3016. memcpy(dest_backup, s->dest, sizeof(s->dest));
  3017. s->dest[0] = s->me.scratchpad;
  3018. s->dest[1] = s->me.scratchpad + 16;
  3019. s->dest[2] = s->me.scratchpad + 16 + 8;
  3020. assert(2*s->uvlinesize == s->linesize); //should be no prob for encoding
  3021. assert(s->linesize >= 64); //FIXME
  3022. }
  3023. encode_mb(s, motion_x, motion_y);
  3024. score= get_bit_count(&s->pb);
  3025. if(s->data_partitioning){
  3026. score+= get_bit_count(&s->pb2);
  3027. score+= get_bit_count(&s->tex_pb);
  3028. }
  3029. if(s->avctx->mb_decision == FF_MB_DECISION_RD){
  3030. MPV_decode_mb(s, s->block);
  3031. score *= s->lambda2;
  3032. score += sse_mb(s) << FF_LAMBDA_SHIFT;
  3033. }
  3034. if(*next_block){
  3035. memcpy(s->dest, dest_backup, sizeof(s->dest));
  3036. }
  3037. if(score<*dmin){
  3038. *dmin= score;
  3039. *next_block^=1;
  3040. copy_context_after_encode(best, s, type);
  3041. }
  3042. }
  3043. static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
  3044. uint32_t *sq = squareTbl + 256;
  3045. int acc=0;
  3046. int x,y;
  3047. if(w==16 && h==16)
  3048. return s->dsp.sse[0](NULL, src1, src2, stride);
  3049. else if(w==8 && h==8)
  3050. return s->dsp.sse[1](NULL, src1, src2, stride);
  3051. for(y=0; y<h; y++){
  3052. for(x=0; x<w; x++){
  3053. acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
  3054. }
  3055. }
  3056. assert(acc>=0);
  3057. return acc;
  3058. }
  3059. static int sse_mb(MpegEncContext *s){
  3060. int w= 16;
  3061. int h= 16;
  3062. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  3063. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  3064. if(w==16 && h==16)
  3065. return s->dsp.sse[0](NULL, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize)
  3066. +s->dsp.sse[1](NULL, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize)
  3067. +s->dsp.sse[1](NULL, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize);
  3068. else
  3069. return sse(s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], w, h, s->linesize)
  3070. +sse(s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], w>>1, h>>1, s->uvlinesize)
  3071. +sse(s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], w>>1, h>>1, s->uvlinesize);
  3072. }
  3073. static void encode_picture(MpegEncContext *s, int picture_number)
  3074. {
  3075. int mb_x, mb_y, pdif = 0;
  3076. int i;
  3077. int bits;
  3078. MpegEncContext best_s, backup_s;
  3079. uint8_t bit_buf[2][3000];
  3080. uint8_t bit_buf2[2][3000];
  3081. uint8_t bit_buf_tex[2][3000];
  3082. PutBitContext pb[2], pb2[2], tex_pb[2];
  3083. for(i=0; i<2; i++){
  3084. init_put_bits(&pb [i], bit_buf [i], 3000);
  3085. init_put_bits(&pb2 [i], bit_buf2 [i], 3000);
  3086. init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000);
  3087. }
  3088. s->picture_number = picture_number;
  3089. /* Reset the average MB variance */
  3090. s->current_picture.mb_var_sum = 0;
  3091. s->current_picture.mc_mb_var_sum = 0;
  3092. #ifdef CONFIG_RISKY
  3093. /* we need to initialize some time vars before we can encode b-frames */
  3094. // RAL: Condition added for MPEG1VIDEO
  3095. if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->h263_msmpeg4))
  3096. ff_set_mpeg4_time(s, s->picture_number);
  3097. #endif
  3098. s->scene_change_score=0;
  3099. s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME ratedistoration
  3100. if(s->pict_type==I_TYPE){
  3101. if(s->msmpeg4_version >= 3) s->no_rounding=1;
  3102. else s->no_rounding=0;
  3103. }else if(s->pict_type!=B_TYPE){
  3104. if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
  3105. s->no_rounding ^= 1;
  3106. }
  3107. /* Estimate motion for every MB */
  3108. s->mb_intra=0; //for the rate distoration & bit compare functions
  3109. if(s->pict_type != I_TYPE){
  3110. if(s->pict_type != B_TYPE){
  3111. if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
  3112. s->me.pre_pass=1;
  3113. s->me.dia_size= s->avctx->pre_dia_size;
  3114. for(mb_y=s->mb_height-1; mb_y >=0 ; mb_y--) {
  3115. for(mb_x=s->mb_width-1; mb_x >=0 ; mb_x--) {
  3116. s->mb_x = mb_x;
  3117. s->mb_y = mb_y;
  3118. ff_pre_estimate_p_frame_motion(s, mb_x, mb_y);
  3119. }
  3120. }
  3121. s->me.pre_pass=0;
  3122. }
  3123. }
  3124. s->me.dia_size= s->avctx->dia_size;
  3125. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  3126. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  3127. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  3128. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  3129. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  3130. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  3131. s->mb_x = mb_x;
  3132. s->mb_y = mb_y;
  3133. s->block_index[0]+=2;
  3134. s->block_index[1]+=2;
  3135. s->block_index[2]+=2;
  3136. s->block_index[3]+=2;
  3137. /* compute motion vector & mb_type and store in context */
  3138. if(s->pict_type==B_TYPE)
  3139. ff_estimate_b_frame_motion(s, mb_x, mb_y);
  3140. else
  3141. ff_estimate_p_frame_motion(s, mb_x, mb_y);
  3142. }
  3143. }
  3144. }else /* if(s->pict_type == I_TYPE) */{
  3145. /* I-Frame */
  3146. //FIXME do we need to zero them?
  3147. memset(s->motion_val[0], 0, sizeof(int16_t)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
  3148. memset(s->p_mv_table , 0, sizeof(int16_t)*(s->mb_stride)*s->mb_height*2);
  3149. memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_stride*s->mb_height);
  3150. if(!s->fixed_qscale){
  3151. /* finding spatial complexity for I-frame rate control */
  3152. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  3153. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  3154. int xx = mb_x * 16;
  3155. int yy = mb_y * 16;
  3156. uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  3157. int varc;
  3158. int sum = s->dsp.pix_sum(pix, s->linesize);
  3159. varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
  3160. s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
  3161. s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  3162. s->current_picture.mb_var_sum += varc;
  3163. }
  3164. }
  3165. }
  3166. }
  3167. emms_c();
  3168. if(s->scene_change_score > s->avctx->scenechange_threshold && s->pict_type == P_TYPE){
  3169. s->pict_type= I_TYPE;
  3170. memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_stride*s->mb_height);
  3171. //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
  3172. }
  3173. if(!s->umvplus){
  3174. if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
  3175. s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
  3176. ff_fix_long_p_mvs(s);
  3177. }
  3178. if(s->pict_type==B_TYPE){
  3179. int a, b;
  3180. a = ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
  3181. b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, MB_TYPE_BIDIR);
  3182. s->f_code = FFMAX(a, b);
  3183. a = ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
  3184. b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, MB_TYPE_BIDIR);
  3185. s->b_code = FFMAX(a, b);
  3186. ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
  3187. ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
  3188. ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
  3189. ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
  3190. }
  3191. }
  3192. if (!s->fixed_qscale)
  3193. s->current_picture.quality = ff_rate_estimate_qscale(s);
  3194. if(s->adaptive_quant){
  3195. #ifdef CONFIG_RISKY
  3196. switch(s->codec_id){
  3197. case CODEC_ID_MPEG4:
  3198. ff_clean_mpeg4_qscales(s);
  3199. break;
  3200. case CODEC_ID_H263:
  3201. case CODEC_ID_H263P:
  3202. case CODEC_ID_FLV1:
  3203. ff_clean_h263_qscales(s);
  3204. break;
  3205. }
  3206. #endif
  3207. s->lambda= s->lambda_table[0];
  3208. //FIXME broken
  3209. }else
  3210. s->lambda= s->current_picture.quality;
  3211. //printf("%d %d\n", s->avctx->global_quality, s->current_picture.quality);
  3212. update_qscale(s);
  3213. if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==I_TYPE && !(s->flags & CODEC_FLAG_QSCALE))
  3214. s->qscale= 3; //reduce cliping problems
  3215. if (s->out_format == FMT_MJPEG) {
  3216. /* for mjpeg, we do include qscale in the matrix */
  3217. s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
  3218. for(i=1;i<64;i++){
  3219. int j= s->dsp.idct_permutation[i];
  3220. s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
  3221. }
  3222. convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
  3223. s->intra_matrix, s->intra_quant_bias, 8, 8);
  3224. }
  3225. //FIXME var duplication
  3226. s->current_picture.key_frame= s->pict_type == I_TYPE;
  3227. s->current_picture.pict_type= s->pict_type;
  3228. if(s->current_picture.key_frame)
  3229. s->picture_in_gop_number=0;
  3230. s->last_bits= get_bit_count(&s->pb);
  3231. switch(s->out_format) {
  3232. case FMT_MJPEG:
  3233. mjpeg_picture_header(s);
  3234. break;
  3235. #ifdef CONFIG_RISKY
  3236. case FMT_H263:
  3237. if (s->codec_id == CODEC_ID_WMV2)
  3238. ff_wmv2_encode_picture_header(s, picture_number);
  3239. else if (s->h263_msmpeg4)
  3240. msmpeg4_encode_picture_header(s, picture_number);
  3241. else if (s->h263_pred)
  3242. mpeg4_encode_picture_header(s, picture_number);
  3243. else if (s->h263_rv10)
  3244. rv10_encode_picture_header(s, picture_number);
  3245. else if (s->codec_id == CODEC_ID_FLV1)
  3246. ff_flv_encode_picture_header(s, picture_number);
  3247. else
  3248. h263_encode_picture_header(s, picture_number);
  3249. break;
  3250. #endif
  3251. case FMT_MPEG1:
  3252. mpeg1_encode_picture_header(s, picture_number);
  3253. break;
  3254. case FMT_H264:
  3255. break;
  3256. }
  3257. bits= get_bit_count(&s->pb);
  3258. s->header_bits= bits - s->last_bits;
  3259. s->last_bits= bits;
  3260. s->mv_bits=0;
  3261. s->misc_bits=0;
  3262. s->i_tex_bits=0;
  3263. s->p_tex_bits=0;
  3264. s->i_count=0;
  3265. s->f_count=0;
  3266. s->b_count=0;
  3267. s->skip_count=0;
  3268. for(i=0; i<3; i++){
  3269. /* init last dc values */
  3270. /* note: quant matrix value (8) is implied here */
  3271. s->last_dc[i] = 128;
  3272. s->current_picture_ptr->error[i] = 0;
  3273. }
  3274. s->mb_skip_run = 0;
  3275. s->last_mv[0][0][0] = 0;
  3276. s->last_mv[0][0][1] = 0;
  3277. s->last_mv[1][0][0] = 0;
  3278. s->last_mv[1][0][1] = 0;
  3279. s->last_mv_dir = 0;
  3280. #ifdef CONFIG_RISKY
  3281. switch(s->codec_id){
  3282. case CODEC_ID_H263:
  3283. case CODEC_ID_H263P:
  3284. case CODEC_ID_FLV1:
  3285. s->gob_index = ff_h263_get_gob_height(s);
  3286. break;
  3287. case CODEC_ID_MPEG4:
  3288. if(s->partitioned_frame)
  3289. ff_mpeg4_init_partitions(s);
  3290. break;
  3291. }
  3292. #endif
  3293. s->resync_mb_x=0;
  3294. s->resync_mb_y=0;
  3295. s->first_slice_line = 1;
  3296. s->ptr_lastgob = s->pb.buf;
  3297. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  3298. s->mb_x=0;
  3299. s->mb_y= mb_y;
  3300. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  3301. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  3302. ff_init_block_index(s);
  3303. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  3304. const int xy= mb_y*s->mb_stride + mb_x;
  3305. int mb_type= s->mb_type[xy];
  3306. // int d;
  3307. int dmin= INT_MAX;
  3308. s->mb_x = mb_x;
  3309. ff_update_block_index(s);
  3310. /* write gob / video packet header */
  3311. #ifdef CONFIG_RISKY
  3312. if(s->rtp_mode && mb_y + mb_x>0){
  3313. int current_packet_size, is_gob_start;
  3314. current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
  3315. is_gob_start=0;
  3316. if(s->codec_id==CODEC_ID_MPEG4){
  3317. if(current_packet_size >= s->rtp_payload_size){
  3318. if(s->partitioned_frame){
  3319. ff_mpeg4_merge_partitions(s);
  3320. ff_mpeg4_init_partitions(s);
  3321. }
  3322. ff_mpeg4_encode_video_packet_header(s);
  3323. if(s->flags&CODEC_FLAG_PASS1){
  3324. int bits= get_bit_count(&s->pb);
  3325. s->misc_bits+= bits - s->last_bits;
  3326. s->last_bits= bits;
  3327. }
  3328. ff_mpeg4_clean_buffers(s);
  3329. is_gob_start=1;
  3330. }
  3331. }else if(s->codec_id==CODEC_ID_MPEG1VIDEO){
  3332. if( current_packet_size >= s->rtp_payload_size
  3333. && s->mb_skip_run==0){
  3334. ff_mpeg1_encode_slice_header(s);
  3335. ff_mpeg1_clean_buffers(s);
  3336. is_gob_start=1;
  3337. }
  3338. }else if(s->codec_id==CODEC_ID_MPEG2VIDEO){
  3339. if( ( current_packet_size >= s->rtp_payload_size || mb_x==0)
  3340. && s->mb_skip_run==0){
  3341. ff_mpeg1_encode_slice_header(s);
  3342. ff_mpeg1_clean_buffers(s);
  3343. is_gob_start=1;
  3344. }
  3345. }else{
  3346. if(current_packet_size >= s->rtp_payload_size
  3347. && s->mb_x==0 && s->mb_y%s->gob_index==0){
  3348. h263_encode_gob_header(s, mb_y);
  3349. is_gob_start=1;
  3350. }
  3351. }
  3352. if(is_gob_start){
  3353. s->ptr_lastgob = pbBufPtr(&s->pb);
  3354. s->first_slice_line=1;
  3355. s->resync_mb_x=mb_x;
  3356. s->resync_mb_y=mb_y;
  3357. }
  3358. }
  3359. #endif
  3360. if( (s->resync_mb_x == s->mb_x)
  3361. && s->resync_mb_y+1 == s->mb_y){
  3362. s->first_slice_line=0;
  3363. }
  3364. s->mb_skiped=0;
  3365. s->dquant=0; //only for QP_RD
  3366. if(mb_type & (mb_type-1) || (s->flags & CODEC_FLAG_QP_RD)){ // more than 1 MB type possible
  3367. int next_block=0;
  3368. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  3369. copy_context_before_encode(&backup_s, s, -1);
  3370. backup_s.pb= s->pb;
  3371. best_s.data_partitioning= s->data_partitioning;
  3372. best_s.partitioned_frame= s->partitioned_frame;
  3373. if(s->data_partitioning){
  3374. backup_s.pb2= s->pb2;
  3375. backup_s.tex_pb= s->tex_pb;
  3376. }
  3377. if(mb_type&MB_TYPE_INTER){
  3378. s->mv_dir = MV_DIR_FORWARD;
  3379. s->mv_type = MV_TYPE_16X16;
  3380. s->mb_intra= 0;
  3381. s->mv[0][0][0] = s->p_mv_table[xy][0];
  3382. s->mv[0][0][1] = s->p_mv_table[xy][1];
  3383. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb,
  3384. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  3385. }
  3386. if(mb_type&MB_TYPE_SKIPED){
  3387. s->mv_dir = MV_DIR_FORWARD;
  3388. s->mv_type = MV_TYPE_16X16;
  3389. s->mb_intra= 0;
  3390. s->mv[0][0][0] = 0;
  3391. s->mv[0][0][1] = 0;
  3392. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_SKIPED, pb, pb2, tex_pb,
  3393. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  3394. }
  3395. if(mb_type&MB_TYPE_INTER4V){
  3396. s->mv_dir = MV_DIR_FORWARD;
  3397. s->mv_type = MV_TYPE_8X8;
  3398. s->mb_intra= 0;
  3399. for(i=0; i<4; i++){
  3400. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  3401. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  3402. }
  3403. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb,
  3404. &dmin, &next_block, 0, 0);
  3405. }
  3406. if(mb_type&MB_TYPE_FORWARD){
  3407. s->mv_dir = MV_DIR_FORWARD;
  3408. s->mv_type = MV_TYPE_16X16;
  3409. s->mb_intra= 0;
  3410. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  3411. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  3412. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb,
  3413. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  3414. }
  3415. if(mb_type&MB_TYPE_BACKWARD){
  3416. s->mv_dir = MV_DIR_BACKWARD;
  3417. s->mv_type = MV_TYPE_16X16;
  3418. s->mb_intra= 0;
  3419. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  3420. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  3421. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  3422. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  3423. }
  3424. if(mb_type&MB_TYPE_BIDIR){
  3425. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  3426. s->mv_type = MV_TYPE_16X16;
  3427. s->mb_intra= 0;
  3428. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  3429. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  3430. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  3431. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  3432. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb,
  3433. &dmin, &next_block, 0, 0);
  3434. }
  3435. if(mb_type&MB_TYPE_DIRECT){
  3436. int mx= s->b_direct_mv_table[xy][0];
  3437. int my= s->b_direct_mv_table[xy][1];
  3438. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  3439. s->mb_intra= 0;
  3440. #ifdef CONFIG_RISKY
  3441. ff_mpeg4_set_direct_mv(s, mx, my);
  3442. #endif
  3443. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb,
  3444. &dmin, &next_block, mx, my);
  3445. }
  3446. if(mb_type&MB_TYPE_INTRA){
  3447. s->mv_dir = 0;
  3448. s->mv_type = MV_TYPE_16X16;
  3449. s->mb_intra= 1;
  3450. s->mv[0][0][0] = 0;
  3451. s->mv[0][0][1] = 0;
  3452. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb,
  3453. &dmin, &next_block, 0, 0);
  3454. if(s->h263_pred || s->h263_aic){
  3455. if(best_s.mb_intra)
  3456. s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
  3457. else
  3458. ff_clean_intra_table_entries(s); //old mode?
  3459. }
  3460. }
  3461. if(s->flags & CODEC_FLAG_QP_RD){
  3462. if(best_s.mv_type==MV_TYPE_16X16 && !(best_s.mv_dir&MV_DIRECT)){
  3463. const int last_qp= backup_s.qscale;
  3464. int dquant, dir, qp, dc[6];
  3465. DCTELEM ac[6][16];
  3466. const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
  3467. assert(backup_s.dquant == 0);
  3468. //FIXME intra
  3469. s->mv_dir= best_s.mv_dir;
  3470. s->mv_type = MV_TYPE_16X16;
  3471. s->mb_intra= best_s.mb_intra;
  3472. s->mv[0][0][0] = best_s.mv[0][0][0];
  3473. s->mv[0][0][1] = best_s.mv[0][0][1];
  3474. s->mv[1][0][0] = best_s.mv[1][0][0];
  3475. s->mv[1][0][1] = best_s.mv[1][0][1];
  3476. dir= s->pict_type == B_TYPE ? 2 : 1;
  3477. if(last_qp + dir > s->avctx->qmax) dir= -dir;
  3478. for(dquant= dir; dquant<=2 && dquant>=-2; dquant += dir){
  3479. qp= last_qp + dquant;
  3480. if(qp < s->avctx->qmin || qp > s->avctx->qmax)
  3481. break;
  3482. backup_s.dquant= dquant;
  3483. if(s->mb_intra){
  3484. for(i=0; i<6; i++){
  3485. dc[i]= s->dc_val[0][ s->block_index[i] ];
  3486. memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(DCTELEM)*16);
  3487. }
  3488. }
  3489. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
  3490. &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
  3491. if(best_s.qscale != qp){
  3492. if(s->mb_intra){
  3493. for(i=0; i<6; i++){
  3494. s->dc_val[0][ s->block_index[i] ]= dc[i];
  3495. memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(DCTELEM)*16);
  3496. }
  3497. }
  3498. if(dir > 0 && dquant==dir){
  3499. dquant= 0;
  3500. dir= -dir;
  3501. }else
  3502. break;
  3503. }
  3504. }
  3505. qp= best_s.qscale;
  3506. s->current_picture.qscale_table[xy]= qp;
  3507. }
  3508. }
  3509. copy_context_after_encode(s, &best_s, -1);
  3510. pb_bits_count= get_bit_count(&s->pb);
  3511. flush_put_bits(&s->pb);
  3512. ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  3513. s->pb= backup_s.pb;
  3514. if(s->data_partitioning){
  3515. pb2_bits_count= get_bit_count(&s->pb2);
  3516. flush_put_bits(&s->pb2);
  3517. ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  3518. s->pb2= backup_s.pb2;
  3519. tex_pb_bits_count= get_bit_count(&s->tex_pb);
  3520. flush_put_bits(&s->tex_pb);
  3521. ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  3522. s->tex_pb= backup_s.tex_pb;
  3523. }
  3524. s->last_bits= get_bit_count(&s->pb);
  3525. #ifdef CONFIG_RISKY
  3526. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
  3527. ff_h263_update_motion_val(s);
  3528. #endif
  3529. if(next_block==0){
  3530. s->dsp.put_pixels_tab[0][0](s->dest[0], s->me.scratchpad , s->linesize ,16);
  3531. s->dsp.put_pixels_tab[1][0](s->dest[1], s->me.scratchpad + 16, s->uvlinesize, 8);
  3532. s->dsp.put_pixels_tab[1][0](s->dest[2], s->me.scratchpad + 24, s->uvlinesize, 8);
  3533. }
  3534. if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
  3535. MPV_decode_mb(s, s->block);
  3536. } else {
  3537. int motion_x, motion_y;
  3538. s->mv_type=MV_TYPE_16X16;
  3539. // only one MB-Type possible
  3540. switch(mb_type){
  3541. case MB_TYPE_INTRA:
  3542. s->mv_dir = 0;
  3543. s->mb_intra= 1;
  3544. motion_x= s->mv[0][0][0] = 0;
  3545. motion_y= s->mv[0][0][1] = 0;
  3546. break;
  3547. case MB_TYPE_INTER:
  3548. s->mv_dir = MV_DIR_FORWARD;
  3549. s->mb_intra= 0;
  3550. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  3551. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  3552. break;
  3553. case MB_TYPE_INTER4V:
  3554. s->mv_dir = MV_DIR_FORWARD;
  3555. s->mv_type = MV_TYPE_8X8;
  3556. s->mb_intra= 0;
  3557. for(i=0; i<4; i++){
  3558. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  3559. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  3560. }
  3561. motion_x= motion_y= 0;
  3562. break;
  3563. case MB_TYPE_DIRECT:
  3564. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  3565. s->mb_intra= 0;
  3566. motion_x=s->b_direct_mv_table[xy][0];
  3567. motion_y=s->b_direct_mv_table[xy][1];
  3568. #ifdef CONFIG_RISKY
  3569. ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
  3570. #endif
  3571. break;
  3572. case MB_TYPE_BIDIR:
  3573. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  3574. s->mb_intra= 0;
  3575. motion_x=0;
  3576. motion_y=0;
  3577. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  3578. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  3579. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  3580. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  3581. break;
  3582. case MB_TYPE_BACKWARD:
  3583. s->mv_dir = MV_DIR_BACKWARD;
  3584. s->mb_intra= 0;
  3585. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  3586. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  3587. break;
  3588. case MB_TYPE_FORWARD:
  3589. s->mv_dir = MV_DIR_FORWARD;
  3590. s->mb_intra= 0;
  3591. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  3592. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  3593. // printf(" %d %d ", motion_x, motion_y);
  3594. break;
  3595. default:
  3596. motion_x=motion_y=0; //gcc warning fix
  3597. av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
  3598. }
  3599. encode_mb(s, motion_x, motion_y);
  3600. // RAL: Update last macrobloc type
  3601. s->last_mv_dir = s->mv_dir;
  3602. #ifdef CONFIG_RISKY
  3603. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
  3604. ff_h263_update_motion_val(s);
  3605. #endif
  3606. MPV_decode_mb(s, s->block);
  3607. }
  3608. /* clean the MV table in IPS frames for direct mode in B frames */
  3609. if(s->mb_intra /* && I,P,S_TYPE */){
  3610. s->p_mv_table[xy][0]=0;
  3611. s->p_mv_table[xy][1]=0;
  3612. }
  3613. if(s->flags&CODEC_FLAG_PSNR){
  3614. int w= 16;
  3615. int h= 16;
  3616. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  3617. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  3618. s->current_picture_ptr->error[0] += sse(
  3619. s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
  3620. s->dest[0], w, h, s->linesize);
  3621. s->current_picture_ptr->error[1] += sse(
  3622. s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  3623. s->dest[1], w>>1, h>>1, s->uvlinesize);
  3624. s->current_picture_ptr->error[2] += sse(
  3625. s, s->new_picture .data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  3626. s->dest[2], w>>1, h>>1, s->uvlinesize);
  3627. }
  3628. //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, get_bit_count(&s->pb));
  3629. }
  3630. }
  3631. emms_c();
  3632. #ifdef CONFIG_RISKY
  3633. if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
  3634. ff_mpeg4_merge_partitions(s);
  3635. if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
  3636. msmpeg4_encode_ext_header(s);
  3637. if(s->codec_id==CODEC_ID_MPEG4)
  3638. ff_mpeg4_stuffing(&s->pb);
  3639. #endif
  3640. //if (s->gob_number)
  3641. // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
  3642. /* Send the last GOB if RTP */
  3643. if (s->rtp_mode) {
  3644. flush_put_bits(&s->pb);
  3645. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  3646. /* Call the RTP callback to send the last GOB */
  3647. if (s->rtp_callback)
  3648. s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
  3649. s->ptr_lastgob = pbBufPtr(&s->pb);
  3650. //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
  3651. }
  3652. }
  3653. void ff_denoise_dct(MpegEncContext *s, DCTELEM *block){
  3654. const int intra= s->mb_intra;
  3655. int i;
  3656. s->dct_count[intra]++;
  3657. for(i=0; i<64; i++){
  3658. int level= block[i];
  3659. if(level){
  3660. if(level>0){
  3661. s->dct_error_sum[intra][i] += level;
  3662. level -= s->dct_offset[intra][i];
  3663. if(level<0) level=0;
  3664. }else{
  3665. s->dct_error_sum[intra][i] -= level;
  3666. level += s->dct_offset[intra][i];
  3667. if(level>0) level=0;
  3668. }
  3669. block[i]= level;
  3670. }
  3671. }
  3672. }
  3673. static int dct_quantize_trellis_c(MpegEncContext *s,
  3674. DCTELEM *block, int n,
  3675. int qscale, int *overflow){
  3676. const int *qmat;
  3677. const uint8_t *scantable= s->intra_scantable.scantable;
  3678. int max=0;
  3679. unsigned int threshold1, threshold2;
  3680. int bias=0;
  3681. int run_tab[65];
  3682. int level_tab[65];
  3683. int score_tab[65];
  3684. int last_run=0;
  3685. int last_level=0;
  3686. int last_score= 0;
  3687. int last_i= 0;
  3688. int not_coded_score= 0;
  3689. int coeff[3][64];
  3690. int coeff_count[64];
  3691. int qmul, qadd, start_i, last_non_zero, i, dc;
  3692. const int esc_length= s->ac_esc_length;
  3693. uint8_t * length;
  3694. uint8_t * last_length;
  3695. int score_limit=0;
  3696. int left_limit= 0;
  3697. const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
  3698. const int patch_table= s->out_format == FMT_MPEG1 && !s->mb_intra;
  3699. s->dsp.fdct (block);
  3700. if(s->dct_error_sum)
  3701. ff_denoise_dct(s, block);
  3702. qmul= qscale*16;
  3703. qadd= ((qscale-1)|1)*8;
  3704. if (s->mb_intra) {
  3705. int q;
  3706. if (!s->h263_aic) {
  3707. if (n < 4)
  3708. q = s->y_dc_scale;
  3709. else
  3710. q = s->c_dc_scale;
  3711. q = q << 3;
  3712. } else{
  3713. /* For AIC we skip quant/dequant of INTRADC */
  3714. q = 1 << 3;
  3715. qadd=0;
  3716. }
  3717. /* note: block[0] is assumed to be positive */
  3718. block[0] = (block[0] + (q >> 1)) / q;
  3719. start_i = 1;
  3720. last_non_zero = 0;
  3721. qmat = s->q_intra_matrix[qscale];
  3722. if(s->mpeg_quant || s->out_format == FMT_MPEG1)
  3723. bias= 1<<(QMAT_SHIFT-1);
  3724. length = s->intra_ac_vlc_length;
  3725. last_length= s->intra_ac_vlc_last_length;
  3726. } else {
  3727. start_i = 0;
  3728. last_non_zero = -1;
  3729. qmat = s->q_inter_matrix[qscale];
  3730. length = s->inter_ac_vlc_length;
  3731. last_length= s->inter_ac_vlc_last_length;
  3732. }
  3733. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  3734. threshold2= (threshold1<<1);
  3735. for(i=start_i; i<64; i++) {
  3736. const int j = scantable[i];
  3737. const int k= i-start_i;
  3738. int level = block[j];
  3739. level = level * qmat[j];
  3740. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  3741. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  3742. if(((unsigned)(level+threshold1))>threshold2){
  3743. if(level>0){
  3744. level= (bias + level)>>QMAT_SHIFT;
  3745. coeff[0][k]= level;
  3746. coeff[1][k]= level-1;
  3747. // coeff[2][k]= level-2;
  3748. }else{
  3749. level= (bias - level)>>QMAT_SHIFT;
  3750. coeff[0][k]= -level;
  3751. coeff[1][k]= -level+1;
  3752. // coeff[2][k]= -level+2;
  3753. }
  3754. coeff_count[k]= FFMIN(level, 2);
  3755. assert(coeff_count[k]);
  3756. max |=level;
  3757. last_non_zero = i;
  3758. }else{
  3759. coeff[0][k]= (level>>31)|1;
  3760. coeff_count[k]= 1;
  3761. }
  3762. }
  3763. *overflow= s->max_qcoeff < max; //overflow might have happend
  3764. if(last_non_zero < start_i){
  3765. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  3766. return last_non_zero;
  3767. }
  3768. score_tab[0]= 0;
  3769. if(patch_table){
  3770. // length[UNI_AC_ENC_INDEX(0, 63)]=
  3771. // length[UNI_AC_ENC_INDEX(0, 65)]= 2;
  3772. }
  3773. for(i=0; i<=last_non_zero - start_i; i++){
  3774. int level_index, run, j;
  3775. const int dct_coeff= block[ scantable[i + start_i] ];
  3776. const int zero_distoration= dct_coeff*dct_coeff;
  3777. int best_score=256*256*256*120;
  3778. last_score += zero_distoration;
  3779. not_coded_score += zero_distoration;
  3780. for(level_index=0; level_index < coeff_count[i]; level_index++){
  3781. int distoration;
  3782. int level= coeff[level_index][i];
  3783. int unquant_coeff;
  3784. assert(level);
  3785. if(s->out_format == FMT_H263){
  3786. if(level>0){
  3787. unquant_coeff= level*qmul + qadd;
  3788. }else{
  3789. unquant_coeff= level*qmul - qadd;
  3790. }
  3791. }else{ //MPEG1
  3792. j= s->dsp.idct_permutation[ scantable[i + start_i] ]; //FIXME optimize
  3793. if(s->mb_intra){
  3794. if (level < 0) {
  3795. unquant_coeff = (int)((-level) * qscale * s->intra_matrix[j]) >> 3;
  3796. unquant_coeff = -((unquant_coeff - 1) | 1);
  3797. } else {
  3798. unquant_coeff = (int)( level * qscale * s->intra_matrix[j]) >> 3;
  3799. unquant_coeff = (unquant_coeff - 1) | 1;
  3800. }
  3801. }else{
  3802. if (level < 0) {
  3803. unquant_coeff = ((((-level) << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
  3804. unquant_coeff = -((unquant_coeff - 1) | 1);
  3805. } else {
  3806. unquant_coeff = ((( level << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
  3807. unquant_coeff = (unquant_coeff - 1) | 1;
  3808. }
  3809. }
  3810. unquant_coeff<<= 3;
  3811. }
  3812. distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff);
  3813. level+=64;
  3814. if((level&(~127)) == 0){
  3815. for(run=0; run<=i - left_limit; run++){
  3816. int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3817. score += score_tab[i-run];
  3818. if(score < best_score){
  3819. best_score=
  3820. score_tab[i+1]= score;
  3821. run_tab[i+1]= run;
  3822. level_tab[i+1]= level-64;
  3823. }
  3824. }
  3825. if(s->out_format == FMT_H263){
  3826. for(run=0; run<=i - left_limit; run++){
  3827. int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3828. score += score_tab[i-run];
  3829. if(score < last_score){
  3830. last_score= score;
  3831. last_run= run;
  3832. last_level= level-64;
  3833. last_i= i+1;
  3834. }
  3835. }
  3836. }
  3837. }else{
  3838. distoration += esc_length*lambda;
  3839. for(run=0; run<=i - left_limit; run++){
  3840. int score= distoration + score_tab[i-run];
  3841. if(score < best_score){
  3842. best_score=
  3843. score_tab[i+1]= score;
  3844. run_tab[i+1]= run;
  3845. level_tab[i+1]= level-64;
  3846. }
  3847. }
  3848. if(s->out_format == FMT_H263){
  3849. for(run=0; run<=i - left_limit; run++){
  3850. int score= distoration + score_tab[i-run];
  3851. if(score < last_score){
  3852. last_score= score;
  3853. last_run= run;
  3854. last_level= level-64;
  3855. last_i= i+1;
  3856. }
  3857. }
  3858. }
  3859. }
  3860. }
  3861. for(j=left_limit; j<=i; j++){
  3862. score_tab[j] += zero_distoration;
  3863. }
  3864. score_limit+= zero_distoration;
  3865. if(score_tab[i+1] < score_limit)
  3866. score_limit= score_tab[i+1];
  3867. //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
  3868. while(score_tab[ left_limit ] > score_limit + lambda) left_limit++;
  3869. if(patch_table){
  3870. // length[UNI_AC_ENC_INDEX(0, 63)]=
  3871. // length[UNI_AC_ENC_INDEX(0, 65)]= 3;
  3872. }
  3873. }
  3874. if(s->out_format != FMT_H263){
  3875. last_score= 256*256*256*120;
  3876. for(i= left_limit; i<=last_non_zero - start_i + 1; i++){
  3877. int score= score_tab[i];
  3878. if(i) score += lambda*2; //FIXME exacter?
  3879. if(score < last_score){
  3880. last_score= score;
  3881. last_i= i;
  3882. last_level= level_tab[i];
  3883. last_run= run_tab[i];
  3884. }
  3885. }
  3886. }
  3887. s->coded_score[n] = last_score - not_coded_score;
  3888. dc= block[0];
  3889. last_non_zero= last_i - 1 + start_i;
  3890. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  3891. if(last_non_zero < start_i)
  3892. return last_non_zero;
  3893. if(last_non_zero == 0 && start_i == 0){
  3894. int best_level= 0;
  3895. int best_score= dc * dc;
  3896. for(i=0; i<coeff_count[0]; i++){
  3897. int level= coeff[i][0];
  3898. int unquant_coeff, score, distoration;
  3899. if(s->out_format == FMT_H263){
  3900. if(level>0){
  3901. unquant_coeff= (level*qmul + qadd)>>3;
  3902. }else{
  3903. unquant_coeff= (level*qmul - qadd)>>3;
  3904. }
  3905. }else{ //MPEG1
  3906. if (level < 0) {
  3907. unquant_coeff = ((((-level) << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
  3908. unquant_coeff = -((unquant_coeff - 1) | 1);
  3909. } else {
  3910. unquant_coeff = ((( level << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
  3911. unquant_coeff = (unquant_coeff - 1) | 1;
  3912. }
  3913. }
  3914. unquant_coeff = (unquant_coeff + 4) >> 3;
  3915. unquant_coeff<<= 3 + 3;
  3916. distoration= (unquant_coeff - dc) * (unquant_coeff - dc);
  3917. level+=64;
  3918. if((level&(~127)) == 0)
  3919. score= distoration + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
  3920. else
  3921. score= distoration + esc_length*lambda;
  3922. if(score < best_score){
  3923. best_score= score;
  3924. best_level= level - 64;
  3925. }
  3926. }
  3927. block[0]= best_level;
  3928. s->coded_score[n] = best_score - dc*dc;
  3929. if(best_level == 0) return -1;
  3930. else return last_non_zero;
  3931. }
  3932. i= last_i;
  3933. assert(last_level);
  3934. //FIXME use permutated scantable
  3935. block[ s->dsp.idct_permutation[ scantable[last_non_zero] ] ]= last_level;
  3936. i -= last_run + 1;
  3937. for(;i>0 ; i -= run_tab[i] + 1){
  3938. const int j= s->dsp.idct_permutation[ scantable[i - 1 + start_i] ];
  3939. block[j]= level_tab[i];
  3940. assert(block[j]);
  3941. }
  3942. return last_non_zero;
  3943. }
  3944. static int dct_quantize_c(MpegEncContext *s,
  3945. DCTELEM *block, int n,
  3946. int qscale, int *overflow)
  3947. {
  3948. int i, j, level, last_non_zero, q;
  3949. const int *qmat;
  3950. const uint8_t *scantable= s->intra_scantable.scantable;
  3951. int bias;
  3952. int max=0;
  3953. unsigned int threshold1, threshold2;
  3954. s->dsp.fdct (block);
  3955. if(s->dct_error_sum)
  3956. ff_denoise_dct(s, block);
  3957. if (s->mb_intra) {
  3958. if (!s->h263_aic) {
  3959. if (n < 4)
  3960. q = s->y_dc_scale;
  3961. else
  3962. q = s->c_dc_scale;
  3963. q = q << 3;
  3964. } else
  3965. /* For AIC we skip quant/dequant of INTRADC */
  3966. q = 1 << 3;
  3967. /* note: block[0] is assumed to be positive */
  3968. block[0] = (block[0] + (q >> 1)) / q;
  3969. i = 1;
  3970. last_non_zero = 0;
  3971. qmat = s->q_intra_matrix[qscale];
  3972. bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  3973. } else {
  3974. i = 0;
  3975. last_non_zero = -1;
  3976. qmat = s->q_inter_matrix[qscale];
  3977. bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  3978. }
  3979. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  3980. threshold2= (threshold1<<1);
  3981. for(;i<64;i++) {
  3982. j = scantable[i];
  3983. level = block[j];
  3984. level = level * qmat[j];
  3985. // if( bias+level >= (1<<QMAT_SHIFT)
  3986. // || bias-level >= (1<<QMAT_SHIFT)){
  3987. if(((unsigned)(level+threshold1))>threshold2){
  3988. if(level>0){
  3989. level= (bias + level)>>QMAT_SHIFT;
  3990. block[j]= level;
  3991. }else{
  3992. level= (bias - level)>>QMAT_SHIFT;
  3993. block[j]= -level;
  3994. }
  3995. max |=level;
  3996. last_non_zero = i;
  3997. }else{
  3998. block[j]=0;
  3999. }
  4000. }
  4001. *overflow= s->max_qcoeff < max; //overflow might have happend
  4002. /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
  4003. if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
  4004. ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
  4005. return last_non_zero;
  4006. }
  4007. #endif //CONFIG_ENCODERS
  4008. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  4009. DCTELEM *block, int n, int qscale)
  4010. {
  4011. int i, level, nCoeffs;
  4012. const uint16_t *quant_matrix;
  4013. nCoeffs= s->block_last_index[n];
  4014. if (s->mb_intra) {
  4015. if (n < 4)
  4016. block[0] = block[0] * s->y_dc_scale;
  4017. else
  4018. block[0] = block[0] * s->c_dc_scale;
  4019. /* XXX: only mpeg1 */
  4020. quant_matrix = s->intra_matrix;
  4021. for(i=1;i<=nCoeffs;i++) {
  4022. int j= s->intra_scantable.permutated[i];
  4023. level = block[j];
  4024. if (level) {
  4025. if (level < 0) {
  4026. level = -level;
  4027. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  4028. level = (level - 1) | 1;
  4029. level = -level;
  4030. } else {
  4031. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  4032. level = (level - 1) | 1;
  4033. }
  4034. #ifdef PARANOID
  4035. if (level < -2048 || level > 2047)
  4036. fprintf(stderr, "unquant error %d %d\n", i, level);
  4037. #endif
  4038. block[j] = level;
  4039. }
  4040. }
  4041. } else {
  4042. i = 0;
  4043. quant_matrix = s->inter_matrix;
  4044. for(;i<=nCoeffs;i++) {
  4045. int j= s->intra_scantable.permutated[i];
  4046. level = block[j];
  4047. if (level) {
  4048. if (level < 0) {
  4049. level = -level;
  4050. level = (((level << 1) + 1) * qscale *
  4051. ((int) (quant_matrix[j]))) >> 4;
  4052. level = (level - 1) | 1;
  4053. level = -level;
  4054. } else {
  4055. level = (((level << 1) + 1) * qscale *
  4056. ((int) (quant_matrix[j]))) >> 4;
  4057. level = (level - 1) | 1;
  4058. }
  4059. #ifdef PARANOID
  4060. if (level < -2048 || level > 2047)
  4061. fprintf(stderr, "unquant error %d %d\n", i, level);
  4062. #endif
  4063. block[j] = level;
  4064. }
  4065. }
  4066. }
  4067. }
  4068. static void dct_unquantize_mpeg2_c(MpegEncContext *s,
  4069. DCTELEM *block, int n, int qscale)
  4070. {
  4071. int i, level, nCoeffs;
  4072. const uint16_t *quant_matrix;
  4073. if(s->alternate_scan) nCoeffs= 63;
  4074. else nCoeffs= s->block_last_index[n];
  4075. if (s->mb_intra) {
  4076. if (n < 4)
  4077. block[0] = block[0] * s->y_dc_scale;
  4078. else
  4079. block[0] = block[0] * s->c_dc_scale;
  4080. quant_matrix = s->intra_matrix;
  4081. for(i=1;i<=nCoeffs;i++) {
  4082. int j= s->intra_scantable.permutated[i];
  4083. level = block[j];
  4084. if (level) {
  4085. if (level < 0) {
  4086. level = -level;
  4087. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  4088. level = -level;
  4089. } else {
  4090. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  4091. }
  4092. #ifdef PARANOID
  4093. if (level < -2048 || level > 2047)
  4094. fprintf(stderr, "unquant error %d %d\n", i, level);
  4095. #endif
  4096. block[j] = level;
  4097. }
  4098. }
  4099. } else {
  4100. int sum=-1;
  4101. i = 0;
  4102. quant_matrix = s->inter_matrix;
  4103. for(;i<=nCoeffs;i++) {
  4104. int j= s->intra_scantable.permutated[i];
  4105. level = block[j];
  4106. if (level) {
  4107. if (level < 0) {
  4108. level = -level;
  4109. level = (((level << 1) + 1) * qscale *
  4110. ((int) (quant_matrix[j]))) >> 4;
  4111. level = -level;
  4112. } else {
  4113. level = (((level << 1) + 1) * qscale *
  4114. ((int) (quant_matrix[j]))) >> 4;
  4115. }
  4116. #ifdef PARANOID
  4117. if (level < -2048 || level > 2047)
  4118. fprintf(stderr, "unquant error %d %d\n", i, level);
  4119. #endif
  4120. block[j] = level;
  4121. sum+=level;
  4122. }
  4123. }
  4124. block[63]^=sum&1;
  4125. }
  4126. }
  4127. static void dct_unquantize_h263_c(MpegEncContext *s,
  4128. DCTELEM *block, int n, int qscale)
  4129. {
  4130. int i, level, qmul, qadd;
  4131. int nCoeffs;
  4132. assert(s->block_last_index[n]>=0);
  4133. qadd = (qscale - 1) | 1;
  4134. qmul = qscale << 1;
  4135. if (s->mb_intra) {
  4136. if (!s->h263_aic) {
  4137. if (n < 4)
  4138. block[0] = block[0] * s->y_dc_scale;
  4139. else
  4140. block[0] = block[0] * s->c_dc_scale;
  4141. }else
  4142. qadd = 0;
  4143. i = 1;
  4144. nCoeffs= 63; //does not allways use zigzag table
  4145. } else {
  4146. i = 0;
  4147. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  4148. }
  4149. for(;i<=nCoeffs;i++) {
  4150. level = block[i];
  4151. if (level) {
  4152. if (level < 0) {
  4153. level = level * qmul - qadd;
  4154. } else {
  4155. level = level * qmul + qadd;
  4156. }
  4157. #ifdef PARANOID
  4158. if (level < -2048 || level > 2047)
  4159. fprintf(stderr, "unquant error %d %d\n", i, level);
  4160. #endif
  4161. block[i] = level;
  4162. }
  4163. }
  4164. }
  4165. static const AVOption mpeg4_options[] =
  4166. {
  4167. AVOPTION_CODEC_INT("bitrate", "desired video bitrate", bit_rate, 4, 240000000, 800000),
  4168. AVOPTION_CODEC_INT("ratetol", "number of bits the bitstream is allowed to diverge from the reference"
  4169. "the reference can be CBR (for CBR pass1) or VBR (for pass2)",
  4170. bit_rate_tolerance, 4, 240000000, 8000),
  4171. AVOPTION_CODEC_INT("qmin", "minimum quantizer", qmin, 1, 31, 2),
  4172. AVOPTION_CODEC_INT("qmax", "maximum quantizer", qmax, 1, 31, 31),
  4173. AVOPTION_CODEC_STRING("rc_eq", "rate control equation",
  4174. rc_eq, "tex^qComp,option1,options2", 0),
  4175. AVOPTION_CODEC_INT("rc_minrate", "rate control minimum bitrate",
  4176. rc_min_rate, 4, 24000000, 0),
  4177. AVOPTION_CODEC_INT("rc_maxrate", "rate control maximum bitrate",
  4178. rc_max_rate, 4, 24000000, 0),
  4179. AVOPTION_CODEC_DOUBLE("rc_buf_aggresivity", "rate control buffer aggresivity",
  4180. rc_buffer_aggressivity, 4, 24000000, 0),
  4181. AVOPTION_CODEC_DOUBLE("rc_initial_cplx", "initial complexity for pass1 ratecontrol",
  4182. rc_initial_cplx, 0., 9999999., 0),
  4183. AVOPTION_CODEC_DOUBLE("i_quant_factor", "qscale factor between p and i frames",
  4184. i_quant_factor, 0., 0., 0),
  4185. AVOPTION_CODEC_DOUBLE("i_quant_offset", "qscale offset between p and i frames",
  4186. i_quant_factor, -999999., 999999., 0),
  4187. AVOPTION_CODEC_INT("dct_algo", "dct alghorithm",
  4188. dct_algo, 0, 5, 0), // fixme - "Auto,FastInt,Int,MMX,MLib,Altivec"
  4189. AVOPTION_CODEC_DOUBLE("lumi_masking", "luminance masking",
  4190. lumi_masking, 0., 999999., 0),
  4191. AVOPTION_CODEC_DOUBLE("temporal_cplx_masking", "temporary complexity masking",
  4192. temporal_cplx_masking, 0., 999999., 0),
  4193. AVOPTION_CODEC_DOUBLE("spatial_cplx_masking", "spatial complexity masking",
  4194. spatial_cplx_masking, 0., 999999., 0),
  4195. AVOPTION_CODEC_DOUBLE("p_masking", "p block masking",
  4196. p_masking, 0., 999999., 0),
  4197. AVOPTION_CODEC_DOUBLE("dark_masking", "darkness masking",
  4198. dark_masking, 0., 999999., 0),
  4199. AVOPTION_CODEC_INT("idct_algo", "idct alghorithm",
  4200. idct_algo, 0, 8, 0), // fixme - "Auto,Int,Simple,SimpleMMX,LibMPEG2MMX,PS2,MLib,ARM,Altivec"
  4201. AVOPTION_CODEC_INT("mb_qmin", "minimum MB quantizer",
  4202. mb_qmin, 0, 8, 0),
  4203. AVOPTION_CODEC_INT("mb_qmax", "maximum MB quantizer",
  4204. mb_qmin, 0, 8, 0),
  4205. AVOPTION_CODEC_INT("me_cmp", "ME compare function",
  4206. me_cmp, 0, 24000000, 0),
  4207. AVOPTION_CODEC_INT("me_sub_cmp", "subpixel ME compare function",
  4208. me_sub_cmp, 0, 24000000, 0),
  4209. AVOPTION_CODEC_INT("dia_size", "ME diamond size & shape",
  4210. dia_size, 0, 24000000, 0),
  4211. AVOPTION_CODEC_INT("last_predictor_count", "amount of previous MV predictors",
  4212. last_predictor_count, 0, 24000000, 0),
  4213. AVOPTION_CODEC_INT("pre_me", "pre pass for ME",
  4214. pre_me, 0, 24000000, 0),
  4215. AVOPTION_CODEC_INT("me_pre_cmp", "ME pre pass compare function",
  4216. me_pre_cmp, 0, 24000000, 0),
  4217. AVOPTION_CODEC_INT("me_range", "maximum ME search range",
  4218. me_range, 0, 24000000, 0),
  4219. AVOPTION_CODEC_INT("pre_dia_size", "ME pre pass diamod size & shape",
  4220. pre_dia_size, 0, 24000000, 0),
  4221. AVOPTION_CODEC_INT("me_subpel_quality", "subpel ME quality",
  4222. me_subpel_quality, 0, 24000000, 0),
  4223. AVOPTION_CODEC_INT("me_range", "maximum ME search range",
  4224. me_range, 0, 24000000, 0),
  4225. AVOPTION_CODEC_FLAG("psnr", "calculate PSNR of compressed frames",
  4226. flags, CODEC_FLAG_PSNR, 0),
  4227. AVOPTION_CODEC_RCOVERRIDE("rc_override", "ratecontrol override (=startframe,endframe,qscale,quality_factor)",
  4228. rc_override),
  4229. AVOPTION_SUB(avoptions_common),
  4230. AVOPTION_END()
  4231. };
  4232. #ifdef CONFIG_ENCODERS
  4233. AVCodec mpeg1video_encoder = {
  4234. "mpeg1video",
  4235. CODEC_TYPE_VIDEO,
  4236. CODEC_ID_MPEG1VIDEO,
  4237. sizeof(MpegEncContext),
  4238. MPV_encode_init,
  4239. MPV_encode_picture,
  4240. MPV_encode_end,
  4241. };
  4242. #ifdef CONFIG_RISKY
  4243. AVCodec mpeg2video_encoder = {
  4244. "mpeg2video",
  4245. CODEC_TYPE_VIDEO,
  4246. CODEC_ID_MPEG2VIDEO,
  4247. sizeof(MpegEncContext),
  4248. MPV_encode_init,
  4249. MPV_encode_picture,
  4250. MPV_encode_end,
  4251. };
  4252. AVCodec h263_encoder = {
  4253. "h263",
  4254. CODEC_TYPE_VIDEO,
  4255. CODEC_ID_H263,
  4256. sizeof(MpegEncContext),
  4257. MPV_encode_init,
  4258. MPV_encode_picture,
  4259. MPV_encode_end,
  4260. };
  4261. AVCodec h263p_encoder = {
  4262. "h263p",
  4263. CODEC_TYPE_VIDEO,
  4264. CODEC_ID_H263P,
  4265. sizeof(MpegEncContext),
  4266. MPV_encode_init,
  4267. MPV_encode_picture,
  4268. MPV_encode_end,
  4269. };
  4270. AVCodec flv_encoder = {
  4271. "flv",
  4272. CODEC_TYPE_VIDEO,
  4273. CODEC_ID_FLV1,
  4274. sizeof(MpegEncContext),
  4275. MPV_encode_init,
  4276. MPV_encode_picture,
  4277. MPV_encode_end,
  4278. };
  4279. AVCodec rv10_encoder = {
  4280. "rv10",
  4281. CODEC_TYPE_VIDEO,
  4282. CODEC_ID_RV10,
  4283. sizeof(MpegEncContext),
  4284. MPV_encode_init,
  4285. MPV_encode_picture,
  4286. MPV_encode_end,
  4287. };
  4288. AVCodec mpeg4_encoder = {
  4289. "mpeg4",
  4290. CODEC_TYPE_VIDEO,
  4291. CODEC_ID_MPEG4,
  4292. sizeof(MpegEncContext),
  4293. MPV_encode_init,
  4294. MPV_encode_picture,
  4295. MPV_encode_end,
  4296. .options = mpeg4_options,
  4297. };
  4298. AVCodec msmpeg4v1_encoder = {
  4299. "msmpeg4v1",
  4300. CODEC_TYPE_VIDEO,
  4301. CODEC_ID_MSMPEG4V1,
  4302. sizeof(MpegEncContext),
  4303. MPV_encode_init,
  4304. MPV_encode_picture,
  4305. MPV_encode_end,
  4306. .options = mpeg4_options,
  4307. };
  4308. AVCodec msmpeg4v2_encoder = {
  4309. "msmpeg4v2",
  4310. CODEC_TYPE_VIDEO,
  4311. CODEC_ID_MSMPEG4V2,
  4312. sizeof(MpegEncContext),
  4313. MPV_encode_init,
  4314. MPV_encode_picture,
  4315. MPV_encode_end,
  4316. .options = mpeg4_options,
  4317. };
  4318. AVCodec msmpeg4v3_encoder = {
  4319. "msmpeg4",
  4320. CODEC_TYPE_VIDEO,
  4321. CODEC_ID_MSMPEG4V3,
  4322. sizeof(MpegEncContext),
  4323. MPV_encode_init,
  4324. MPV_encode_picture,
  4325. MPV_encode_end,
  4326. .options = mpeg4_options,
  4327. };
  4328. AVCodec wmv1_encoder = {
  4329. "wmv1",
  4330. CODEC_TYPE_VIDEO,
  4331. CODEC_ID_WMV1,
  4332. sizeof(MpegEncContext),
  4333. MPV_encode_init,
  4334. MPV_encode_picture,
  4335. MPV_encode_end,
  4336. .options = mpeg4_options,
  4337. };
  4338. #endif
  4339. AVCodec mjpeg_encoder = {
  4340. "mjpeg",
  4341. CODEC_TYPE_VIDEO,
  4342. CODEC_ID_MJPEG,
  4343. sizeof(MpegEncContext),
  4344. MPV_encode_init,
  4345. MPV_encode_picture,
  4346. MPV_encode_end,
  4347. };
  4348. #endif //CONFIG_ENCODERS