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.

4577 lines
159KB

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