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.

4521 lines
158KB

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