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.

4741 lines
164KB

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