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.

4600 lines
160KB

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