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.

5083 lines
178KB

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