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.

5338 lines
192KB

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