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.

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