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.

4573 lines
159KB

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