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.

4643 lines
161KB

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