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.

5049 lines
176KB

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