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.

4762 lines
165KB

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