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.

4624 lines
160KB

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