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.

4679 lines
162KB

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