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.

4641 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. int coded_pic_num=0;
  1223. if(s->reordered_input_picture[0])
  1224. coded_pic_num= s->reordered_input_picture[0]->coded_picture_number + 1;
  1225. for(i=1; i<MAX_PICTURE_COUNT; i++)
  1226. s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
  1227. s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
  1228. /* set next picture types & ordering */
  1229. if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
  1230. if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){
  1231. s->reordered_input_picture[0]= s->input_picture[0];
  1232. s->reordered_input_picture[0]->pict_type= I_TYPE;
  1233. s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
  1234. }else{
  1235. int b_frames;
  1236. if(s->flags&CODEC_FLAG_PASS2){
  1237. for(i=0; i<s->max_b_frames+1; i++){
  1238. int pict_num= s->input_picture[0]->display_picture_number + i;
  1239. int pict_type= s->rc_context.entry[pict_num].new_pict_type;
  1240. s->input_picture[i]->pict_type= pict_type;
  1241. if(i + 1 >= s->rc_context.num_entries) break;
  1242. }
  1243. }
  1244. if(s->input_picture[0]->pict_type){
  1245. /* user selected pict_type */
  1246. for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){
  1247. if(s->input_picture[b_frames]->pict_type!=B_TYPE) break;
  1248. }
  1249. if(b_frames > s->max_b_frames){
  1250. fprintf(stderr, "warning, too many bframes in a row\n");
  1251. b_frames = s->max_b_frames;
  1252. }
  1253. }else if(s->b_frame_strategy==0){
  1254. b_frames= s->max_b_frames;
  1255. while(b_frames && !s->input_picture[b_frames]) b_frames--;
  1256. }else if(s->b_frame_strategy==1){
  1257. for(i=1; i<s->max_b_frames+1; i++){
  1258. if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){
  1259. s->input_picture[i]->b_frame_score=
  1260. get_intra_count(s, s->input_picture[i ]->data[0],
  1261. s->input_picture[i-1]->data[0], s->linesize) + 1;
  1262. }
  1263. }
  1264. for(i=0; i<s->max_b_frames; i++){
  1265. if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
  1266. }
  1267. b_frames= FFMAX(0, i-1);
  1268. /* reset scores */
  1269. for(i=0; i<b_frames+1; i++){
  1270. s->input_picture[i]->b_frame_score=0;
  1271. }
  1272. }else{
  1273. fprintf(stderr, "illegal b frame strategy\n");
  1274. b_frames=0;
  1275. }
  1276. emms_c();
  1277. //static int b_count=0;
  1278. //b_count+= b_frames;
  1279. //printf("b_frames: %d\n", b_count);
  1280. s->reordered_input_picture[0]= s->input_picture[b_frames];
  1281. if( s->picture_in_gop_number + b_frames >= s->gop_size
  1282. || s->reordered_input_picture[0]->pict_type== I_TYPE)
  1283. s->reordered_input_picture[0]->pict_type= I_TYPE;
  1284. else
  1285. s->reordered_input_picture[0]->pict_type= P_TYPE;
  1286. s->reordered_input_picture[0]->coded_picture_number= coded_pic_num;
  1287. for(i=0; i<b_frames; i++){
  1288. coded_pic_num++;
  1289. s->reordered_input_picture[i+1]= s->input_picture[i];
  1290. s->reordered_input_picture[i+1]->pict_type= B_TYPE;
  1291. s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num;
  1292. }
  1293. }
  1294. }
  1295. if(s->reordered_input_picture[0]){
  1296. s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE ? 3 : 0;
  1297. s->new_picture= *s->reordered_input_picture[0];
  1298. if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
  1299. // input is a shared pix, so we cant modifiy it -> alloc a new one & ensure that the shared one is reuseable
  1300. int i= find_unused_picture(s, 0);
  1301. Picture *pic= &s->picture[i];
  1302. /* mark us unused / free shared pic */
  1303. for(i=0; i<4; i++)
  1304. s->reordered_input_picture[0]->data[i]= NULL;
  1305. s->reordered_input_picture[0]->type= 0;
  1306. //FIXME bad, copy * except
  1307. pic->pict_type = s->reordered_input_picture[0]->pict_type;
  1308. pic->quality = s->reordered_input_picture[0]->quality;
  1309. pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number;
  1310. pic->reference = s->reordered_input_picture[0]->reference;
  1311. pic->pts = s->reordered_input_picture[0]->pts;
  1312. alloc_picture(s, pic, 0);
  1313. s->current_picture_ptr= pic;
  1314. }else{
  1315. // input is not a shared pix -> reuse buffer for current_pix
  1316. assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
  1317. || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
  1318. s->current_picture_ptr= s->reordered_input_picture[0];
  1319. for(i=0; i<4; i++){
  1320. //reverse the +16 we did before storing the input
  1321. s->current_picture_ptr->data[i]-=16;
  1322. }
  1323. }
  1324. s->current_picture= *s->current_picture_ptr;
  1325. s->picture_number= s->new_picture.display_picture_number;
  1326. //printf("dpn:%d\n", s->picture_number);
  1327. }else{
  1328. memset(&s->new_picture, 0, sizeof(Picture));
  1329. }
  1330. }
  1331. int MPV_encode_picture(AVCodecContext *avctx,
  1332. unsigned char *buf, int buf_size, void *data)
  1333. {
  1334. MpegEncContext *s = avctx->priv_data;
  1335. AVFrame *pic_arg = data;
  1336. int i;
  1337. if(avctx->pix_fmt != PIX_FMT_YUV420P){
  1338. fprintf(stderr, "this codec supports only YUV420P\n");
  1339. return -1;
  1340. }
  1341. init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
  1342. s->picture_in_gop_number++;
  1343. load_input_picture(s, pic_arg);
  1344. select_input_picture(s);
  1345. /* output? */
  1346. if(s->new_picture.data[0]){
  1347. s->pict_type= s->new_picture.pict_type;
  1348. if (s->fixed_qscale){ /* the ratecontrol needs the last qscale so we dont touch it for CBR */
  1349. s->qscale= (int)(s->new_picture.quality+0.5);
  1350. assert(s->qscale);
  1351. }
  1352. //emms_c();
  1353. //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
  1354. MPV_frame_start(s, avctx);
  1355. encode_picture(s, s->picture_number);
  1356. avctx->real_pict_num = s->picture_number;
  1357. avctx->header_bits = s->header_bits;
  1358. avctx->mv_bits = s->mv_bits;
  1359. avctx->misc_bits = s->misc_bits;
  1360. avctx->i_tex_bits = s->i_tex_bits;
  1361. avctx->p_tex_bits = s->p_tex_bits;
  1362. avctx->i_count = s->i_count;
  1363. avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
  1364. avctx->skip_count = s->skip_count;
  1365. MPV_frame_end(s);
  1366. if (s->out_format == FMT_MJPEG)
  1367. mjpeg_picture_trailer(s);
  1368. if(s->flags&CODEC_FLAG_PASS1)
  1369. ff_write_pass1_stats(s);
  1370. for(i=0; i<4; i++){
  1371. avctx->error[i] += s->current_picture_ptr->error[i];
  1372. }
  1373. }
  1374. s->input_picture_number++;
  1375. flush_put_bits(&s->pb);
  1376. s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
  1377. s->total_bits += s->frame_bits;
  1378. avctx->frame_bits = s->frame_bits;
  1379. return pbBufPtr(&s->pb) - s->pb.buf;
  1380. }
  1381. #endif //CONFIG_ENCODERS
  1382. static inline void gmc1_motion(MpegEncContext *s,
  1383. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1384. int dest_offset,
  1385. uint8_t **ref_picture, int src_offset)
  1386. {
  1387. uint8_t *ptr;
  1388. int offset, src_x, src_y, linesize, uvlinesize;
  1389. int motion_x, motion_y;
  1390. int emu=0;
  1391. motion_x= s->sprite_offset[0][0];
  1392. motion_y= s->sprite_offset[0][1];
  1393. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  1394. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  1395. motion_x<<=(3-s->sprite_warping_accuracy);
  1396. motion_y<<=(3-s->sprite_warping_accuracy);
  1397. src_x = clip(src_x, -16, s->width);
  1398. if (src_x == s->width)
  1399. motion_x =0;
  1400. src_y = clip(src_y, -16, s->height);
  1401. if (src_y == s->height)
  1402. motion_y =0;
  1403. linesize = s->linesize;
  1404. uvlinesize = s->uvlinesize;
  1405. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  1406. dest_y+=dest_offset;
  1407. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1408. if(src_x<0 || src_y<0 || src_x + 17 >= s->h_edge_pos
  1409. || src_y + 17 >= s->v_edge_pos){
  1410. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  1411. ptr= s->edge_emu_buffer;
  1412. }
  1413. }
  1414. if((motion_x|motion_y)&7){
  1415. s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1416. s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1417. }else{
  1418. int dxy;
  1419. dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
  1420. if (s->no_rounding){
  1421. s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
  1422. }else{
  1423. s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
  1424. }
  1425. }
  1426. if(s->flags&CODEC_FLAG_GRAY) return;
  1427. motion_x= s->sprite_offset[1][0];
  1428. motion_y= s->sprite_offset[1][1];
  1429. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  1430. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  1431. motion_x<<=(3-s->sprite_warping_accuracy);
  1432. motion_y<<=(3-s->sprite_warping_accuracy);
  1433. src_x = clip(src_x, -8, s->width>>1);
  1434. if (src_x == s->width>>1)
  1435. motion_x =0;
  1436. src_y = clip(src_y, -8, s->height>>1);
  1437. if (src_y == s->height>>1)
  1438. motion_y =0;
  1439. offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
  1440. ptr = ref_picture[1] + offset;
  1441. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1442. if(src_x<0 || src_y<0 || src_x + 9 >= s->h_edge_pos>>1
  1443. || src_y + 9 >= s->v_edge_pos>>1){
  1444. 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);
  1445. ptr= s->edge_emu_buffer;
  1446. emu=1;
  1447. }
  1448. }
  1449. s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1450. ptr = ref_picture[2] + offset;
  1451. if(emu){
  1452. 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);
  1453. ptr= s->edge_emu_buffer;
  1454. }
  1455. s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1456. return;
  1457. }
  1458. static inline void gmc_motion(MpegEncContext *s,
  1459. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1460. int dest_offset,
  1461. uint8_t **ref_picture, int src_offset)
  1462. {
  1463. uint8_t *ptr;
  1464. int linesize, uvlinesize;
  1465. const int a= s->sprite_warping_accuracy;
  1466. int ox, oy;
  1467. linesize = s->linesize;
  1468. uvlinesize = s->uvlinesize;
  1469. ptr = ref_picture[0] + src_offset;
  1470. dest_y+=dest_offset;
  1471. ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
  1472. oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
  1473. s->dsp.gmc(dest_y, ptr, linesize, 16,
  1474. ox,
  1475. oy,
  1476. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1477. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1478. a+1, (1<<(2*a+1)) - s->no_rounding,
  1479. s->h_edge_pos, s->v_edge_pos);
  1480. s->dsp.gmc(dest_y+8, ptr, linesize, 16,
  1481. ox + s->sprite_delta[0][0]*8,
  1482. oy + s->sprite_delta[1][0]*8,
  1483. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1484. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1485. a+1, (1<<(2*a+1)) - s->no_rounding,
  1486. s->h_edge_pos, s->v_edge_pos);
  1487. if(s->flags&CODEC_FLAG_GRAY) return;
  1488. dest_cb+=dest_offset>>1;
  1489. dest_cr+=dest_offset>>1;
  1490. ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
  1491. oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
  1492. ptr = ref_picture[1] + (src_offset>>1);
  1493. s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
  1494. ox,
  1495. oy,
  1496. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1497. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1498. a+1, (1<<(2*a+1)) - s->no_rounding,
  1499. s->h_edge_pos>>1, s->v_edge_pos>>1);
  1500. ptr = ref_picture[2] + (src_offset>>1);
  1501. s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
  1502. ox,
  1503. oy,
  1504. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1505. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1506. a+1, (1<<(2*a+1)) - s->no_rounding,
  1507. s->h_edge_pos>>1, s->v_edge_pos>>1);
  1508. }
  1509. /**
  1510. * Copies a rectangular area of samples to a temporary buffer and replicates the boarder samples.
  1511. * @param buf destination buffer
  1512. * @param src source buffer
  1513. * @param linesize number of bytes between 2 vertically adjacent samples in both the source and destination buffers
  1514. * @param block_w width of block
  1515. * @param block_h height of block
  1516. * @param src_x x coordinate of the top left sample of the block in the source buffer
  1517. * @param src_y y coordinate of the top left sample of the block in the source buffer
  1518. * @param w width of the source buffer
  1519. * @param h height of the source buffer
  1520. */
  1521. void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h,
  1522. int src_x, int src_y, int w, int h){
  1523. int x, y;
  1524. int start_y, start_x, end_y, end_x;
  1525. if(src_y>= h){
  1526. src+= (h-1-src_y)*linesize;
  1527. src_y=h-1;
  1528. }else if(src_y<=-block_h){
  1529. src+= (1-block_h-src_y)*linesize;
  1530. src_y=1-block_h;
  1531. }
  1532. if(src_x>= w){
  1533. src+= (w-1-src_x);
  1534. src_x=w-1;
  1535. }else if(src_x<=-block_w){
  1536. src+= (1-block_w-src_x);
  1537. src_x=1-block_w;
  1538. }
  1539. start_y= FFMAX(0, -src_y);
  1540. start_x= FFMAX(0, -src_x);
  1541. end_y= FFMIN(block_h, h-src_y);
  1542. end_x= FFMIN(block_w, w-src_x);
  1543. // copy existing part
  1544. for(y=start_y; y<end_y; y++){
  1545. for(x=start_x; x<end_x; x++){
  1546. buf[x + y*linesize]= src[x + y*linesize];
  1547. }
  1548. }
  1549. //top
  1550. for(y=0; y<start_y; y++){
  1551. for(x=start_x; x<end_x; x++){
  1552. buf[x + y*linesize]= buf[x + start_y*linesize];
  1553. }
  1554. }
  1555. //bottom
  1556. for(y=end_y; y<block_h; y++){
  1557. for(x=start_x; x<end_x; x++){
  1558. buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
  1559. }
  1560. }
  1561. for(y=0; y<block_h; y++){
  1562. //left
  1563. for(x=0; x<start_x; x++){
  1564. buf[x + y*linesize]= buf[start_x + y*linesize];
  1565. }
  1566. //right
  1567. for(x=end_x; x<block_w; x++){
  1568. buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
  1569. }
  1570. }
  1571. }
  1572. /* apply one mpeg motion vector to the three components */
  1573. static inline void mpeg_motion(MpegEncContext *s,
  1574. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1575. int dest_offset,
  1576. uint8_t **ref_picture, int src_offset,
  1577. int field_based, op_pixels_func (*pix_op)[4],
  1578. int motion_x, int motion_y, int h)
  1579. {
  1580. uint8_t *ptr;
  1581. int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
  1582. int emu=0;
  1583. #if 0
  1584. if(s->quarter_sample)
  1585. {
  1586. motion_x>>=1;
  1587. motion_y>>=1;
  1588. }
  1589. #endif
  1590. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1591. src_x = s->mb_x * 16 + (motion_x >> 1);
  1592. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
  1593. /* WARNING: do no forget half pels */
  1594. height = s->height >> field_based;
  1595. v_edge_pos = s->v_edge_pos >> field_based;
  1596. src_x = clip(src_x, -16, s->width);
  1597. if (src_x == s->width)
  1598. dxy &= ~1;
  1599. src_y = clip(src_y, -16, height);
  1600. if (src_y == height)
  1601. dxy &= ~2;
  1602. linesize = s->current_picture.linesize[0] << field_based;
  1603. uvlinesize = s->current_picture.linesize[1] << field_based;
  1604. ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
  1605. dest_y += dest_offset;
  1606. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1607. if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->h_edge_pos
  1608. || src_y + (motion_y&1) + h > v_edge_pos){
  1609. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - src_offset, s->linesize, 17, 17+field_based, //FIXME linesize? and uv below
  1610. src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
  1611. ptr= s->edge_emu_buffer + src_offset;
  1612. emu=1;
  1613. }
  1614. }
  1615. pix_op[0][dxy](dest_y, ptr, linesize, h);
  1616. if(s->flags&CODEC_FLAG_GRAY) return;
  1617. if (s->out_format == FMT_H263) {
  1618. dxy = 0;
  1619. if ((motion_x & 3) != 0)
  1620. dxy |= 1;
  1621. if ((motion_y & 3) != 0)
  1622. dxy |= 2;
  1623. mx = motion_x >> 2;
  1624. my = motion_y >> 2;
  1625. } else {
  1626. mx = motion_x / 2;
  1627. my = motion_y / 2;
  1628. dxy = ((my & 1) << 1) | (mx & 1);
  1629. mx >>= 1;
  1630. my >>= 1;
  1631. }
  1632. src_x = s->mb_x * 8 + mx;
  1633. src_y = s->mb_y * (8 >> field_based) + my;
  1634. src_x = clip(src_x, -8, s->width >> 1);
  1635. if (src_x == (s->width >> 1))
  1636. dxy &= ~1;
  1637. src_y = clip(src_y, -8, height >> 1);
  1638. if (src_y == (height >> 1))
  1639. dxy &= ~2;
  1640. offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
  1641. ptr = ref_picture[1] + offset;
  1642. if(emu){
  1643. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
  1644. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1645. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1646. }
  1647. pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1648. ptr = ref_picture[2] + offset;
  1649. if(emu){
  1650. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
  1651. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1652. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1653. }
  1654. pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1655. }
  1656. static inline void qpel_motion(MpegEncContext *s,
  1657. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1658. int dest_offset,
  1659. uint8_t **ref_picture, int src_offset,
  1660. int field_based, op_pixels_func (*pix_op)[4],
  1661. qpel_mc_func (*qpix_op)[16],
  1662. int motion_x, int motion_y, int h)
  1663. {
  1664. uint8_t *ptr;
  1665. int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
  1666. int emu=0;
  1667. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  1668. src_x = s->mb_x * 16 + (motion_x >> 2);
  1669. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  1670. height = s->height >> field_based;
  1671. v_edge_pos = s->v_edge_pos >> field_based;
  1672. src_x = clip(src_x, -16, s->width);
  1673. if (src_x == s->width)
  1674. dxy &= ~3;
  1675. src_y = clip(src_y, -16, height);
  1676. if (src_y == height)
  1677. dxy &= ~12;
  1678. linesize = s->linesize << field_based;
  1679. uvlinesize = s->uvlinesize << field_based;
  1680. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  1681. dest_y += dest_offset;
  1682. //printf("%d %d %d\n", src_x, src_y, dxy);
  1683. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1684. if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->h_edge_pos
  1685. || src_y + (motion_y&3) + h > v_edge_pos){
  1686. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - src_offset, s->linesize, 17, 17+field_based,
  1687. src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
  1688. ptr= s->edge_emu_buffer + src_offset;
  1689. emu=1;
  1690. }
  1691. }
  1692. if(!field_based)
  1693. qpix_op[0][dxy](dest_y, ptr, linesize);
  1694. else{
  1695. //damn interlaced mode
  1696. //FIXME boundary mirroring is not exactly correct here
  1697. qpix_op[1][dxy](dest_y , ptr , linesize);
  1698. qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
  1699. }
  1700. if(s->flags&CODEC_FLAG_GRAY) return;
  1701. if(field_based){
  1702. mx= motion_x/2;
  1703. my= motion_y>>1;
  1704. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
  1705. static const int rtab[8]= {0,0,1,1,0,0,0,1};
  1706. mx= (motion_x>>1) + rtab[motion_x&7];
  1707. my= (motion_y>>1) + rtab[motion_y&7];
  1708. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
  1709. mx= (motion_x>>1)|(motion_x&1);
  1710. my= (motion_y>>1)|(motion_y&1);
  1711. }else{
  1712. mx= motion_x/2;
  1713. my= motion_y/2;
  1714. }
  1715. mx= (mx>>1)|(mx&1);
  1716. my= (my>>1)|(my&1);
  1717. dxy= (mx&1) | ((my&1)<<1);
  1718. mx>>=1;
  1719. my>>=1;
  1720. src_x = s->mb_x * 8 + mx;
  1721. src_y = s->mb_y * (8 >> field_based) + my;
  1722. src_x = clip(src_x, -8, s->width >> 1);
  1723. if (src_x == (s->width >> 1))
  1724. dxy &= ~1;
  1725. src_y = clip(src_y, -8, height >> 1);
  1726. if (src_y == (height >> 1))
  1727. dxy &= ~2;
  1728. offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
  1729. ptr = ref_picture[1] + offset;
  1730. if(emu){
  1731. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
  1732. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1733. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1734. }
  1735. pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1736. ptr = ref_picture[2] + offset;
  1737. if(emu){
  1738. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
  1739. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1740. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1741. }
  1742. pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1743. }
  1744. inline int ff_h263_round_chroma(int x){
  1745. if (x >= 0)
  1746. return (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
  1747. else {
  1748. x = -x;
  1749. return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
  1750. }
  1751. }
  1752. /**
  1753. * motion compesation of a single macroblock
  1754. * @param s context
  1755. * @param dest_y luma destination pointer
  1756. * @param dest_cb chroma cb/u destination pointer
  1757. * @param dest_cr chroma cr/v destination pointer
  1758. * @param dir direction (0->forward, 1->backward)
  1759. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  1760. * @param pic_op halfpel motion compensation function (average or put normally)
  1761. * @param pic_op qpel motion compensation function (average or put normally)
  1762. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  1763. */
  1764. static inline void MPV_motion(MpegEncContext *s,
  1765. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1766. int dir, uint8_t **ref_picture,
  1767. op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
  1768. {
  1769. int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
  1770. int mb_x, mb_y, i;
  1771. uint8_t *ptr, *dest;
  1772. int emu=0;
  1773. mb_x = s->mb_x;
  1774. mb_y = s->mb_y;
  1775. switch(s->mv_type) {
  1776. case MV_TYPE_16X16:
  1777. #ifdef CONFIG_RISKY
  1778. if(s->mcsel){
  1779. if(s->real_sprite_warping_points==1){
  1780. gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
  1781. ref_picture, 0);
  1782. }else{
  1783. gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
  1784. ref_picture, 0);
  1785. }
  1786. }else if(s->quarter_sample){
  1787. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  1788. ref_picture, 0,
  1789. 0, pix_op, qpix_op,
  1790. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1791. }else if(s->mspel){
  1792. ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
  1793. ref_picture, pix_op,
  1794. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1795. }else
  1796. #endif
  1797. {
  1798. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1799. ref_picture, 0,
  1800. 0, pix_op,
  1801. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1802. }
  1803. break;
  1804. case MV_TYPE_8X8:
  1805. mx = 0;
  1806. my = 0;
  1807. if(s->quarter_sample){
  1808. for(i=0;i<4;i++) {
  1809. motion_x = s->mv[dir][i][0];
  1810. motion_y = s->mv[dir][i][1];
  1811. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  1812. src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
  1813. src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
  1814. /* WARNING: do no forget half pels */
  1815. src_x = clip(src_x, -16, s->width);
  1816. if (src_x == s->width)
  1817. dxy &= ~3;
  1818. src_y = clip(src_y, -16, s->height);
  1819. if (src_y == s->height)
  1820. dxy &= ~12;
  1821. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  1822. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1823. if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 8 > s->h_edge_pos
  1824. || src_y + (motion_y&3) + 8 > s->v_edge_pos){
  1825. 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);
  1826. ptr= s->edge_emu_buffer;
  1827. }
  1828. }
  1829. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  1830. qpix_op[1][dxy](dest, ptr, s->linesize);
  1831. mx += s->mv[dir][i][0]/2;
  1832. my += s->mv[dir][i][1]/2;
  1833. }
  1834. }else{
  1835. for(i=0;i<4;i++) {
  1836. motion_x = s->mv[dir][i][0];
  1837. motion_y = s->mv[dir][i][1];
  1838. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1839. src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
  1840. src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8;
  1841. /* WARNING: do no forget half pels */
  1842. src_x = clip(src_x, -16, s->width);
  1843. if (src_x == s->width)
  1844. dxy &= ~1;
  1845. src_y = clip(src_y, -16, s->height);
  1846. if (src_y == s->height)
  1847. dxy &= ~2;
  1848. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  1849. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1850. if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->h_edge_pos
  1851. || src_y + (motion_y&1) + 8 > s->v_edge_pos){
  1852. 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);
  1853. ptr= s->edge_emu_buffer;
  1854. }
  1855. }
  1856. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  1857. pix_op[1][dxy](dest, ptr, s->linesize, 8);
  1858. mx += s->mv[dir][i][0];
  1859. my += s->mv[dir][i][1];
  1860. }
  1861. }
  1862. if(s->flags&CODEC_FLAG_GRAY) break;
  1863. /* In case of 8X8, we construct a single chroma motion vector
  1864. with a special rounding */
  1865. mx= ff_h263_round_chroma(mx);
  1866. my= ff_h263_round_chroma(my);
  1867. dxy = ((my & 1) << 1) | (mx & 1);
  1868. mx >>= 1;
  1869. my >>= 1;
  1870. src_x = mb_x * 8 + mx;
  1871. src_y = mb_y * 8 + my;
  1872. src_x = clip(src_x, -8, s->width/2);
  1873. if (src_x == s->width/2)
  1874. dxy &= ~1;
  1875. src_y = clip(src_y, -8, s->height/2);
  1876. if (src_y == s->height/2)
  1877. dxy &= ~2;
  1878. offset = (src_y * (s->uvlinesize)) + src_x;
  1879. ptr = ref_picture[1] + offset;
  1880. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1881. if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->h_edge_pos>>1
  1882. || src_y + (dxy>>1) + 8 > s->v_edge_pos>>1){
  1883. 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);
  1884. ptr= s->edge_emu_buffer;
  1885. emu=1;
  1886. }
  1887. }
  1888. pix_op[1][dxy](dest_cb, ptr, s->uvlinesize, 8);
  1889. ptr = ref_picture[2] + offset;
  1890. if(emu){
  1891. 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);
  1892. ptr= s->edge_emu_buffer;
  1893. }
  1894. pix_op[1][dxy](dest_cr, ptr, s->uvlinesize, 8);
  1895. break;
  1896. case MV_TYPE_FIELD:
  1897. if (s->picture_structure == PICT_FRAME) {
  1898. if(s->quarter_sample){
  1899. /* top field */
  1900. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  1901. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  1902. 1, pix_op, qpix_op,
  1903. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  1904. /* bottom field */
  1905. qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  1906. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  1907. 1, pix_op, qpix_op,
  1908. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  1909. }else{
  1910. /* top field */
  1911. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1912. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  1913. 1, pix_op,
  1914. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  1915. /* bottom field */
  1916. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  1917. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  1918. 1, pix_op,
  1919. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  1920. }
  1921. } else {
  1922. int offset;
  1923. if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
  1924. offset= s->field_select[dir][0] ? s->linesize : 0;
  1925. }else{
  1926. ref_picture= s->current_picture.data;
  1927. offset= s->field_select[dir][0] ? s->linesize : -s->linesize;
  1928. }
  1929. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1930. ref_picture, offset,
  1931. 0, pix_op,
  1932. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  1933. }
  1934. break;
  1935. case MV_TYPE_16X8:{
  1936. int offset;
  1937. uint8_t ** ref2picture;
  1938. if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
  1939. ref2picture= ref_picture;
  1940. offset= s->field_select[dir][0] ? s->linesize : 0;
  1941. }else{
  1942. ref2picture= s->current_picture.data;
  1943. offset= s->field_select[dir][0] ? s->linesize : -s->linesize;
  1944. }
  1945. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1946. ref2picture, offset,
  1947. 0, pix_op,
  1948. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  1949. if(s->picture_structure == s->field_select[dir][1] + 1 || s->pict_type == B_TYPE || s->first_field){
  1950. ref2picture= ref_picture;
  1951. offset= s->field_select[dir][1] ? s->linesize : 0;
  1952. }else{
  1953. ref2picture= s->current_picture.data;
  1954. offset= s->field_select[dir][1] ? s->linesize : -s->linesize;
  1955. }
  1956. // I know it is ugly but this is the only way to fool emu_edge without rewrite mpeg_motion
  1957. mpeg_motion(s, dest_y+16*s->linesize, dest_cb+8*s->uvlinesize, dest_cr+8*s->uvlinesize,
  1958. 0,
  1959. ref2picture, offset,
  1960. 0, pix_op,
  1961. s->mv[dir][1][0], s->mv[dir][1][1]+16, 8);
  1962. }
  1963. break;
  1964. case MV_TYPE_DMV:
  1965. {
  1966. op_pixels_func (*dmv_pix_op)[4];
  1967. int offset;
  1968. dmv_pix_op = s->dsp.put_pixels_tab;
  1969. if(s->picture_structure == PICT_FRAME){
  1970. //put top field from top field
  1971. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1972. ref_picture, 0,
  1973. 1, dmv_pix_op,
  1974. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  1975. //put bottom field from bottom field
  1976. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  1977. ref_picture, s->linesize,
  1978. 1, dmv_pix_op,
  1979. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  1980. dmv_pix_op = s->dsp.avg_pixels_tab;
  1981. //avg top field from bottom field
  1982. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1983. ref_picture, s->linesize,
  1984. 1, dmv_pix_op,
  1985. s->mv[dir][2][0], s->mv[dir][2][1], 8);
  1986. //avg bottom field from top field
  1987. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  1988. ref_picture, 0,
  1989. 1, dmv_pix_op,
  1990. s->mv[dir][3][0], s->mv[dir][3][1], 8);
  1991. }else{
  1992. offset=(s->picture_structure == PICT_BOTTOM_FIELD)?
  1993. s->linesize : 0;
  1994. //put field from the same parity
  1995. //same parity is never in the same frame
  1996. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  1997. ref_picture,offset,
  1998. 0,dmv_pix_op,
  1999. s->mv[dir][0][0],s->mv[dir][0][1],16);
  2000. // after put we make avg of the same block
  2001. dmv_pix_op=s->dsp.avg_pixels_tab;
  2002. //opposite parity is always in the same frame if this is second field
  2003. if(!s->first_field){
  2004. ref_picture = s->current_picture.data;
  2005. //top field is one linesize from frame beginig
  2006. offset=(s->picture_structure == PICT_BOTTOM_FIELD)?
  2007. -s->linesize : s->linesize;
  2008. }else
  2009. offset=(s->picture_structure == PICT_BOTTOM_FIELD)?
  2010. 0 : s->linesize;
  2011. //avg field from the opposite parity
  2012. mpeg_motion(s, dest_y, dest_cb, dest_cr,0,
  2013. ref_picture, offset,
  2014. 0,dmv_pix_op,
  2015. s->mv[dir][2][0],s->mv[dir][2][1],16);
  2016. }
  2017. }
  2018. break;
  2019. }
  2020. }
  2021. /* put block[] to dest[] */
  2022. static inline void put_dct(MpegEncContext *s,
  2023. DCTELEM *block, int i, uint8_t *dest, int line_size)
  2024. {
  2025. s->dct_unquantize(s, block, i, s->qscale);
  2026. s->dsp.idct_put (dest, line_size, block);
  2027. }
  2028. /* add block[] to dest[] */
  2029. static inline void add_dct(MpegEncContext *s,
  2030. DCTELEM *block, int i, uint8_t *dest, int line_size)
  2031. {
  2032. if (s->block_last_index[i] >= 0) {
  2033. s->dsp.idct_add (dest, line_size, block);
  2034. }
  2035. }
  2036. static inline void add_dequant_dct(MpegEncContext *s,
  2037. DCTELEM *block, int i, uint8_t *dest, int line_size)
  2038. {
  2039. if (s->block_last_index[i] >= 0) {
  2040. s->dct_unquantize(s, block, i, s->qscale);
  2041. s->dsp.idct_add (dest, line_size, block);
  2042. }
  2043. }
  2044. /**
  2045. * cleans dc, ac, coded_block for the current non intra MB
  2046. */
  2047. void ff_clean_intra_table_entries(MpegEncContext *s)
  2048. {
  2049. int wrap = s->block_wrap[0];
  2050. int xy = s->block_index[0];
  2051. s->dc_val[0][xy ] =
  2052. s->dc_val[0][xy + 1 ] =
  2053. s->dc_val[0][xy + wrap] =
  2054. s->dc_val[0][xy + 1 + wrap] = 1024;
  2055. /* ac pred */
  2056. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  2057. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  2058. if (s->msmpeg4_version>=3) {
  2059. s->coded_block[xy ] =
  2060. s->coded_block[xy + 1 ] =
  2061. s->coded_block[xy + wrap] =
  2062. s->coded_block[xy + 1 + wrap] = 0;
  2063. }
  2064. /* chroma */
  2065. wrap = s->block_wrap[4];
  2066. xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
  2067. s->dc_val[1][xy] =
  2068. s->dc_val[2][xy] = 1024;
  2069. /* ac pred */
  2070. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  2071. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  2072. s->mbintra_table[s->mb_x + s->mb_y*s->mb_stride]= 0;
  2073. }
  2074. /* generic function called after a macroblock has been parsed by the
  2075. decoder or after it has been encoded by the encoder.
  2076. Important variables used:
  2077. s->mb_intra : true if intra macroblock
  2078. s->mv_dir : motion vector direction
  2079. s->mv_type : motion vector type
  2080. s->mv : motion vector
  2081. s->interlaced_dct : true if interlaced dct used (mpeg2)
  2082. */
  2083. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  2084. {
  2085. int mb_x, mb_y;
  2086. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  2087. #ifdef HAVE_XVMC
  2088. if(s->avctx->xvmc_acceleration){
  2089. XVMC_decode_mb(s,block);
  2090. return;
  2091. }
  2092. #endif
  2093. mb_x = s->mb_x;
  2094. mb_y = s->mb_y;
  2095. s->current_picture.qscale_table[mb_xy]= s->qscale;
  2096. /* update DC predictors for P macroblocks */
  2097. if (!s->mb_intra) {
  2098. if (s->h263_pred || s->h263_aic) {
  2099. if(s->mbintra_table[mb_xy])
  2100. ff_clean_intra_table_entries(s);
  2101. } else {
  2102. s->last_dc[0] =
  2103. s->last_dc[1] =
  2104. s->last_dc[2] = 128 << s->intra_dc_precision;
  2105. }
  2106. }
  2107. else if (s->h263_pred || s->h263_aic)
  2108. s->mbintra_table[mb_xy]=1;
  2109. if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
  2110. uint8_t *dest_y, *dest_cb, *dest_cr;
  2111. int dct_linesize, dct_offset;
  2112. op_pixels_func (*op_pix)[4];
  2113. qpel_mc_func (*op_qpix)[16];
  2114. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
  2115. const int uvlinesize= s->current_picture.linesize[1];
  2116. /* avoid copy if macroblock skipped in last frame too */
  2117. /* skip only during decoding as we might trash the buffers during encoding a bit */
  2118. if(!s->encoding){
  2119. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  2120. const int age= s->current_picture.age;
  2121. assert(age);
  2122. if (s->mb_skiped) {
  2123. s->mb_skiped= 0;
  2124. assert(s->pict_type!=I_TYPE);
  2125. (*mbskip_ptr) ++; /* indicate that this time we skiped it */
  2126. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  2127. /* if previous was skipped too, then nothing to do ! */
  2128. if (*mbskip_ptr >= age && s->current_picture.reference){
  2129. return;
  2130. }
  2131. } else if(!s->current_picture.reference){
  2132. (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
  2133. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  2134. } else{
  2135. *mbskip_ptr = 0; /* not skipped */
  2136. }
  2137. }
  2138. if (s->interlaced_dct) {
  2139. dct_linesize = linesize * 2;
  2140. dct_offset = linesize;
  2141. } else {
  2142. dct_linesize = linesize;
  2143. dct_offset = linesize * 8;
  2144. }
  2145. dest_y= s->dest[0];
  2146. dest_cb= s->dest[1];
  2147. dest_cr= s->dest[2];
  2148. if (!s->mb_intra) {
  2149. /* motion handling */
  2150. /* decoding or more than one mb_type (MC was allready done otherwise) */
  2151. if(!s->encoding){
  2152. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  2153. op_pix = s->dsp.put_pixels_tab;
  2154. op_qpix= s->dsp.put_qpel_pixels_tab;
  2155. }else{
  2156. op_pix = s->dsp.put_no_rnd_pixels_tab;
  2157. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  2158. }
  2159. if (s->mv_dir & MV_DIR_FORWARD) {
  2160. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  2161. op_pix = s->dsp.avg_pixels_tab;
  2162. op_qpix= s->dsp.avg_qpel_pixels_tab;
  2163. }
  2164. if (s->mv_dir & MV_DIR_BACKWARD) {
  2165. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  2166. }
  2167. }
  2168. /* skip dequant / idct if we are really late ;) */
  2169. if(s->hurry_up>1) return;
  2170. /* add dct residue */
  2171. if(s->encoding || !( s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO
  2172. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  2173. add_dequant_dct(s, block[0], 0, dest_y, dct_linesize);
  2174. add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  2175. add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  2176. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  2177. if(!(s->flags&CODEC_FLAG_GRAY)){
  2178. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize);
  2179. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize);
  2180. }
  2181. } else if(s->codec_id != CODEC_ID_WMV2){
  2182. add_dct(s, block[0], 0, dest_y, dct_linesize);
  2183. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  2184. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  2185. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  2186. if(!(s->flags&CODEC_FLAG_GRAY)){
  2187. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  2188. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  2189. }
  2190. }
  2191. #ifdef CONFIG_RISKY
  2192. else{
  2193. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  2194. }
  2195. #endif
  2196. } else {
  2197. /* dct only in intra block */
  2198. if(s->encoding || !(s->mpeg2 || s->codec_id==CODEC_ID_MPEG1VIDEO)){
  2199. put_dct(s, block[0], 0, dest_y, dct_linesize);
  2200. put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  2201. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  2202. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  2203. if(!(s->flags&CODEC_FLAG_GRAY)){
  2204. put_dct(s, block[4], 4, dest_cb, uvlinesize);
  2205. put_dct(s, block[5], 5, dest_cr, uvlinesize);
  2206. }
  2207. }else{
  2208. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  2209. s->dsp.idct_put(dest_y + 8, dct_linesize, block[1]);
  2210. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  2211. s->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
  2212. if(!(s->flags&CODEC_FLAG_GRAY)){
  2213. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  2214. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  2215. }
  2216. }
  2217. }
  2218. }
  2219. }
  2220. #ifdef CONFIG_ENCODERS
  2221. static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
  2222. {
  2223. static const char tab[64]=
  2224. {3,2,2,1,1,1,1,1,
  2225. 1,1,1,1,1,1,1,1,
  2226. 1,1,1,1,1,1,1,1,
  2227. 0,0,0,0,0,0,0,0,
  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. int score=0;
  2233. int run=0;
  2234. int i;
  2235. DCTELEM *block= s->block[n];
  2236. const int last_index= s->block_last_index[n];
  2237. int skip_dc;
  2238. if(threshold<0){
  2239. skip_dc=0;
  2240. threshold= -threshold;
  2241. }else
  2242. skip_dc=1;
  2243. /* are all which we could set to zero are allready zero? */
  2244. if(last_index<=skip_dc - 1) return;
  2245. for(i=0; i<=last_index; i++){
  2246. const int j = s->intra_scantable.permutated[i];
  2247. const int level = ABS(block[j]);
  2248. if(level==1){
  2249. if(skip_dc && i==0) continue;
  2250. score+= tab[run];
  2251. run=0;
  2252. }else if(level>1){
  2253. return;
  2254. }else{
  2255. run++;
  2256. }
  2257. }
  2258. if(score >= threshold) return;
  2259. for(i=skip_dc; i<=last_index; i++){
  2260. const int j = s->intra_scantable.permutated[i];
  2261. block[j]=0;
  2262. }
  2263. if(block[0]) s->block_last_index[n]= 0;
  2264. else s->block_last_index[n]= -1;
  2265. }
  2266. static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
  2267. {
  2268. int i;
  2269. const int maxlevel= s->max_qcoeff;
  2270. const int minlevel= s->min_qcoeff;
  2271. if(s->mb_intra){
  2272. i=1; //skip clipping of intra dc
  2273. }else
  2274. i=0;
  2275. for(;i<=last_index; i++){
  2276. const int j= s->intra_scantable.permutated[i];
  2277. int level = block[j];
  2278. if (level>maxlevel) level=maxlevel;
  2279. else if(level<minlevel) level=minlevel;
  2280. block[j]= level;
  2281. }
  2282. }
  2283. #if 0
  2284. static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
  2285. int score=0;
  2286. int x,y;
  2287. for(y=0; y<7; y++){
  2288. for(x=0; x<16; x+=4){
  2289. score+= ABS(s[x ] - s[x +stride]) + ABS(s[x+1] - s[x+1+stride])
  2290. +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]);
  2291. }
  2292. s+= stride;
  2293. }
  2294. return score;
  2295. }
  2296. static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
  2297. int score=0;
  2298. int x,y;
  2299. for(y=0; y<7; y++){
  2300. for(x=0; x<16; x++){
  2301. score+= ABS(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
  2302. }
  2303. s1+= stride;
  2304. s2+= stride;
  2305. }
  2306. return score;
  2307. }
  2308. #else
  2309. #define SQ(a) ((a)*(a))
  2310. static int pix_vcmp16x8(uint8_t *s, int stride){ //FIXME move to dsputil & optimize
  2311. int score=0;
  2312. int x,y;
  2313. for(y=0; y<7; y++){
  2314. for(x=0; x<16; x+=4){
  2315. score+= SQ(s[x ] - s[x +stride]) + SQ(s[x+1] - s[x+1+stride])
  2316. +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]);
  2317. }
  2318. s+= stride;
  2319. }
  2320. return score;
  2321. }
  2322. static int pix_diff_vcmp16x8(uint8_t *s1, uint8_t*s2, int stride){ //FIXME move to dsputil & optimize
  2323. int score=0;
  2324. int x,y;
  2325. for(y=0; y<7; y++){
  2326. for(x=0; x<16; x++){
  2327. score+= SQ(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]);
  2328. }
  2329. s1+= stride;
  2330. s2+= stride;
  2331. }
  2332. return score;
  2333. }
  2334. #endif
  2335. #endif //CONFIG_ENCODERS
  2336. /**
  2337. *
  2338. * @param h is the normal height, this will be reduced automatically if needed for the last row
  2339. */
  2340. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  2341. if (s->avctx->draw_horiz_band) {
  2342. AVFrame *src;
  2343. int offset[4];
  2344. if(s->picture_structure != PICT_FRAME){
  2345. h <<= 1;
  2346. y <<= 1;
  2347. if(s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  2348. }
  2349. h= FFMIN(h, s->height - y);
  2350. if(s->pict_type==B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  2351. src= (AVFrame*)s->current_picture_ptr;
  2352. else if(s->last_picture_ptr)
  2353. src= (AVFrame*)s->last_picture_ptr;
  2354. else
  2355. return;
  2356. if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  2357. offset[0]=
  2358. offset[1]=
  2359. offset[2]=
  2360. offset[3]= 0;
  2361. }else{
  2362. offset[0]= y * s->linesize;;
  2363. offset[1]=
  2364. offset[2]= (y>>1) * s->uvlinesize;;
  2365. offset[3]= 0;
  2366. }
  2367. emms_c();
  2368. s->avctx->draw_horiz_band(s->avctx, src, offset,
  2369. y, s->picture_structure, h);
  2370. }
  2371. }
  2372. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2373. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
  2374. const int uvlinesize= s->current_picture.linesize[1];
  2375. s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2376. s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1) + s->mb_x*2;
  2377. s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1 + s->mb_x*2;
  2378. s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2) + s->mb_x*2;
  2379. s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
  2380. 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;
  2381. if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME){
  2382. s->dest[0] = s->current_picture.data[0] + s->mb_x * 16 - 16;
  2383. s->dest[1] = s->current_picture.data[1] + s->mb_x * 8 - 8;
  2384. s->dest[2] = s->current_picture.data[2] + s->mb_x * 8 - 8;
  2385. }else{
  2386. s->dest[0] = s->current_picture.data[0] + (s->mb_y * 16* linesize ) + s->mb_x * 16 - 16;
  2387. s->dest[1] = s->current_picture.data[1] + (s->mb_y * 8 * uvlinesize) + s->mb_x * 8 - 8;
  2388. s->dest[2] = s->current_picture.data[2] + (s->mb_y * 8 * uvlinesize) + s->mb_x * 8 - 8;
  2389. }
  2390. }
  2391. #ifdef CONFIG_ENCODERS
  2392. static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  2393. {
  2394. const int mb_x= s->mb_x;
  2395. const int mb_y= s->mb_y;
  2396. int i;
  2397. int skip_dct[6];
  2398. int dct_offset = s->linesize*8; //default for progressive frames
  2399. for(i=0; i<6; i++) skip_dct[i]=0;
  2400. if(s->adaptive_quant){
  2401. s->dquant= s->current_picture.qscale_table[mb_x + mb_y*s->mb_stride] - s->qscale;
  2402. if(s->out_format==FMT_H263){
  2403. if (s->dquant> 2) s->dquant= 2;
  2404. else if(s->dquant<-2) s->dquant=-2;
  2405. }
  2406. if(s->codec_id==CODEC_ID_MPEG4){
  2407. if(!s->mb_intra){
  2408. if(s->mv_dir&MV_DIRECT)
  2409. s->dquant=0;
  2410. assert(s->dquant==0 || s->mv_type!=MV_TYPE_8X8);
  2411. }
  2412. }
  2413. s->qscale+= s->dquant;
  2414. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  2415. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  2416. }
  2417. if (s->mb_intra) {
  2418. uint8_t *ptr;
  2419. int wrap_y;
  2420. int emu=0;
  2421. wrap_y = s->linesize;
  2422. ptr = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  2423. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  2424. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
  2425. ptr= s->edge_emu_buffer;
  2426. emu=1;
  2427. }
  2428. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  2429. int progressive_score, interlaced_score;
  2430. progressive_score= pix_vcmp16x8(ptr, wrap_y ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y );
  2431. interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y , wrap_y*2);
  2432. if(progressive_score > interlaced_score + 100){
  2433. s->interlaced_dct=1;
  2434. dct_offset= wrap_y;
  2435. wrap_y<<=1;
  2436. }else
  2437. s->interlaced_dct=0;
  2438. }
  2439. s->dsp.get_pixels(s->block[0], ptr , wrap_y);
  2440. s->dsp.get_pixels(s->block[1], ptr + 8, wrap_y);
  2441. s->dsp.get_pixels(s->block[2], ptr + dct_offset , wrap_y);
  2442. s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
  2443. if(s->flags&CODEC_FLAG_GRAY){
  2444. skip_dct[4]= 1;
  2445. skip_dct[5]= 1;
  2446. }else{
  2447. int wrap_c = s->uvlinesize;
  2448. ptr = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2449. if(emu){
  2450. 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);
  2451. ptr= s->edge_emu_buffer;
  2452. }
  2453. s->dsp.get_pixels(s->block[4], ptr, wrap_c);
  2454. ptr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2455. if(emu){
  2456. 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);
  2457. ptr= s->edge_emu_buffer;
  2458. }
  2459. s->dsp.get_pixels(s->block[5], ptr, wrap_c);
  2460. }
  2461. }else{
  2462. op_pixels_func (*op_pix)[4];
  2463. qpel_mc_func (*op_qpix)[16];
  2464. uint8_t *dest_y, *dest_cb, *dest_cr;
  2465. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  2466. int wrap_y, wrap_c;
  2467. int emu=0;
  2468. dest_y = s->dest[0];
  2469. dest_cb = s->dest[1];
  2470. dest_cr = s->dest[2];
  2471. wrap_y = s->linesize;
  2472. wrap_c = s->uvlinesize;
  2473. ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  2474. ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2475. ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2476. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  2477. op_pix = s->dsp.put_pixels_tab;
  2478. op_qpix= s->dsp.put_qpel_pixels_tab;
  2479. }else{
  2480. op_pix = s->dsp.put_no_rnd_pixels_tab;
  2481. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  2482. }
  2483. if (s->mv_dir & MV_DIR_FORWARD) {
  2484. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  2485. op_pix = s->dsp.avg_pixels_tab;
  2486. op_qpix= s->dsp.avg_qpel_pixels_tab;
  2487. }
  2488. if (s->mv_dir & MV_DIR_BACKWARD) {
  2489. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  2490. }
  2491. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  2492. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
  2493. ptr_y= s->edge_emu_buffer;
  2494. emu=1;
  2495. }
  2496. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  2497. int progressive_score, interlaced_score;
  2498. progressive_score= pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y )
  2499. + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y );
  2500. interlaced_score = pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y*2)
  2501. + pix_diff_vcmp16x8(ptr_y + wrap_y , dest_y + wrap_y , wrap_y*2);
  2502. if(progressive_score > interlaced_score + 600){
  2503. s->interlaced_dct=1;
  2504. dct_offset= wrap_y;
  2505. wrap_y<<=1;
  2506. }else
  2507. s->interlaced_dct=0;
  2508. }
  2509. s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
  2510. s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  2511. s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
  2512. s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
  2513. if(s->flags&CODEC_FLAG_GRAY){
  2514. skip_dct[4]= 1;
  2515. skip_dct[5]= 1;
  2516. }else{
  2517. if(emu){
  2518. 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);
  2519. ptr_cb= s->edge_emu_buffer;
  2520. }
  2521. s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  2522. if(emu){
  2523. 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);
  2524. ptr_cr= s->edge_emu_buffer;
  2525. }
  2526. s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  2527. }
  2528. /* pre quantization */
  2529. if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){
  2530. //FIXME optimize
  2531. if(s->dsp.pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
  2532. if(s->dsp.pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
  2533. if(s->dsp.pix_abs8x8(ptr_y +dct_offset , dest_y +dct_offset , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
  2534. if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
  2535. if(s->dsp.pix_abs8x8(ptr_cb , dest_cb , wrap_c) < 20*s->qscale) skip_dct[4]= 1;
  2536. if(s->dsp.pix_abs8x8(ptr_cr , dest_cr , wrap_c) < 20*s->qscale) skip_dct[5]= 1;
  2537. #if 0
  2538. {
  2539. static int stat[7];
  2540. int num=0;
  2541. for(i=0; i<6; i++)
  2542. if(skip_dct[i]) num++;
  2543. stat[num]++;
  2544. if(s->mb_x==0 && s->mb_y==0){
  2545. for(i=0; i<7; i++){
  2546. printf("%6d %1d\n", stat[i], i);
  2547. }
  2548. }
  2549. }
  2550. #endif
  2551. }
  2552. }
  2553. #if 0
  2554. {
  2555. float adap_parm;
  2556. adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_stride*mb_y+mb_x] + 1.0) /
  2557. ((s->mb_var[s->mb_stride*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0);
  2558. printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d",
  2559. (s->mb_type[s->mb_stride*mb_y+mb_x] > 0) ? 'I' : 'P',
  2560. s->qscale, adap_parm, s->qscale*adap_parm,
  2561. s->mb_var[s->mb_stride*mb_y+mb_x], s->avg_mb_var);
  2562. }
  2563. #endif
  2564. /* DCT & quantize */
  2565. if(s->out_format==FMT_MJPEG){
  2566. for(i=0;i<6;i++) {
  2567. int overflow;
  2568. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
  2569. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  2570. }
  2571. }else{
  2572. for(i=0;i<6;i++) {
  2573. if(!skip_dct[i]){
  2574. int overflow;
  2575. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  2576. // FIXME we could decide to change to quantizer instead of clipping
  2577. // JS: I don't think that would be a good idea it could lower quality instead
  2578. // of improve it. Just INTRADC clipping deserves changes in quantizer
  2579. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  2580. }else
  2581. s->block_last_index[i]= -1;
  2582. }
  2583. if(s->luma_elim_threshold && !s->mb_intra)
  2584. for(i=0; i<4; i++)
  2585. dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
  2586. if(s->chroma_elim_threshold && !s->mb_intra)
  2587. for(i=4; i<6; i++)
  2588. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
  2589. }
  2590. if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
  2591. s->block_last_index[4]=
  2592. s->block_last_index[5]= 0;
  2593. s->block[4][0]=
  2594. s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
  2595. }
  2596. /* huffman encode */
  2597. switch(s->codec_id){ //FIXME funct ptr could be slightly faster
  2598. case CODEC_ID_MPEG1VIDEO:
  2599. mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
  2600. #ifdef CONFIG_RISKY
  2601. case CODEC_ID_MPEG4:
  2602. mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  2603. case CODEC_ID_MSMPEG4V2:
  2604. case CODEC_ID_MSMPEG4V3:
  2605. case CODEC_ID_WMV1:
  2606. msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  2607. case CODEC_ID_WMV2:
  2608. ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
  2609. case CODEC_ID_H263:
  2610. case CODEC_ID_H263P:
  2611. case CODEC_ID_FLV1:
  2612. case CODEC_ID_RV10:
  2613. h263_encode_mb(s, s->block, motion_x, motion_y); break;
  2614. #endif
  2615. case CODEC_ID_MJPEG:
  2616. mjpeg_encode_mb(s, s->block); break;
  2617. default:
  2618. assert(0);
  2619. }
  2620. }
  2621. #endif //CONFIG_ENCODERS
  2622. /**
  2623. * combines the (truncated) bitstream to a complete frame
  2624. * @returns -1 if no complete frame could be created
  2625. */
  2626. int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size){
  2627. ParseContext *pc= &s->parse_context;
  2628. #if 0
  2629. if(pc->overread){
  2630. printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
  2631. printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
  2632. }
  2633. #endif
  2634. /* copy overreaded byes from last frame into buffer */
  2635. for(; pc->overread>0; pc->overread--){
  2636. pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
  2637. }
  2638. pc->last_index= pc->index;
  2639. /* copy into buffer end return */
  2640. if(next == END_NOT_FOUND){
  2641. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  2642. memcpy(&pc->buffer[pc->index], *buf, *buf_size);
  2643. pc->index += *buf_size;
  2644. return -1;
  2645. }
  2646. *buf_size=
  2647. pc->overread_index= pc->index + next;
  2648. /* append to buffer */
  2649. if(pc->index){
  2650. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  2651. memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
  2652. pc->index = 0;
  2653. *buf= pc->buffer;
  2654. }
  2655. /* store overread bytes */
  2656. for(;next < 0; next++){
  2657. pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
  2658. pc->overread++;
  2659. }
  2660. #if 0
  2661. if(pc->overread){
  2662. printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
  2663. printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
  2664. }
  2665. #endif
  2666. return 0;
  2667. }
  2668. void ff_mpeg_flush(AVCodecContext *avctx){
  2669. int i;
  2670. MpegEncContext *s = avctx->priv_data;
  2671. for(i=0; i<MAX_PICTURE_COUNT; i++){
  2672. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  2673. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  2674. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  2675. }
  2676. s->last_picture_ptr = s->next_picture_ptr = NULL;
  2677. s->parse_context.state= -1;
  2678. s->parse_context.frame_start_found= 0;
  2679. s->parse_context.overread= 0;
  2680. s->parse_context.overread_index= 0;
  2681. s->parse_context.index= 0;
  2682. s->parse_context.last_index= 0;
  2683. }
  2684. #ifdef CONFIG_ENCODERS
  2685. void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
  2686. {
  2687. int bytes= length>>4;
  2688. int bits= length&15;
  2689. int i;
  2690. if(length==0) return;
  2691. for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
  2692. put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
  2693. }
  2694. static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  2695. int i;
  2696. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  2697. /* mpeg1 */
  2698. d->mb_skip_run= s->mb_skip_run;
  2699. for(i=0; i<3; i++)
  2700. d->last_dc[i]= s->last_dc[i];
  2701. /* statistics */
  2702. d->mv_bits= s->mv_bits;
  2703. d->i_tex_bits= s->i_tex_bits;
  2704. d->p_tex_bits= s->p_tex_bits;
  2705. d->i_count= s->i_count;
  2706. d->f_count= s->f_count;
  2707. d->b_count= s->b_count;
  2708. d->skip_count= s->skip_count;
  2709. d->misc_bits= s->misc_bits;
  2710. d->last_bits= 0;
  2711. d->mb_skiped= 0;
  2712. d->qscale= s->qscale;
  2713. }
  2714. static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  2715. int i;
  2716. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  2717. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  2718. /* mpeg1 */
  2719. d->mb_skip_run= s->mb_skip_run;
  2720. for(i=0; i<3; i++)
  2721. d->last_dc[i]= s->last_dc[i];
  2722. /* statistics */
  2723. d->mv_bits= s->mv_bits;
  2724. d->i_tex_bits= s->i_tex_bits;
  2725. d->p_tex_bits= s->p_tex_bits;
  2726. d->i_count= s->i_count;
  2727. d->f_count= s->f_count;
  2728. d->b_count= s->b_count;
  2729. d->skip_count= s->skip_count;
  2730. d->misc_bits= s->misc_bits;
  2731. d->mb_intra= s->mb_intra;
  2732. d->mb_skiped= s->mb_skiped;
  2733. d->mv_type= s->mv_type;
  2734. d->mv_dir= s->mv_dir;
  2735. d->pb= s->pb;
  2736. if(s->data_partitioning){
  2737. d->pb2= s->pb2;
  2738. d->tex_pb= s->tex_pb;
  2739. }
  2740. d->block= s->block;
  2741. for(i=0; i<6; i++)
  2742. d->block_last_index[i]= s->block_last_index[i];
  2743. d->interlaced_dct= s->interlaced_dct;
  2744. d->qscale= s->qscale;
  2745. }
  2746. static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
  2747. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  2748. int *dmin, int *next_block, int motion_x, int motion_y)
  2749. {
  2750. int score;
  2751. uint8_t *dest_backup[3];
  2752. copy_context_before_encode(s, backup, type);
  2753. s->block= s->blocks[*next_block];
  2754. s->pb= pb[*next_block];
  2755. if(s->data_partitioning){
  2756. s->pb2 = pb2 [*next_block];
  2757. s->tex_pb= tex_pb[*next_block];
  2758. }
  2759. if(*next_block){
  2760. memcpy(dest_backup, s->dest, sizeof(s->dest));
  2761. s->dest[0] = s->me.scratchpad;
  2762. s->dest[1] = s->me.scratchpad + 16;
  2763. s->dest[2] = s->me.scratchpad + 16 + 8;
  2764. assert(2*s->uvlinesize == s->linesize); //should be no prob for encoding
  2765. assert(s->linesize >= 64); //FIXME
  2766. }
  2767. encode_mb(s, motion_x, motion_y);
  2768. score= get_bit_count(&s->pb);
  2769. if(s->data_partitioning){
  2770. score+= get_bit_count(&s->pb2);
  2771. score+= get_bit_count(&s->tex_pb);
  2772. }
  2773. if(s->avctx->mb_decision == FF_MB_DECISION_RD){
  2774. MPV_decode_mb(s, s->block);
  2775. score *= s->qscale * s->qscale * 109;
  2776. score += sse_mb(s) << 7;
  2777. }
  2778. if(*next_block){
  2779. memcpy(s->dest, dest_backup, sizeof(s->dest));
  2780. }
  2781. if(score<*dmin){
  2782. *dmin= score;
  2783. *next_block^=1;
  2784. copy_context_after_encode(best, s, type);
  2785. }
  2786. }
  2787. static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
  2788. uint32_t *sq = squareTbl + 256;
  2789. int acc=0;
  2790. int x,y;
  2791. if(w==16 && h==16)
  2792. return s->dsp.sse[0](NULL, src1, src2, stride);
  2793. else if(w==8 && h==8)
  2794. return s->dsp.sse[1](NULL, src1, src2, stride);
  2795. for(y=0; y<h; y++){
  2796. for(x=0; x<w; x++){
  2797. acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
  2798. }
  2799. }
  2800. assert(acc>=0);
  2801. return acc;
  2802. }
  2803. static int sse_mb(MpegEncContext *s){
  2804. int w= 16;
  2805. int h= 16;
  2806. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  2807. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  2808. if(w==16 && h==16)
  2809. 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)
  2810. +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)
  2811. +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);
  2812. else
  2813. 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)
  2814. +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)
  2815. +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);
  2816. }
  2817. static void encode_picture(MpegEncContext *s, int picture_number)
  2818. {
  2819. int mb_x, mb_y, pdif = 0;
  2820. int i;
  2821. int bits;
  2822. MpegEncContext best_s, backup_s;
  2823. uint8_t bit_buf[2][3000];
  2824. uint8_t bit_buf2[2][3000];
  2825. uint8_t bit_buf_tex[2][3000];
  2826. PutBitContext pb[2], pb2[2], tex_pb[2];
  2827. for(i=0; i<2; i++){
  2828. init_put_bits(&pb [i], bit_buf [i], 3000, NULL, NULL);
  2829. init_put_bits(&pb2 [i], bit_buf2 [i], 3000, NULL, NULL);
  2830. init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
  2831. }
  2832. s->picture_number = picture_number;
  2833. /* Reset the average MB variance */
  2834. s->current_picture.mb_var_sum = 0;
  2835. s->current_picture.mc_mb_var_sum = 0;
  2836. #ifdef CONFIG_RISKY
  2837. /* we need to initialize some time vars before we can encode b-frames */
  2838. // RAL: Condition added for MPEG1VIDEO
  2839. if (s->codec_id == CODEC_ID_MPEG1VIDEO || (s->h263_pred && !s->h263_msmpeg4))
  2840. ff_set_mpeg4_time(s, s->picture_number);
  2841. #endif
  2842. s->scene_change_score=0;
  2843. s->qscale= (int)(s->frame_qscale + 0.5); //FIXME qscale / ... stuff for ME ratedistoration
  2844. if(s->pict_type==I_TYPE){
  2845. if(s->msmpeg4_version >= 3) s->no_rounding=1;
  2846. else s->no_rounding=0;
  2847. }else if(s->pict_type!=B_TYPE){
  2848. if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
  2849. s->no_rounding ^= 1;
  2850. }
  2851. /* Estimate motion for every MB */
  2852. s->mb_intra=0; //for the rate distoration & bit compare functions
  2853. if(s->pict_type != I_TYPE){
  2854. if(s->pict_type != B_TYPE){
  2855. if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
  2856. s->me.pre_pass=1;
  2857. s->me.dia_size= s->avctx->pre_dia_size;
  2858. for(mb_y=s->mb_height-1; mb_y >=0 ; mb_y--) {
  2859. for(mb_x=s->mb_width-1; mb_x >=0 ; mb_x--) {
  2860. s->mb_x = mb_x;
  2861. s->mb_y = mb_y;
  2862. ff_pre_estimate_p_frame_motion(s, mb_x, mb_y);
  2863. }
  2864. }
  2865. s->me.pre_pass=0;
  2866. }
  2867. }
  2868. s->me.dia_size= s->avctx->dia_size;
  2869. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  2870. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  2871. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  2872. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  2873. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  2874. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2875. s->mb_x = mb_x;
  2876. s->mb_y = mb_y;
  2877. s->block_index[0]+=2;
  2878. s->block_index[1]+=2;
  2879. s->block_index[2]+=2;
  2880. s->block_index[3]+=2;
  2881. /* compute motion vector & mb_type and store in context */
  2882. if(s->pict_type==B_TYPE)
  2883. ff_estimate_b_frame_motion(s, mb_x, mb_y);
  2884. else
  2885. ff_estimate_p_frame_motion(s, mb_x, mb_y);
  2886. }
  2887. }
  2888. }else /* if(s->pict_type == I_TYPE) */{
  2889. /* I-Frame */
  2890. //FIXME do we need to zero them?
  2891. memset(s->motion_val[0], 0, sizeof(int16_t)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
  2892. memset(s->p_mv_table , 0, sizeof(int16_t)*(s->mb_stride)*s->mb_height*2);
  2893. memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_stride*s->mb_height);
  2894. if(!s->fixed_qscale){
  2895. /* finding spatial complexity for I-frame rate control */
  2896. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  2897. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  2898. int xx = mb_x * 16;
  2899. int yy = mb_y * 16;
  2900. uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  2901. int varc;
  2902. int sum = s->dsp.pix_sum(pix, s->linesize);
  2903. varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
  2904. s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
  2905. s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  2906. s->current_picture.mb_var_sum += varc;
  2907. }
  2908. }
  2909. }
  2910. }
  2911. emms_c();
  2912. if(s->scene_change_score > 0 && s->pict_type == P_TYPE){
  2913. s->pict_type= I_TYPE;
  2914. memset(s->mb_type , MB_TYPE_INTRA, sizeof(uint8_t)*s->mb_stride*s->mb_height);
  2915. //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
  2916. }
  2917. if(!s->umvplus){
  2918. if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
  2919. s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
  2920. ff_fix_long_p_mvs(s);
  2921. }
  2922. if(s->pict_type==B_TYPE){
  2923. int a, b;
  2924. a = ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD);
  2925. b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, MB_TYPE_BIDIR);
  2926. s->f_code = FFMAX(a, b);
  2927. a = ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD);
  2928. b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, MB_TYPE_BIDIR);
  2929. s->b_code = FFMAX(a, b);
  2930. ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD);
  2931. ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD);
  2932. ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR);
  2933. ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR);
  2934. }
  2935. }
  2936. if (s->fixed_qscale)
  2937. s->frame_qscale = s->current_picture.quality;
  2938. else
  2939. s->frame_qscale = ff_rate_estimate_qscale(s);
  2940. if(s->adaptive_quant){
  2941. #ifdef CONFIG_RISKY
  2942. switch(s->codec_id){
  2943. case CODEC_ID_MPEG4:
  2944. ff_clean_mpeg4_qscales(s);
  2945. break;
  2946. case CODEC_ID_H263:
  2947. case CODEC_ID_H263P:
  2948. case CODEC_ID_FLV1:
  2949. ff_clean_h263_qscales(s);
  2950. break;
  2951. }
  2952. #endif
  2953. s->qscale= s->current_picture.qscale_table[0];
  2954. }else
  2955. s->qscale= (int)(s->frame_qscale + 0.5);
  2956. if (s->out_format == FMT_MJPEG) {
  2957. /* for mjpeg, we do include qscale in the matrix */
  2958. s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
  2959. for(i=1;i<64;i++){
  2960. int j= s->dsp.idct_permutation[i];
  2961. s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
  2962. }
  2963. convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
  2964. s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias, 8, 8);
  2965. }
  2966. //FIXME var duplication
  2967. s->current_picture.key_frame= s->pict_type == I_TYPE;
  2968. s->current_picture.pict_type= s->pict_type;
  2969. if(s->current_picture.key_frame)
  2970. s->picture_in_gop_number=0;
  2971. s->last_bits= get_bit_count(&s->pb);
  2972. switch(s->out_format) {
  2973. case FMT_MJPEG:
  2974. mjpeg_picture_header(s);
  2975. break;
  2976. #ifdef CONFIG_RISKY
  2977. case FMT_H263:
  2978. if (s->codec_id == CODEC_ID_WMV2)
  2979. ff_wmv2_encode_picture_header(s, picture_number);
  2980. else if (s->h263_msmpeg4)
  2981. msmpeg4_encode_picture_header(s, picture_number);
  2982. else if (s->h263_pred)
  2983. mpeg4_encode_picture_header(s, picture_number);
  2984. else if (s->h263_rv10)
  2985. rv10_encode_picture_header(s, picture_number);
  2986. else if (s->codec_id == CODEC_ID_FLV1)
  2987. ff_flv_encode_picture_header(s, picture_number);
  2988. else
  2989. h263_encode_picture_header(s, picture_number);
  2990. break;
  2991. #endif
  2992. case FMT_MPEG1:
  2993. mpeg1_encode_picture_header(s, picture_number);
  2994. break;
  2995. }
  2996. bits= get_bit_count(&s->pb);
  2997. s->header_bits= bits - s->last_bits;
  2998. s->last_bits= bits;
  2999. s->mv_bits=0;
  3000. s->misc_bits=0;
  3001. s->i_tex_bits=0;
  3002. s->p_tex_bits=0;
  3003. s->i_count=0;
  3004. s->f_count=0;
  3005. s->b_count=0;
  3006. s->skip_count=0;
  3007. for(i=0; i<3; i++){
  3008. /* init last dc values */
  3009. /* note: quant matrix value (8) is implied here */
  3010. s->last_dc[i] = 128;
  3011. s->current_picture_ptr->error[i] = 0;
  3012. }
  3013. s->mb_skip_run = 0;
  3014. s->last_mv[0][0][0] = 0;
  3015. s->last_mv[0][0][1] = 0;
  3016. s->last_mv[1][0][0] = 0;
  3017. s->last_mv[1][0][1] = 0;
  3018. s->last_mv_dir = 0;
  3019. #ifdef CONFIG_RISKY
  3020. switch(s->codec_id){
  3021. case CODEC_ID_H263:
  3022. case CODEC_ID_H263P:
  3023. case CODEC_ID_FLV1:
  3024. s->gob_index = ff_h263_get_gob_height(s);
  3025. break;
  3026. case CODEC_ID_MPEG4:
  3027. if(s->partitioned_frame)
  3028. ff_mpeg4_init_partitions(s);
  3029. break;
  3030. }
  3031. #endif
  3032. s->resync_mb_x=0;
  3033. s->resync_mb_y=0;
  3034. s->first_slice_line = 1;
  3035. s->ptr_lastgob = s->pb.buf;
  3036. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  3037. s->mb_x=0;
  3038. s->mb_y= mb_y;
  3039. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  3040. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  3041. ff_init_block_index(s);
  3042. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  3043. const int xy= mb_y*s->mb_stride + mb_x;
  3044. int mb_type= s->mb_type[xy];
  3045. // int d;
  3046. int dmin= INT_MAX;
  3047. s->mb_x = mb_x;
  3048. ff_update_block_index(s);
  3049. /* write gob / video packet header */
  3050. #ifdef CONFIG_RISKY
  3051. if(s->rtp_mode){
  3052. int current_packet_size, is_gob_start;
  3053. current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
  3054. is_gob_start=0;
  3055. if(s->codec_id==CODEC_ID_MPEG4){
  3056. if(current_packet_size >= s->rtp_payload_size
  3057. && s->mb_y + s->mb_x>0){
  3058. if(s->partitioned_frame){
  3059. ff_mpeg4_merge_partitions(s);
  3060. ff_mpeg4_init_partitions(s);
  3061. }
  3062. ff_mpeg4_encode_video_packet_header(s);
  3063. if(s->flags&CODEC_FLAG_PASS1){
  3064. int bits= get_bit_count(&s->pb);
  3065. s->misc_bits+= bits - s->last_bits;
  3066. s->last_bits= bits;
  3067. }
  3068. ff_mpeg4_clean_buffers(s);
  3069. is_gob_start=1;
  3070. }
  3071. }else if(s->codec_id==CODEC_ID_MPEG1VIDEO){
  3072. if( current_packet_size >= s->rtp_payload_size
  3073. && s->mb_y + s->mb_x>0 && s->mb_skip_run==0){
  3074. ff_mpeg1_encode_slice_header(s);
  3075. ff_mpeg1_clean_buffers(s);
  3076. is_gob_start=1;
  3077. }
  3078. }else{
  3079. if(current_packet_size >= s->rtp_payload_size
  3080. && s->mb_x==0 && s->mb_y>0 && s->mb_y%s->gob_index==0){
  3081. h263_encode_gob_header(s, mb_y);
  3082. is_gob_start=1;
  3083. }
  3084. }
  3085. if(is_gob_start){
  3086. s->ptr_lastgob = pbBufPtr(&s->pb);
  3087. s->first_slice_line=1;
  3088. s->resync_mb_x=mb_x;
  3089. s->resync_mb_y=mb_y;
  3090. }
  3091. }
  3092. #endif
  3093. if( (s->resync_mb_x == s->mb_x)
  3094. && s->resync_mb_y+1 == s->mb_y){
  3095. s->first_slice_line=0;
  3096. }
  3097. s->mb_skiped=0;
  3098. if(mb_type & (mb_type-1)){ // more than 1 MB type possible
  3099. int next_block=0;
  3100. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  3101. copy_context_before_encode(&backup_s, s, -1);
  3102. backup_s.pb= s->pb;
  3103. best_s.data_partitioning= s->data_partitioning;
  3104. best_s.partitioned_frame= s->partitioned_frame;
  3105. if(s->data_partitioning){
  3106. backup_s.pb2= s->pb2;
  3107. backup_s.tex_pb= s->tex_pb;
  3108. }
  3109. if(mb_type&MB_TYPE_INTER){
  3110. s->mv_dir = MV_DIR_FORWARD;
  3111. s->mv_type = MV_TYPE_16X16;
  3112. s->mb_intra= 0;
  3113. s->mv[0][0][0] = s->p_mv_table[xy][0];
  3114. s->mv[0][0][1] = s->p_mv_table[xy][1];
  3115. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb,
  3116. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  3117. }
  3118. if(mb_type&MB_TYPE_INTER4V){
  3119. s->mv_dir = MV_DIR_FORWARD;
  3120. s->mv_type = MV_TYPE_8X8;
  3121. s->mb_intra= 0;
  3122. for(i=0; i<4; i++){
  3123. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  3124. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  3125. }
  3126. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb,
  3127. &dmin, &next_block, 0, 0);
  3128. }
  3129. if(mb_type&MB_TYPE_FORWARD){
  3130. s->mv_dir = MV_DIR_FORWARD;
  3131. s->mv_type = MV_TYPE_16X16;
  3132. s->mb_intra= 0;
  3133. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  3134. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  3135. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb,
  3136. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  3137. }
  3138. if(mb_type&MB_TYPE_BACKWARD){
  3139. s->mv_dir = MV_DIR_BACKWARD;
  3140. s->mv_type = MV_TYPE_16X16;
  3141. s->mb_intra= 0;
  3142. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  3143. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  3144. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  3145. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  3146. }
  3147. if(mb_type&MB_TYPE_BIDIR){
  3148. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  3149. s->mv_type = MV_TYPE_16X16;
  3150. s->mb_intra= 0;
  3151. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  3152. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  3153. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  3154. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  3155. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb,
  3156. &dmin, &next_block, 0, 0);
  3157. }
  3158. if(mb_type&MB_TYPE_DIRECT){
  3159. int mx= s->b_direct_mv_table[xy][0];
  3160. int my= s->b_direct_mv_table[xy][1];
  3161. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  3162. s->mb_intra= 0;
  3163. #ifdef CONFIG_RISKY
  3164. ff_mpeg4_set_direct_mv(s, mx, my);
  3165. #endif
  3166. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb,
  3167. &dmin, &next_block, mx, my);
  3168. }
  3169. if(mb_type&MB_TYPE_INTRA){
  3170. s->mv_dir = 0;
  3171. s->mv_type = MV_TYPE_16X16;
  3172. s->mb_intra= 1;
  3173. s->mv[0][0][0] = 0;
  3174. s->mv[0][0][1] = 0;
  3175. encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb,
  3176. &dmin, &next_block, 0, 0);
  3177. if(s->h263_pred || s->h263_aic){
  3178. if(best_s.mb_intra)
  3179. s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
  3180. else
  3181. ff_clean_intra_table_entries(s); //old mode?
  3182. }
  3183. }
  3184. copy_context_after_encode(s, &best_s, -1);
  3185. pb_bits_count= get_bit_count(&s->pb);
  3186. flush_put_bits(&s->pb);
  3187. ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  3188. s->pb= backup_s.pb;
  3189. if(s->data_partitioning){
  3190. pb2_bits_count= get_bit_count(&s->pb2);
  3191. flush_put_bits(&s->pb2);
  3192. ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  3193. s->pb2= backup_s.pb2;
  3194. tex_pb_bits_count= get_bit_count(&s->tex_pb);
  3195. flush_put_bits(&s->tex_pb);
  3196. ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  3197. s->tex_pb= backup_s.tex_pb;
  3198. }
  3199. s->last_bits= get_bit_count(&s->pb);
  3200. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
  3201. ff_h263_update_motion_val(s);
  3202. if(next_block==0){
  3203. s->dsp.put_pixels_tab[0][0](s->dest[0], s->me.scratchpad , s->linesize ,16);
  3204. s->dsp.put_pixels_tab[1][0](s->dest[1], s->me.scratchpad + 16, s->uvlinesize, 8);
  3205. s->dsp.put_pixels_tab[1][0](s->dest[2], s->me.scratchpad + 24, s->uvlinesize, 8);
  3206. }
  3207. if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
  3208. MPV_decode_mb(s, s->block);
  3209. } else {
  3210. int motion_x, motion_y;
  3211. int intra_score;
  3212. int inter_score= s->current_picture.mb_cmp_score[mb_x + mb_y*s->mb_stride];
  3213. if(s->avctx->mb_decision==FF_MB_DECISION_SIMPLE && s->pict_type==P_TYPE){ //FIXME check if the mess is usefull at all
  3214. /* get luma score */
  3215. if((s->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
  3216. 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
  3217. }else{
  3218. uint8_t *dest_y;
  3219. int mean= s->current_picture.mb_mean[mb_x + mb_y*s->mb_stride]; //FIXME
  3220. mean*= 0x01010101;
  3221. dest_y = s->new_picture.data[0] + (mb_y * 16 * s->linesize ) + mb_x * 16;
  3222. for(i=0; i<16; i++){
  3223. *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 0]) = mean;
  3224. *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 4]) = mean;
  3225. *(uint32_t*)(&s->me.scratchpad[i*s->linesize+ 8]) = mean;
  3226. *(uint32_t*)(&s->me.scratchpad[i*s->linesize+12]) = mean;
  3227. }
  3228. s->mb_intra=1;
  3229. intra_score= s->dsp.mb_cmp[0](s, s->me.scratchpad, dest_y, s->linesize);
  3230. /* printf("intra:%7d inter:%7d var:%7d mc_var.%7d\n", intra_score>>8, inter_score>>8,
  3231. s->current_picture.mb_var[mb_x + mb_y*s->mb_stride],
  3232. s->current_picture.mc_mb_var[mb_x + mb_y*s->mb_stride]);*/
  3233. }
  3234. /* get chroma score */
  3235. if(s->avctx->mb_cmp&FF_CMP_CHROMA){
  3236. int i;
  3237. s->mb_intra=1;
  3238. for(i=1; i<3; i++){
  3239. uint8_t *dest_c;
  3240. int mean;
  3241. if(s->out_format == FMT_H263){
  3242. mean= (s->dc_val[i][mb_x + (mb_y+1)*(s->mb_width+2)] + 4)>>3; //FIXME not exact but simple ;)
  3243. }else{
  3244. mean= (s->last_dc[i] + 4)>>3;
  3245. }
  3246. dest_c = s->new_picture.data[i] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  3247. mean*= 0x01010101;
  3248. for(i=0; i<8; i++){
  3249. *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 0]) = mean;
  3250. *(uint32_t*)(&s->me.scratchpad[i*s->uvlinesize+ 4]) = mean;
  3251. }
  3252. intra_score+= s->dsp.mb_cmp[1](s, s->me.scratchpad, dest_c, s->uvlinesize);
  3253. }
  3254. }
  3255. /* bias */
  3256. switch(s->avctx->mb_cmp&0xFF){
  3257. default:
  3258. case FF_CMP_SAD:
  3259. intra_score+= 32*s->qscale;
  3260. break;
  3261. case FF_CMP_SSE:
  3262. intra_score+= 24*s->qscale*s->qscale;
  3263. break;
  3264. case FF_CMP_SATD:
  3265. intra_score+= 96*s->qscale;
  3266. break;
  3267. case FF_CMP_DCT:
  3268. intra_score+= 48*s->qscale;
  3269. break;
  3270. case FF_CMP_BIT:
  3271. intra_score+= 16;
  3272. break;
  3273. case FF_CMP_PSNR:
  3274. case FF_CMP_RD:
  3275. intra_score+= (s->qscale*s->qscale*109*8 + 64)>>7;
  3276. break;
  3277. }
  3278. if(intra_score < inter_score)
  3279. mb_type= MB_TYPE_INTRA;
  3280. }
  3281. s->mv_type=MV_TYPE_16X16;
  3282. // only one MB-Type possible
  3283. switch(mb_type){
  3284. case MB_TYPE_INTRA:
  3285. s->mv_dir = 0;
  3286. s->mb_intra= 1;
  3287. motion_x= s->mv[0][0][0] = 0;
  3288. motion_y= s->mv[0][0][1] = 0;
  3289. break;
  3290. case MB_TYPE_INTER:
  3291. s->mv_dir = MV_DIR_FORWARD;
  3292. s->mb_intra= 0;
  3293. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  3294. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  3295. break;
  3296. case MB_TYPE_INTER4V:
  3297. s->mv_dir = MV_DIR_FORWARD;
  3298. s->mv_type = MV_TYPE_8X8;
  3299. s->mb_intra= 0;
  3300. for(i=0; i<4; i++){
  3301. s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
  3302. s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
  3303. }
  3304. motion_x= motion_y= 0;
  3305. break;
  3306. case MB_TYPE_DIRECT:
  3307. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  3308. s->mb_intra= 0;
  3309. motion_x=s->b_direct_mv_table[xy][0];
  3310. motion_y=s->b_direct_mv_table[xy][1];
  3311. #ifdef CONFIG_RISKY
  3312. ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
  3313. #endif
  3314. break;
  3315. case MB_TYPE_BIDIR:
  3316. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  3317. s->mb_intra= 0;
  3318. motion_x=0;
  3319. motion_y=0;
  3320. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  3321. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  3322. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  3323. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  3324. break;
  3325. case MB_TYPE_BACKWARD:
  3326. s->mv_dir = MV_DIR_BACKWARD;
  3327. s->mb_intra= 0;
  3328. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  3329. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  3330. break;
  3331. case MB_TYPE_FORWARD:
  3332. s->mv_dir = MV_DIR_FORWARD;
  3333. s->mb_intra= 0;
  3334. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  3335. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  3336. // printf(" %d %d ", motion_x, motion_y);
  3337. break;
  3338. default:
  3339. motion_x=motion_y=0; //gcc warning fix
  3340. printf("illegal MB type\n");
  3341. }
  3342. encode_mb(s, motion_x, motion_y);
  3343. // RAL: Update last macrobloc type
  3344. s->last_mv_dir = s->mv_dir;
  3345. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
  3346. ff_h263_update_motion_val(s);
  3347. MPV_decode_mb(s, s->block);
  3348. }
  3349. /* clean the MV table in IPS frames for direct mode in B frames */
  3350. if(s->mb_intra /* && I,P,S_TYPE */){
  3351. s->p_mv_table[xy][0]=0;
  3352. s->p_mv_table[xy][1]=0;
  3353. }
  3354. if(s->flags&CODEC_FLAG_PSNR){
  3355. int w= 16;
  3356. int h= 16;
  3357. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  3358. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  3359. s->current_picture_ptr->error[0] += sse(
  3360. s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
  3361. s->dest[0], w, h, s->linesize);
  3362. s->current_picture_ptr->error[1] += sse(
  3363. s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  3364. s->dest[1], w>>1, h>>1, s->uvlinesize);
  3365. s->current_picture_ptr->error[2] += sse(
  3366. s, s->new_picture .data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  3367. s->dest[2], w>>1, h>>1, s->uvlinesize);
  3368. }
  3369. //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, get_bit_count(&s->pb));
  3370. }
  3371. }
  3372. emms_c();
  3373. #ifdef CONFIG_RISKY
  3374. if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
  3375. ff_mpeg4_merge_partitions(s);
  3376. if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
  3377. msmpeg4_encode_ext_header(s);
  3378. if(s->codec_id==CODEC_ID_MPEG4)
  3379. ff_mpeg4_stuffing(&s->pb);
  3380. #endif
  3381. //if (s->gob_number)
  3382. // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
  3383. /* Send the last GOB if RTP */
  3384. if (s->rtp_mode) {
  3385. flush_put_bits(&s->pb);
  3386. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  3387. /* Call the RTP callback to send the last GOB */
  3388. if (s->rtp_callback)
  3389. s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
  3390. s->ptr_lastgob = pbBufPtr(&s->pb);
  3391. //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif);
  3392. }
  3393. }
  3394. static int dct_quantize_trellis_c(MpegEncContext *s,
  3395. DCTELEM *block, int n,
  3396. int qscale, int *overflow){
  3397. const int *qmat;
  3398. const uint8_t *scantable= s->intra_scantable.scantable;
  3399. int max=0;
  3400. unsigned int threshold1, threshold2;
  3401. int bias=0;
  3402. int run_tab[65];
  3403. int level_tab[65];
  3404. int score_tab[65];
  3405. int last_run=0;
  3406. int last_level=0;
  3407. int last_score= 0;
  3408. int last_i= 0;
  3409. int coeff[3][64];
  3410. int coeff_count[64];
  3411. int lambda, qmul, qadd, start_i, last_non_zero, i;
  3412. const int esc_length= s->ac_esc_length;
  3413. uint8_t * length;
  3414. uint8_t * last_length;
  3415. int score_limit=0;
  3416. int left_limit= 0;
  3417. s->dsp.fdct (block);
  3418. qmul= qscale*16;
  3419. qadd= ((qscale-1)|1)*8;
  3420. if (s->mb_intra) {
  3421. int q;
  3422. if (!s->h263_aic) {
  3423. if (n < 4)
  3424. q = s->y_dc_scale;
  3425. else
  3426. q = s->c_dc_scale;
  3427. q = q << 3;
  3428. } else{
  3429. /* For AIC we skip quant/dequant of INTRADC */
  3430. q = 1 << 3;
  3431. qadd=0;
  3432. }
  3433. /* note: block[0] is assumed to be positive */
  3434. block[0] = (block[0] + (q >> 1)) / q;
  3435. start_i = 1;
  3436. last_non_zero = 0;
  3437. qmat = s->q_intra_matrix[qscale];
  3438. if(s->mpeg_quant || s->codec_id== CODEC_ID_MPEG1VIDEO)
  3439. bias= 1<<(QMAT_SHIFT-1);
  3440. length = s->intra_ac_vlc_length;
  3441. last_length= s->intra_ac_vlc_last_length;
  3442. } else {
  3443. start_i = 0;
  3444. last_non_zero = -1;
  3445. qmat = s->q_inter_matrix[qscale];
  3446. length = s->inter_ac_vlc_length;
  3447. last_length= s->inter_ac_vlc_last_length;
  3448. }
  3449. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  3450. threshold2= (threshold1<<1);
  3451. for(i=start_i; i<64; i++) {
  3452. const int j = scantable[i];
  3453. const int k= i-start_i;
  3454. int level = block[j];
  3455. level = level * qmat[j];
  3456. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  3457. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  3458. if(((unsigned)(level+threshold1))>threshold2){
  3459. if(level>0){
  3460. level= (bias + level)>>QMAT_SHIFT;
  3461. coeff[0][k]= level;
  3462. coeff[1][k]= level-1;
  3463. // coeff[2][k]= level-2;
  3464. }else{
  3465. level= (bias - level)>>QMAT_SHIFT;
  3466. coeff[0][k]= -level;
  3467. coeff[1][k]= -level+1;
  3468. // coeff[2][k]= -level+2;
  3469. }
  3470. coeff_count[k]= FFMIN(level, 2);
  3471. max |=level;
  3472. last_non_zero = i;
  3473. }else{
  3474. coeff[0][k]= (level>>31)|1;
  3475. coeff_count[k]= 1;
  3476. }
  3477. }
  3478. *overflow= s->max_qcoeff < max; //overflow might have happend
  3479. if(last_non_zero < start_i){
  3480. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  3481. return last_non_zero;
  3482. }
  3483. lambda= (qscale*qscale*64*105 + 64)>>7; //FIXME finetune
  3484. score_tab[0]= 0;
  3485. for(i=0; i<=last_non_zero - start_i; i++){
  3486. int level_index, run, j;
  3487. const int dct_coeff= block[ scantable[i + start_i] ];
  3488. const int zero_distoration= dct_coeff*dct_coeff;
  3489. int best_score=256*256*256*120;
  3490. last_score += zero_distoration;
  3491. for(level_index=0; level_index < coeff_count[i]; level_index++){
  3492. int distoration;
  3493. int level= coeff[level_index][i];
  3494. int unquant_coeff;
  3495. assert(level);
  3496. if(s->out_format == FMT_H263){
  3497. if(level>0){
  3498. unquant_coeff= level*qmul + qadd;
  3499. }else{
  3500. unquant_coeff= level*qmul - qadd;
  3501. }
  3502. }else{ //MPEG1
  3503. j= s->dsp.idct_permutation[ scantable[i + start_i] ]; //FIXME optimize
  3504. if(s->mb_intra){
  3505. if (level < 0) {
  3506. unquant_coeff = (int)((-level) * qscale * s->intra_matrix[j]) >> 3;
  3507. unquant_coeff = -((unquant_coeff - 1) | 1);
  3508. } else {
  3509. unquant_coeff = (int)( level * qscale * s->intra_matrix[j]) >> 3;
  3510. unquant_coeff = (unquant_coeff - 1) | 1;
  3511. }
  3512. }else{
  3513. if (level < 0) {
  3514. unquant_coeff = ((((-level) << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
  3515. unquant_coeff = -((unquant_coeff - 1) | 1);
  3516. } else {
  3517. unquant_coeff = ((( level << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
  3518. unquant_coeff = (unquant_coeff - 1) | 1;
  3519. }
  3520. }
  3521. unquant_coeff<<= 3;
  3522. }
  3523. distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff);
  3524. level+=64;
  3525. if((level&(~127)) == 0){
  3526. for(run=0; run<=i - left_limit; run++){
  3527. int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3528. score += score_tab[i-run];
  3529. if(score < best_score){
  3530. best_score=
  3531. score_tab[i+1]= score;
  3532. run_tab[i+1]= run;
  3533. level_tab[i+1]= level-64;
  3534. }
  3535. }
  3536. if(s->out_format == FMT_H263){
  3537. for(run=0; run<=i - left_limit; run++){
  3538. int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  3539. score += score_tab[i-run];
  3540. if(score < last_score){
  3541. last_score= score;
  3542. last_run= run;
  3543. last_level= level-64;
  3544. last_i= i+1;
  3545. }
  3546. }
  3547. }
  3548. }else{
  3549. distoration += esc_length*lambda;
  3550. for(run=0; run<=i - left_limit; run++){
  3551. int score= distoration + score_tab[i-run];
  3552. if(score < best_score){
  3553. best_score=
  3554. score_tab[i+1]= score;
  3555. run_tab[i+1]= run;
  3556. level_tab[i+1]= level-64;
  3557. }
  3558. }
  3559. if(s->out_format == FMT_H263){
  3560. for(run=0; run<=i - left_limit; run++){
  3561. int score= distoration + score_tab[i-run];
  3562. if(score < last_score){
  3563. last_score= score;
  3564. last_run= run;
  3565. last_level= level-64;
  3566. last_i= i+1;
  3567. }
  3568. }
  3569. }
  3570. }
  3571. }
  3572. for(j=left_limit; j<=i; j++){
  3573. score_tab[j] += zero_distoration;
  3574. }
  3575. score_limit+= zero_distoration;
  3576. if(score_tab[i+1] < score_limit)
  3577. score_limit= score_tab[i+1];
  3578. //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
  3579. while(score_tab[ left_limit ] > score_limit + lambda) left_limit++;
  3580. }
  3581. //FIXME add some cbp penalty
  3582. if(s->out_format != FMT_H263){
  3583. last_score= 256*256*256*120;
  3584. for(i= left_limit; i<=last_non_zero - start_i + 1; i++){
  3585. int score= score_tab[i];
  3586. if(i) score += lambda*2; //FIXME exacter?
  3587. if(score < last_score){
  3588. last_score= score;
  3589. last_i= i;
  3590. last_level= level_tab[i];
  3591. last_run= run_tab[i];
  3592. }
  3593. }
  3594. }
  3595. last_non_zero= last_i - 1 + start_i;
  3596. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  3597. if(last_non_zero < start_i)
  3598. return last_non_zero;
  3599. i= last_i;
  3600. assert(last_level);
  3601. //FIXME use permutated scantable
  3602. block[ s->dsp.idct_permutation[ scantable[last_non_zero] ] ]= last_level;
  3603. i -= last_run + 1;
  3604. for(;i>0 ; i -= run_tab[i] + 1){
  3605. const int j= s->dsp.idct_permutation[ scantable[i - 1 + start_i] ];
  3606. block[j]= level_tab[i];
  3607. assert(block[j]);
  3608. }
  3609. return last_non_zero;
  3610. }
  3611. static int dct_quantize_c(MpegEncContext *s,
  3612. DCTELEM *block, int n,
  3613. int qscale, int *overflow)
  3614. {
  3615. int i, j, level, last_non_zero, q;
  3616. const int *qmat;
  3617. const uint8_t *scantable= s->intra_scantable.scantable;
  3618. int bias;
  3619. int max=0;
  3620. unsigned int threshold1, threshold2;
  3621. s->dsp.fdct (block);
  3622. if (s->mb_intra) {
  3623. if (!s->h263_aic) {
  3624. if (n < 4)
  3625. q = s->y_dc_scale;
  3626. else
  3627. q = s->c_dc_scale;
  3628. q = q << 3;
  3629. } else
  3630. /* For AIC we skip quant/dequant of INTRADC */
  3631. q = 1 << 3;
  3632. /* note: block[0] is assumed to be positive */
  3633. block[0] = (block[0] + (q >> 1)) / q;
  3634. i = 1;
  3635. last_non_zero = 0;
  3636. qmat = s->q_intra_matrix[qscale];
  3637. bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  3638. } else {
  3639. i = 0;
  3640. last_non_zero = -1;
  3641. qmat = s->q_inter_matrix[qscale];
  3642. bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  3643. }
  3644. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  3645. threshold2= (threshold1<<1);
  3646. for(;i<64;i++) {
  3647. j = scantable[i];
  3648. level = block[j];
  3649. level = level * qmat[j];
  3650. // if( bias+level >= (1<<QMAT_SHIFT)
  3651. // || bias-level >= (1<<QMAT_SHIFT)){
  3652. if(((unsigned)(level+threshold1))>threshold2){
  3653. if(level>0){
  3654. level= (bias + level)>>QMAT_SHIFT;
  3655. block[j]= level;
  3656. }else{
  3657. level= (bias - level)>>QMAT_SHIFT;
  3658. block[j]= -level;
  3659. }
  3660. max |=level;
  3661. last_non_zero = i;
  3662. }else{
  3663. block[j]=0;
  3664. }
  3665. }
  3666. *overflow= s->max_qcoeff < max; //overflow might have happend
  3667. /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
  3668. if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
  3669. ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
  3670. return last_non_zero;
  3671. }
  3672. #endif //CONFIG_ENCODERS
  3673. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  3674. DCTELEM *block, int n, int qscale)
  3675. {
  3676. int i, level, nCoeffs;
  3677. const uint16_t *quant_matrix;
  3678. nCoeffs= s->block_last_index[n];
  3679. if (s->mb_intra) {
  3680. if (n < 4)
  3681. block[0] = block[0] * s->y_dc_scale;
  3682. else
  3683. block[0] = block[0] * s->c_dc_scale;
  3684. /* XXX: only mpeg1 */
  3685. quant_matrix = s->intra_matrix;
  3686. for(i=1;i<=nCoeffs;i++) {
  3687. int j= s->intra_scantable.permutated[i];
  3688. level = block[j];
  3689. if (level) {
  3690. if (level < 0) {
  3691. level = -level;
  3692. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  3693. level = (level - 1) | 1;
  3694. level = -level;
  3695. } else {
  3696. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  3697. level = (level - 1) | 1;
  3698. }
  3699. #ifdef PARANOID
  3700. if (level < -2048 || level > 2047)
  3701. fprintf(stderr, "unquant error %d %d\n", i, level);
  3702. #endif
  3703. block[j] = level;
  3704. }
  3705. }
  3706. } else {
  3707. i = 0;
  3708. quant_matrix = s->inter_matrix;
  3709. for(;i<=nCoeffs;i++) {
  3710. int j= s->intra_scantable.permutated[i];
  3711. level = block[j];
  3712. if (level) {
  3713. if (level < 0) {
  3714. level = -level;
  3715. level = (((level << 1) + 1) * qscale *
  3716. ((int) (quant_matrix[j]))) >> 4;
  3717. level = (level - 1) | 1;
  3718. level = -level;
  3719. } else {
  3720. level = (((level << 1) + 1) * qscale *
  3721. ((int) (quant_matrix[j]))) >> 4;
  3722. level = (level - 1) | 1;
  3723. }
  3724. #ifdef PARANOID
  3725. if (level < -2048 || level > 2047)
  3726. fprintf(stderr, "unquant error %d %d\n", i, level);
  3727. #endif
  3728. block[j] = level;
  3729. }
  3730. }
  3731. }
  3732. }
  3733. static void dct_unquantize_mpeg2_c(MpegEncContext *s,
  3734. DCTELEM *block, int n, int qscale)
  3735. {
  3736. int i, level, nCoeffs;
  3737. const uint16_t *quant_matrix;
  3738. if(s->alternate_scan) nCoeffs= 63;
  3739. else nCoeffs= s->block_last_index[n];
  3740. if (s->mb_intra) {
  3741. if (n < 4)
  3742. block[0] = block[0] * s->y_dc_scale;
  3743. else
  3744. block[0] = block[0] * s->c_dc_scale;
  3745. quant_matrix = s->intra_matrix;
  3746. for(i=1;i<=nCoeffs;i++) {
  3747. int j= s->intra_scantable.permutated[i];
  3748. level = block[j];
  3749. if (level) {
  3750. if (level < 0) {
  3751. level = -level;
  3752. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  3753. level = -level;
  3754. } else {
  3755. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  3756. }
  3757. #ifdef PARANOID
  3758. if (level < -2048 || level > 2047)
  3759. fprintf(stderr, "unquant error %d %d\n", i, level);
  3760. #endif
  3761. block[j] = level;
  3762. }
  3763. }
  3764. } else {
  3765. int sum=-1;
  3766. i = 0;
  3767. quant_matrix = s->inter_matrix;
  3768. for(;i<=nCoeffs;i++) {
  3769. int j= s->intra_scantable.permutated[i];
  3770. level = block[j];
  3771. if (level) {
  3772. if (level < 0) {
  3773. level = -level;
  3774. level = (((level << 1) + 1) * qscale *
  3775. ((int) (quant_matrix[j]))) >> 4;
  3776. level = -level;
  3777. } else {
  3778. level = (((level << 1) + 1) * qscale *
  3779. ((int) (quant_matrix[j]))) >> 4;
  3780. }
  3781. #ifdef PARANOID
  3782. if (level < -2048 || level > 2047)
  3783. fprintf(stderr, "unquant error %d %d\n", i, level);
  3784. #endif
  3785. block[j] = level;
  3786. sum+=level;
  3787. }
  3788. }
  3789. block[63]^=sum&1;
  3790. }
  3791. }
  3792. static void dct_unquantize_h263_c(MpegEncContext *s,
  3793. DCTELEM *block, int n, int qscale)
  3794. {
  3795. int i, level, qmul, qadd;
  3796. int nCoeffs;
  3797. assert(s->block_last_index[n]>=0);
  3798. qadd = (qscale - 1) | 1;
  3799. qmul = qscale << 1;
  3800. if (s->mb_intra) {
  3801. if (!s->h263_aic) {
  3802. if (n < 4)
  3803. block[0] = block[0] * s->y_dc_scale;
  3804. else
  3805. block[0] = block[0] * s->c_dc_scale;
  3806. }else
  3807. qadd = 0;
  3808. i = 1;
  3809. nCoeffs= 63; //does not allways use zigzag table
  3810. } else {
  3811. i = 0;
  3812. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  3813. }
  3814. for(;i<=nCoeffs;i++) {
  3815. level = block[i];
  3816. if (level) {
  3817. if (level < 0) {
  3818. level = level * qmul - qadd;
  3819. } else {
  3820. level = level * qmul + qadd;
  3821. }
  3822. #ifdef PARANOID
  3823. if (level < -2048 || level > 2047)
  3824. fprintf(stderr, "unquant error %d %d\n", i, level);
  3825. #endif
  3826. block[i] = level;
  3827. }
  3828. }
  3829. }
  3830. static const AVOption mpeg4_options[] =
  3831. {
  3832. AVOPTION_CODEC_INT("bitrate", "desired video bitrate", bit_rate, 4, 240000000, 800000),
  3833. AVOPTION_CODEC_INT("ratetol", "number of bits the bitstream is allowed to diverge from the reference"
  3834. "the reference can be CBR (for CBR pass1) or VBR (for pass2)",
  3835. bit_rate_tolerance, 4, 240000000, 8000),
  3836. AVOPTION_CODEC_INT("qmin", "minimum quantizer", qmin, 1, 31, 2),
  3837. AVOPTION_CODEC_INT("qmax", "maximum quantizer", qmax, 1, 31, 31),
  3838. AVOPTION_CODEC_STRING("rc_eq", "rate control equation",
  3839. rc_eq, "tex^qComp,option1,options2", 0),
  3840. AVOPTION_CODEC_INT("rc_minrate", "rate control minimum bitrate",
  3841. rc_min_rate, 4, 24000000, 0),
  3842. AVOPTION_CODEC_INT("rc_maxrate", "rate control maximum bitrate",
  3843. rc_max_rate, 4, 24000000, 0),
  3844. AVOPTION_CODEC_DOUBLE("rc_buf_aggresivity", "rate control buffer aggresivity",
  3845. rc_buffer_aggressivity, 4, 24000000, 0),
  3846. AVOPTION_CODEC_DOUBLE("rc_initial_cplx", "initial complexity for pass1 ratecontrol",
  3847. rc_initial_cplx, 0., 9999999., 0),
  3848. AVOPTION_CODEC_DOUBLE("i_quant_factor", "qscale factor between p and i frames",
  3849. i_quant_factor, 0., 0., 0),
  3850. AVOPTION_CODEC_DOUBLE("i_quant_offset", "qscale offset between p and i frames",
  3851. i_quant_factor, -999999., 999999., 0),
  3852. AVOPTION_CODEC_INT("dct_algo", "dct alghorithm",
  3853. dct_algo, 0, 5, 0), // fixme - "Auto,FastInt,Int,MMX,MLib,Altivec"
  3854. AVOPTION_CODEC_DOUBLE("lumi_masking", "luminance masking",
  3855. lumi_masking, 0., 999999., 0),
  3856. AVOPTION_CODEC_DOUBLE("temporal_cplx_masking", "temporary complexity masking",
  3857. temporal_cplx_masking, 0., 999999., 0),
  3858. AVOPTION_CODEC_DOUBLE("spatial_cplx_masking", "spatial complexity masking",
  3859. spatial_cplx_masking, 0., 999999., 0),
  3860. AVOPTION_CODEC_DOUBLE("p_masking", "p block masking",
  3861. p_masking, 0., 999999., 0),
  3862. AVOPTION_CODEC_DOUBLE("dark_masking", "darkness masking",
  3863. dark_masking, 0., 999999., 0),
  3864. AVOPTION_CODEC_INT("idct_algo", "idct alghorithm",
  3865. idct_algo, 0, 8, 0), // fixme - "Auto,Int,Simple,SimpleMMX,LibMPEG2MMX,PS2,MLib,ARM,Altivec"
  3866. AVOPTION_CODEC_INT("mb_qmin", "minimum MB quantizer",
  3867. mb_qmin, 0, 8, 0),
  3868. AVOPTION_CODEC_INT("mb_qmax", "maximum MB quantizer",
  3869. mb_qmin, 0, 8, 0),
  3870. AVOPTION_CODEC_INT("me_cmp", "ME compare function",
  3871. me_cmp, 0, 24000000, 0),
  3872. AVOPTION_CODEC_INT("me_sub_cmp", "subpixel ME compare function",
  3873. me_sub_cmp, 0, 24000000, 0),
  3874. AVOPTION_CODEC_INT("dia_size", "ME diamond size & shape",
  3875. dia_size, 0, 24000000, 0),
  3876. AVOPTION_CODEC_INT("last_predictor_count", "amount of previous MV predictors",
  3877. last_predictor_count, 0, 24000000, 0),
  3878. AVOPTION_CODEC_INT("pre_me", "pre pass for ME",
  3879. pre_me, 0, 24000000, 0),
  3880. AVOPTION_CODEC_INT("me_pre_cmp", "ME pre pass compare function",
  3881. me_pre_cmp, 0, 24000000, 0),
  3882. AVOPTION_CODEC_INT("me_range", "maximum ME search range",
  3883. me_range, 0, 24000000, 0),
  3884. AVOPTION_CODEC_INT("pre_dia_size", "ME pre pass diamod size & shape",
  3885. pre_dia_size, 0, 24000000, 0),
  3886. AVOPTION_CODEC_INT("me_subpel_quality", "subpel ME quality",
  3887. me_subpel_quality, 0, 24000000, 0),
  3888. AVOPTION_CODEC_INT("me_range", "maximum ME search range",
  3889. me_range, 0, 24000000, 0),
  3890. AVOPTION_CODEC_FLAG("psnr", "calculate PSNR of compressed frames",
  3891. flags, CODEC_FLAG_PSNR, 0),
  3892. AVOPTION_CODEC_RCOVERRIDE("rc_override", "ratecontrol override (=startframe,endframe,qscale,quality_factor)",
  3893. rc_override),
  3894. AVOPTION_SUB(avoptions_common),
  3895. AVOPTION_END()
  3896. };
  3897. #ifdef CONFIG_ENCODERS
  3898. AVCodec mpeg1video_encoder = {
  3899. "mpeg1video",
  3900. CODEC_TYPE_VIDEO,
  3901. CODEC_ID_MPEG1VIDEO,
  3902. sizeof(MpegEncContext),
  3903. MPV_encode_init,
  3904. MPV_encode_picture,
  3905. MPV_encode_end,
  3906. };
  3907. #ifdef CONFIG_RISKY
  3908. AVCodec h263_encoder = {
  3909. "h263",
  3910. CODEC_TYPE_VIDEO,
  3911. CODEC_ID_H263,
  3912. sizeof(MpegEncContext),
  3913. MPV_encode_init,
  3914. MPV_encode_picture,
  3915. MPV_encode_end,
  3916. };
  3917. AVCodec h263p_encoder = {
  3918. "h263p",
  3919. CODEC_TYPE_VIDEO,
  3920. CODEC_ID_H263P,
  3921. sizeof(MpegEncContext),
  3922. MPV_encode_init,
  3923. MPV_encode_picture,
  3924. MPV_encode_end,
  3925. };
  3926. AVCodec flv_encoder = {
  3927. "flv",
  3928. CODEC_TYPE_VIDEO,
  3929. CODEC_ID_FLV1,
  3930. sizeof(MpegEncContext),
  3931. MPV_encode_init,
  3932. MPV_encode_picture,
  3933. MPV_encode_end,
  3934. };
  3935. AVCodec rv10_encoder = {
  3936. "rv10",
  3937. CODEC_TYPE_VIDEO,
  3938. CODEC_ID_RV10,
  3939. sizeof(MpegEncContext),
  3940. MPV_encode_init,
  3941. MPV_encode_picture,
  3942. MPV_encode_end,
  3943. };
  3944. AVCodec mpeg4_encoder = {
  3945. "mpeg4",
  3946. CODEC_TYPE_VIDEO,
  3947. CODEC_ID_MPEG4,
  3948. sizeof(MpegEncContext),
  3949. MPV_encode_init,
  3950. MPV_encode_picture,
  3951. MPV_encode_end,
  3952. .options = mpeg4_options,
  3953. };
  3954. AVCodec msmpeg4v1_encoder = {
  3955. "msmpeg4v1",
  3956. CODEC_TYPE_VIDEO,
  3957. CODEC_ID_MSMPEG4V1,
  3958. sizeof(MpegEncContext),
  3959. MPV_encode_init,
  3960. MPV_encode_picture,
  3961. MPV_encode_end,
  3962. .options = mpeg4_options,
  3963. };
  3964. AVCodec msmpeg4v2_encoder = {
  3965. "msmpeg4v2",
  3966. CODEC_TYPE_VIDEO,
  3967. CODEC_ID_MSMPEG4V2,
  3968. sizeof(MpegEncContext),
  3969. MPV_encode_init,
  3970. MPV_encode_picture,
  3971. MPV_encode_end,
  3972. .options = mpeg4_options,
  3973. };
  3974. AVCodec msmpeg4v3_encoder = {
  3975. "msmpeg4",
  3976. CODEC_TYPE_VIDEO,
  3977. CODEC_ID_MSMPEG4V3,
  3978. sizeof(MpegEncContext),
  3979. MPV_encode_init,
  3980. MPV_encode_picture,
  3981. MPV_encode_end,
  3982. .options = mpeg4_options,
  3983. };
  3984. AVCodec wmv1_encoder = {
  3985. "wmv1",
  3986. CODEC_TYPE_VIDEO,
  3987. CODEC_ID_WMV1,
  3988. sizeof(MpegEncContext),
  3989. MPV_encode_init,
  3990. MPV_encode_picture,
  3991. MPV_encode_end,
  3992. .options = mpeg4_options,
  3993. };
  3994. #endif
  3995. AVCodec mjpeg_encoder = {
  3996. "mjpeg",
  3997. CODEC_TYPE_VIDEO,
  3998. CODEC_ID_MJPEG,
  3999. sizeof(MpegEncContext),
  4000. MPV_encode_init,
  4001. MPV_encode_picture,
  4002. MPV_encode_end,
  4003. };
  4004. #endif //CONFIG_ENCODERS