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.

4599 lines
160KB

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