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.

4627 lines
160KB

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