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.

5361 lines
193KB

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