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.

5365 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. flush_put_bits(&s->pb);
  1670. s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
  1671. stuffing_count= ff_vbv_update(s, s->frame_bits);
  1672. if(stuffing_count){
  1673. switch(s->codec_id){
  1674. case CODEC_ID_MPEG1VIDEO:
  1675. case CODEC_ID_MPEG2VIDEO:
  1676. while(stuffing_count--){
  1677. put_bits(&s->pb, 8, 0);
  1678. }
  1679. break;
  1680. case CODEC_ID_MPEG4:
  1681. put_bits(&s->pb, 16, 0);
  1682. put_bits(&s->pb, 16, 0x1C3);
  1683. stuffing_count -= 4;
  1684. while(stuffing_count--){
  1685. put_bits(&s->pb, 8, 0xFF);
  1686. }
  1687. break;
  1688. default:
  1689. av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
  1690. }
  1691. flush_put_bits(&s->pb);
  1692. s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
  1693. }
  1694. /* update mpeg1/2 vbv_delay for CBR */
  1695. if(s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate){
  1696. int vbv_delay;
  1697. assert(s->repeat_first_field==0);
  1698. vbv_delay= lrintf(90000 * s->rc_context.buffer_index / s->avctx->rc_max_rate);
  1699. assert(vbv_delay < 0xFFFF);
  1700. s->vbv_delay_ptr[0] &= 0xF8;
  1701. s->vbv_delay_ptr[0] |= vbv_delay>>13;
  1702. s->vbv_delay_ptr[1] = vbv_delay>>5;
  1703. s->vbv_delay_ptr[2] &= 0x07;
  1704. s->vbv_delay_ptr[2] |= vbv_delay<<3;
  1705. }
  1706. s->total_bits += s->frame_bits;
  1707. avctx->frame_bits = s->frame_bits;
  1708. }else{
  1709. assert((pbBufPtr(&s->pb) == s->pb.buf));
  1710. s->frame_bits=0;
  1711. }
  1712. assert((s->frame_bits&7)==0);
  1713. return s->frame_bits/8;
  1714. }
  1715. #endif //CONFIG_ENCODERS
  1716. static inline void gmc1_motion(MpegEncContext *s,
  1717. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1718. int dest_offset,
  1719. uint8_t **ref_picture, int src_offset)
  1720. {
  1721. uint8_t *ptr;
  1722. int offset, src_x, src_y, linesize, uvlinesize;
  1723. int motion_x, motion_y;
  1724. int emu=0;
  1725. motion_x= s->sprite_offset[0][0];
  1726. motion_y= s->sprite_offset[0][1];
  1727. src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
  1728. src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
  1729. motion_x<<=(3-s->sprite_warping_accuracy);
  1730. motion_y<<=(3-s->sprite_warping_accuracy);
  1731. src_x = clip(src_x, -16, s->width);
  1732. if (src_x == s->width)
  1733. motion_x =0;
  1734. src_y = clip(src_y, -16, s->height);
  1735. if (src_y == s->height)
  1736. motion_y =0;
  1737. linesize = s->linesize;
  1738. uvlinesize = s->uvlinesize;
  1739. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  1740. dest_y+=dest_offset;
  1741. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1742. if( (unsigned)src_x >= s->h_edge_pos - 17
  1743. || (unsigned)src_y >= s->v_edge_pos - 17){
  1744. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
  1745. ptr= s->edge_emu_buffer;
  1746. }
  1747. }
  1748. if((motion_x|motion_y)&7){
  1749. s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1750. s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1751. }else{
  1752. int dxy;
  1753. dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
  1754. if (s->no_rounding){
  1755. s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
  1756. }else{
  1757. s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
  1758. }
  1759. }
  1760. if(s->flags&CODEC_FLAG_GRAY) return;
  1761. motion_x= s->sprite_offset[1][0];
  1762. motion_y= s->sprite_offset[1][1];
  1763. src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
  1764. src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
  1765. motion_x<<=(3-s->sprite_warping_accuracy);
  1766. motion_y<<=(3-s->sprite_warping_accuracy);
  1767. src_x = clip(src_x, -8, s->width>>1);
  1768. if (src_x == s->width>>1)
  1769. motion_x =0;
  1770. src_y = clip(src_y, -8, s->height>>1);
  1771. if (src_y == s->height>>1)
  1772. motion_y =0;
  1773. offset = (src_y * uvlinesize) + src_x + (src_offset>>1);
  1774. ptr = ref_picture[1] + offset;
  1775. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1776. if( (unsigned)src_x >= (s->h_edge_pos>>1) - 9
  1777. || (unsigned)src_y >= (s->v_edge_pos>>1) - 9){
  1778. 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);
  1779. ptr= s->edge_emu_buffer;
  1780. emu=1;
  1781. }
  1782. }
  1783. s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1784. ptr = ref_picture[2] + offset;
  1785. if(emu){
  1786. 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);
  1787. ptr= s->edge_emu_buffer;
  1788. }
  1789. s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
  1790. return;
  1791. }
  1792. static inline void gmc_motion(MpegEncContext *s,
  1793. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1794. int dest_offset,
  1795. uint8_t **ref_picture, int src_offset)
  1796. {
  1797. uint8_t *ptr;
  1798. int linesize, uvlinesize;
  1799. const int a= s->sprite_warping_accuracy;
  1800. int ox, oy;
  1801. linesize = s->linesize;
  1802. uvlinesize = s->uvlinesize;
  1803. ptr = ref_picture[0] + src_offset;
  1804. dest_y+=dest_offset;
  1805. ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
  1806. oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
  1807. s->dsp.gmc(dest_y, ptr, linesize, 16,
  1808. ox,
  1809. oy,
  1810. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1811. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1812. a+1, (1<<(2*a+1)) - s->no_rounding,
  1813. s->h_edge_pos, s->v_edge_pos);
  1814. s->dsp.gmc(dest_y+8, ptr, linesize, 16,
  1815. ox + s->sprite_delta[0][0]*8,
  1816. oy + s->sprite_delta[1][0]*8,
  1817. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1818. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1819. a+1, (1<<(2*a+1)) - s->no_rounding,
  1820. s->h_edge_pos, s->v_edge_pos);
  1821. if(s->flags&CODEC_FLAG_GRAY) return;
  1822. dest_cb+=dest_offset>>1;
  1823. dest_cr+=dest_offset>>1;
  1824. ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
  1825. oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
  1826. ptr = ref_picture[1] + (src_offset>>1);
  1827. s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
  1828. ox,
  1829. oy,
  1830. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1831. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1832. a+1, (1<<(2*a+1)) - s->no_rounding,
  1833. s->h_edge_pos>>1, s->v_edge_pos>>1);
  1834. ptr = ref_picture[2] + (src_offset>>1);
  1835. s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
  1836. ox,
  1837. oy,
  1838. s->sprite_delta[0][0], s->sprite_delta[0][1],
  1839. s->sprite_delta[1][0], s->sprite_delta[1][1],
  1840. a+1, (1<<(2*a+1)) - s->no_rounding,
  1841. s->h_edge_pos>>1, s->v_edge_pos>>1);
  1842. }
  1843. /**
  1844. * Copies a rectangular area of samples to a temporary buffer and replicates the boarder samples.
  1845. * @param buf destination buffer
  1846. * @param src source buffer
  1847. * @param linesize number of bytes between 2 vertically adjacent samples in both the source and destination buffers
  1848. * @param block_w width of block
  1849. * @param block_h height of block
  1850. * @param src_x x coordinate of the top left sample of the block in the source buffer
  1851. * @param src_y y coordinate of the top left sample of the block in the source buffer
  1852. * @param w width of the source buffer
  1853. * @param h height of the source buffer
  1854. */
  1855. void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h,
  1856. int src_x, int src_y, int w, int h){
  1857. int x, y;
  1858. int start_y, start_x, end_y, end_x;
  1859. if(src_y>= h){
  1860. src+= (h-1-src_y)*linesize;
  1861. src_y=h-1;
  1862. }else if(src_y<=-block_h){
  1863. src+= (1-block_h-src_y)*linesize;
  1864. src_y=1-block_h;
  1865. }
  1866. if(src_x>= w){
  1867. src+= (w-1-src_x);
  1868. src_x=w-1;
  1869. }else if(src_x<=-block_w){
  1870. src+= (1-block_w-src_x);
  1871. src_x=1-block_w;
  1872. }
  1873. start_y= FFMAX(0, -src_y);
  1874. start_x= FFMAX(0, -src_x);
  1875. end_y= FFMIN(block_h, h-src_y);
  1876. end_x= FFMIN(block_w, w-src_x);
  1877. // copy existing part
  1878. for(y=start_y; y<end_y; y++){
  1879. for(x=start_x; x<end_x; x++){
  1880. buf[x + y*linesize]= src[x + y*linesize];
  1881. }
  1882. }
  1883. //top
  1884. for(y=0; y<start_y; y++){
  1885. for(x=start_x; x<end_x; x++){
  1886. buf[x + y*linesize]= buf[x + start_y*linesize];
  1887. }
  1888. }
  1889. //bottom
  1890. for(y=end_y; y<block_h; y++){
  1891. for(x=start_x; x<end_x; x++){
  1892. buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
  1893. }
  1894. }
  1895. for(y=0; y<block_h; y++){
  1896. //left
  1897. for(x=0; x<start_x; x++){
  1898. buf[x + y*linesize]= buf[start_x + y*linesize];
  1899. }
  1900. //right
  1901. for(x=end_x; x<block_w; x++){
  1902. buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
  1903. }
  1904. }
  1905. }
  1906. static inline int hpel_motion(MpegEncContext *s,
  1907. uint8_t *dest, uint8_t *src,
  1908. int src_x, int src_y,
  1909. int width, int height, int stride,
  1910. int h_edge_pos, int v_edge_pos,
  1911. int w, int h, op_pixels_func *pix_op,
  1912. int motion_x, int motion_y)
  1913. {
  1914. int dxy;
  1915. int emu=0;
  1916. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  1917. src_x += motion_x >> 1;
  1918. src_y += motion_y >> 1;
  1919. /* WARNING: do no forget half pels */
  1920. src_x = clip(src_x, -16, width); //FIXME unneeded for emu?
  1921. if (src_x == width)
  1922. dxy &= ~1;
  1923. src_y = clip(src_y, -16, height);
  1924. if (src_y == height)
  1925. dxy &= ~2;
  1926. src += src_y * stride + src_x;
  1927. if(s->unrestricted_mv && (s->flags&CODEC_FLAG_EMU_EDGE)){
  1928. if( (unsigned)src_x > h_edge_pos - (motion_x&1) - w
  1929. || (unsigned)src_y > v_edge_pos - (motion_y&1) - h){
  1930. ff_emulated_edge_mc(s->edge_emu_buffer, src, stride, w+1, h+1,
  1931. src_x, src_y, h_edge_pos, v_edge_pos);
  1932. src= s->edge_emu_buffer;
  1933. emu=1;
  1934. }
  1935. }
  1936. pix_op[dxy](dest, src, stride, h);
  1937. return emu;
  1938. }
  1939. /* apply one mpeg motion vector to the three components */
  1940. static inline void mpeg_motion(MpegEncContext *s,
  1941. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1942. int dest_offset,
  1943. uint8_t **ref_picture, int src_offset,
  1944. int field_based, op_pixels_func (*pix_op)[4],
  1945. int motion_x, int motion_y, int h)
  1946. {
  1947. uint8_t *ptr;
  1948. int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, uvlinesize;
  1949. int emu=0;
  1950. #if 0
  1951. if(s->quarter_sample)
  1952. {
  1953. motion_x>>=1;
  1954. motion_y>>=1;
  1955. }
  1956. #endif
  1957. height = s->height >> field_based;
  1958. v_edge_pos = s->v_edge_pos >> field_based;
  1959. uvlinesize = s->current_picture.linesize[1] << field_based;
  1960. emu= hpel_motion(s,
  1961. dest_y + dest_offset, ref_picture[0] + src_offset,
  1962. s->mb_x * 16, s->mb_y * (16 >> field_based),
  1963. s->width, height, s->current_picture.linesize[0] << field_based,
  1964. s->h_edge_pos, v_edge_pos,
  1965. 16, h, pix_op[0],
  1966. motion_x, motion_y);
  1967. if(s->flags&CODEC_FLAG_GRAY) return;
  1968. if (s->out_format == FMT_H263) {
  1969. dxy = 0;
  1970. if ((motion_x & 3) != 0)
  1971. dxy |= 1;
  1972. if ((motion_y & 3) != 0)
  1973. dxy |= 2;
  1974. mx = motion_x >> 2;
  1975. my = motion_y >> 2;
  1976. } else {
  1977. mx = motion_x / 2;
  1978. my = motion_y / 2;
  1979. dxy = ((my & 1) << 1) | (mx & 1);
  1980. mx >>= 1;
  1981. my >>= 1;
  1982. }
  1983. src_x = s->mb_x * 8 + mx;
  1984. src_y = s->mb_y * (8 >> field_based) + my;
  1985. src_x = clip(src_x, -8, s->width >> 1);
  1986. if (src_x == (s->width >> 1))
  1987. dxy &= ~1;
  1988. src_y = clip(src_y, -8, height >> 1);
  1989. if (src_y == (height >> 1))
  1990. dxy &= ~2;
  1991. offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
  1992. ptr = ref_picture[1] + offset;
  1993. if(emu){
  1994. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
  1995. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  1996. ptr= s->edge_emu_buffer + (src_offset >> 1);
  1997. }
  1998. pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  1999. ptr = ref_picture[2] + offset;
  2000. if(emu){
  2001. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based,
  2002. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  2003. ptr= s->edge_emu_buffer + (src_offset >> 1);
  2004. }
  2005. pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  2006. }
  2007. //FIXME move to dsputil, avg variant, 16x16 version
  2008. static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
  2009. int x;
  2010. uint8_t * const top = src[1];
  2011. uint8_t * const left = src[2];
  2012. uint8_t * const mid = src[0];
  2013. uint8_t * const right = src[3];
  2014. uint8_t * const bottom= src[4];
  2015. #define OBMC_FILTER(x, t, l, m, r, b)\
  2016. dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
  2017. #define OBMC_FILTER4(x, t, l, m, r, b)\
  2018. OBMC_FILTER(x , t, l, m, r, b);\
  2019. OBMC_FILTER(x+1 , t, l, m, r, b);\
  2020. OBMC_FILTER(x +stride, t, l, m, r, b);\
  2021. OBMC_FILTER(x+1+stride, t, l, m, r, b);
  2022. x=0;
  2023. OBMC_FILTER (x , 2, 2, 4, 0, 0);
  2024. OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
  2025. OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
  2026. OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
  2027. OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
  2028. OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
  2029. x+= stride;
  2030. OBMC_FILTER (x , 1, 2, 5, 0, 0);
  2031. OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
  2032. OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
  2033. OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
  2034. x+= stride;
  2035. OBMC_FILTER4(x , 1, 2, 5, 0, 0);
  2036. OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
  2037. OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
  2038. OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
  2039. x+= 2*stride;
  2040. OBMC_FILTER4(x , 0, 2, 5, 0, 1);
  2041. OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
  2042. OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
  2043. OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
  2044. x+= 2*stride;
  2045. OBMC_FILTER (x , 0, 2, 5, 0, 1);
  2046. OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
  2047. OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
  2048. OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
  2049. OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
  2050. OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
  2051. x+= stride;
  2052. OBMC_FILTER (x , 0, 2, 4, 0, 2);
  2053. OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
  2054. OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
  2055. OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
  2056. }
  2057. /* obmc for 1 8x8 luma block */
  2058. static inline void obmc_motion(MpegEncContext *s,
  2059. uint8_t *dest, uint8_t *src,
  2060. int src_x, int src_y,
  2061. op_pixels_func *pix_op,
  2062. int16_t mv[5][2]/* mid top left right bottom*/)
  2063. #define MID 0
  2064. {
  2065. int i;
  2066. uint8_t *ptr[5];
  2067. assert(s->quarter_sample==0);
  2068. for(i=0; i<5; i++){
  2069. if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
  2070. ptr[i]= ptr[MID];
  2071. }else{
  2072. ptr[i]= s->edge_emu_buffer + 16 + 8*(i&1) + s->linesize*8*(i>>1);
  2073. hpel_motion(s, ptr[i], src,
  2074. src_x, src_y,
  2075. s->width, s->height, s->linesize,
  2076. s->h_edge_pos, s->v_edge_pos,
  2077. 8, 8, pix_op,
  2078. mv[i][0], mv[i][1]);
  2079. }
  2080. }
  2081. put_obmc(dest, ptr, s->linesize);
  2082. }
  2083. static inline void qpel_motion(MpegEncContext *s,
  2084. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2085. int dest_offset,
  2086. uint8_t **ref_picture, int src_offset,
  2087. int field_based, op_pixels_func (*pix_op)[4],
  2088. qpel_mc_func (*qpix_op)[16],
  2089. int motion_x, int motion_y, int h)
  2090. {
  2091. uint8_t *ptr;
  2092. int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize;
  2093. int emu=0;
  2094. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  2095. src_x = s->mb_x * 16 + (motion_x >> 2);
  2096. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
  2097. height = s->height >> field_based;
  2098. v_edge_pos = s->v_edge_pos >> field_based;
  2099. src_x = clip(src_x, -16, s->width);
  2100. if (src_x == s->width)
  2101. dxy &= ~3;
  2102. src_y = clip(src_y, -16, height);
  2103. if (src_y == height)
  2104. dxy &= ~12;
  2105. linesize = s->linesize << field_based;
  2106. uvlinesize = s->uvlinesize << field_based;
  2107. ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset;
  2108. dest_y += dest_offset;
  2109. //printf("%d %d %d\n", src_x, src_y, dxy);
  2110. if(s->flags&CODEC_FLAG_EMU_EDGE){
  2111. if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 16
  2112. || (unsigned)src_y > v_edge_pos - (motion_y&3) - h ){
  2113. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - src_offset, s->linesize, 17, 17+field_based,
  2114. src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
  2115. ptr= s->edge_emu_buffer + src_offset;
  2116. emu=1;
  2117. }
  2118. }
  2119. if(!field_based)
  2120. qpix_op[0][dxy](dest_y, ptr, linesize);
  2121. else{
  2122. //damn interlaced mode
  2123. //FIXME boundary mirroring is not exactly correct here
  2124. qpix_op[1][dxy](dest_y , ptr , linesize);
  2125. qpix_op[1][dxy](dest_y+8, ptr+8, linesize);
  2126. }
  2127. if(s->flags&CODEC_FLAG_GRAY) return;
  2128. if(field_based){
  2129. mx= motion_x/2;
  2130. my= motion_y>>1;
  2131. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
  2132. static const int rtab[8]= {0,0,1,1,0,0,0,1};
  2133. mx= (motion_x>>1) + rtab[motion_x&7];
  2134. my= (motion_y>>1) + rtab[motion_y&7];
  2135. }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
  2136. mx= (motion_x>>1)|(motion_x&1);
  2137. my= (motion_y>>1)|(motion_y&1);
  2138. }else{
  2139. mx= motion_x/2;
  2140. my= motion_y/2;
  2141. }
  2142. mx= (mx>>1)|(mx&1);
  2143. my= (my>>1)|(my&1);
  2144. dxy= (mx&1) | ((my&1)<<1);
  2145. mx>>=1;
  2146. my>>=1;
  2147. src_x = s->mb_x * 8 + mx;
  2148. src_y = s->mb_y * (8 >> field_based) + my;
  2149. src_x = clip(src_x, -8, s->width >> 1);
  2150. if (src_x == (s->width >> 1))
  2151. dxy &= ~1;
  2152. src_y = clip(src_y, -8, height >> 1);
  2153. if (src_y == (height >> 1))
  2154. dxy &= ~2;
  2155. offset = (src_y * uvlinesize) + src_x + (src_offset >> 1);
  2156. ptr = ref_picture[1] + offset;
  2157. if(emu){
  2158. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
  2159. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  2160. ptr= s->edge_emu_buffer + (src_offset >> 1);
  2161. }
  2162. pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  2163. ptr = ref_picture[2] + offset;
  2164. if(emu){
  2165. ff_emulated_edge_mc(s->edge_emu_buffer, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based,
  2166. src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
  2167. ptr= s->edge_emu_buffer + (src_offset >> 1);
  2168. }
  2169. pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1);
  2170. }
  2171. inline int ff_h263_round_chroma(int x){
  2172. if (x >= 0)
  2173. return (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
  2174. else {
  2175. x = -x;
  2176. return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
  2177. }
  2178. }
  2179. /**
  2180. * h263 chorma 4mv motion compensation.
  2181. */
  2182. static inline void chroma_4mv_motion(MpegEncContext *s,
  2183. uint8_t *dest_cb, uint8_t *dest_cr,
  2184. uint8_t **ref_picture,
  2185. op_pixels_func *pix_op,
  2186. int mx, int my){
  2187. int dxy, emu=0, src_x, src_y, offset;
  2188. uint8_t *ptr;
  2189. /* In case of 8X8, we construct a single chroma motion vector
  2190. with a special rounding */
  2191. mx= ff_h263_round_chroma(mx);
  2192. my= ff_h263_round_chroma(my);
  2193. dxy = ((my & 1) << 1) | (mx & 1);
  2194. mx >>= 1;
  2195. my >>= 1;
  2196. src_x = s->mb_x * 8 + mx;
  2197. src_y = s->mb_y * 8 + my;
  2198. src_x = clip(src_x, -8, s->width/2);
  2199. if (src_x == s->width/2)
  2200. dxy &= ~1;
  2201. src_y = clip(src_y, -8, s->height/2);
  2202. if (src_y == s->height/2)
  2203. dxy &= ~2;
  2204. offset = (src_y * (s->uvlinesize)) + src_x;
  2205. ptr = ref_picture[1] + offset;
  2206. if(s->flags&CODEC_FLAG_EMU_EDGE){
  2207. if( (unsigned)src_x > (s->h_edge_pos>>1) - (dxy &1) - 8
  2208. || (unsigned)src_y > (s->v_edge_pos>>1) - (dxy>>1) - 8){
  2209. 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);
  2210. ptr= s->edge_emu_buffer;
  2211. emu=1;
  2212. }
  2213. }
  2214. pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
  2215. ptr = ref_picture[2] + offset;
  2216. if(emu){
  2217. 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);
  2218. ptr= s->edge_emu_buffer;
  2219. }
  2220. pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
  2221. }
  2222. /**
  2223. * motion compesation of a single macroblock
  2224. * @param s context
  2225. * @param dest_y luma destination pointer
  2226. * @param dest_cb chroma cb/u destination pointer
  2227. * @param dest_cr chroma cr/v destination pointer
  2228. * @param dir direction (0->forward, 1->backward)
  2229. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  2230. * @param pic_op halfpel motion compensation function (average or put normally)
  2231. * @param pic_op qpel motion compensation function (average or put normally)
  2232. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  2233. */
  2234. static inline void MPV_motion(MpegEncContext *s,
  2235. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  2236. int dir, uint8_t **ref_picture,
  2237. op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
  2238. {
  2239. int dxy, mx, my, src_x, src_y, motion_x, motion_y;
  2240. int mb_x, mb_y, i;
  2241. uint8_t *ptr, *dest;
  2242. mb_x = s->mb_x;
  2243. mb_y = s->mb_y;
  2244. if(s->obmc && s->pict_type != B_TYPE){
  2245. int16_t mv_cache[4][4][2];
  2246. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  2247. const int mot_stride= s->mb_width*2 + 2;
  2248. const int mot_xy= 1 + mb_x*2 + (mb_y*2 + 1)*mot_stride;
  2249. assert(!s->mb_skiped);
  2250. memcpy(mv_cache[1][1], s->current_picture.motion_val[0][mot_xy ], sizeof(int16_t)*4);
  2251. memcpy(mv_cache[2][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
  2252. memcpy(mv_cache[3][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
  2253. if(mb_y==0 || IS_INTRA(s->current_picture.mb_type[xy-s->mb_stride])){
  2254. memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4);
  2255. }else{
  2256. memcpy(mv_cache[0][1], s->current_picture.motion_val[0][mot_xy-mot_stride], sizeof(int16_t)*4);
  2257. }
  2258. if(mb_x==0 || IS_INTRA(s->current_picture.mb_type[xy-1])){
  2259. *(int32_t*)mv_cache[1][0]= *(int32_t*)mv_cache[1][1];
  2260. *(int32_t*)mv_cache[2][0]= *(int32_t*)mv_cache[2][1];
  2261. }else{
  2262. *(int32_t*)mv_cache[1][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1];
  2263. *(int32_t*)mv_cache[2][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1+mot_stride];
  2264. }
  2265. if(mb_x+1>=s->mb_width || IS_INTRA(s->current_picture.mb_type[xy+1])){
  2266. *(int32_t*)mv_cache[1][3]= *(int32_t*)mv_cache[1][2];
  2267. *(int32_t*)mv_cache[2][3]= *(int32_t*)mv_cache[2][2];
  2268. }else{
  2269. *(int32_t*)mv_cache[1][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2];
  2270. *(int32_t*)mv_cache[2][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2+mot_stride];
  2271. }
  2272. mx = 0;
  2273. my = 0;
  2274. for(i=0;i<4;i++) {
  2275. const int x= (i&1)+1;
  2276. const int y= (i>>1)+1;
  2277. int16_t mv[5][2]= {
  2278. {mv_cache[y][x ][0], mv_cache[y][x ][1]},
  2279. {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
  2280. {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
  2281. {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
  2282. {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
  2283. //FIXME cleanup
  2284. obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  2285. ref_picture[0],
  2286. mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
  2287. pix_op[1],
  2288. mv);
  2289. mx += mv[0][0];
  2290. my += mv[0][1];
  2291. }
  2292. if(!(s->flags&CODEC_FLAG_GRAY))
  2293. chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
  2294. return;
  2295. }
  2296. switch(s->mv_type) {
  2297. case MV_TYPE_16X16:
  2298. #ifdef CONFIG_RISKY
  2299. if(s->mcsel){
  2300. if(s->real_sprite_warping_points==1){
  2301. gmc1_motion(s, dest_y, dest_cb, dest_cr, 0,
  2302. ref_picture, 0);
  2303. }else{
  2304. gmc_motion(s, dest_y, dest_cb, dest_cr, 0,
  2305. ref_picture, 0);
  2306. }
  2307. }else if(s->quarter_sample){
  2308. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  2309. ref_picture, 0,
  2310. 0, pix_op, qpix_op,
  2311. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2312. }else if(s->mspel){
  2313. ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
  2314. ref_picture, pix_op,
  2315. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2316. }else
  2317. #endif
  2318. {
  2319. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2320. ref_picture, 0,
  2321. 0, pix_op,
  2322. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2323. }
  2324. break;
  2325. case MV_TYPE_8X8:
  2326. mx = 0;
  2327. my = 0;
  2328. if(s->quarter_sample){
  2329. for(i=0;i<4;i++) {
  2330. motion_x = s->mv[dir][i][0];
  2331. motion_y = s->mv[dir][i][1];
  2332. dxy = ((motion_y & 3) << 2) | (motion_x & 3);
  2333. src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
  2334. src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
  2335. /* WARNING: do no forget half pels */
  2336. src_x = clip(src_x, -16, s->width);
  2337. if (src_x == s->width)
  2338. dxy &= ~3;
  2339. src_y = clip(src_y, -16, s->height);
  2340. if (src_y == s->height)
  2341. dxy &= ~12;
  2342. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  2343. if(s->flags&CODEC_FLAG_EMU_EDGE){
  2344. if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 8
  2345. || (unsigned)src_y > s->v_edge_pos - (motion_y&3) - 8 ){
  2346. 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);
  2347. ptr= s->edge_emu_buffer;
  2348. }
  2349. }
  2350. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  2351. qpix_op[1][dxy](dest, ptr, s->linesize);
  2352. mx += s->mv[dir][i][0]/2;
  2353. my += s->mv[dir][i][1]/2;
  2354. }
  2355. }else{
  2356. for(i=0;i<4;i++) {
  2357. hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
  2358. ref_picture[0],
  2359. mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
  2360. s->width, s->height, s->linesize,
  2361. s->h_edge_pos, s->v_edge_pos,
  2362. 8, 8, pix_op[1],
  2363. s->mv[dir][i][0], s->mv[dir][i][1]);
  2364. mx += s->mv[dir][i][0];
  2365. my += s->mv[dir][i][1];
  2366. }
  2367. }
  2368. if(!(s->flags&CODEC_FLAG_GRAY))
  2369. chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
  2370. break;
  2371. case MV_TYPE_FIELD:
  2372. if (s->picture_structure == PICT_FRAME) {
  2373. if(s->quarter_sample){
  2374. /* top field */
  2375. qpel_motion(s, dest_y, dest_cb, dest_cr, 0,
  2376. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  2377. 1, pix_op, qpix_op,
  2378. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  2379. /* bottom field */
  2380. qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  2381. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  2382. 1, pix_op, qpix_op,
  2383. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  2384. }else{
  2385. /* top field */
  2386. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2387. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  2388. 1, pix_op,
  2389. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  2390. /* bottom field */
  2391. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  2392. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  2393. 1, pix_op,
  2394. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  2395. }
  2396. } else {
  2397. int offset;
  2398. if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
  2399. offset= s->field_select[dir][0] ? s->linesize : 0;
  2400. }else{
  2401. ref_picture= s->current_picture.data;
  2402. offset= s->field_select[dir][0] ? s->linesize : -s->linesize;
  2403. }
  2404. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2405. ref_picture, offset,
  2406. 0, pix_op,
  2407. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  2408. }
  2409. break;
  2410. case MV_TYPE_16X8:{
  2411. int offset;
  2412. uint8_t ** ref2picture;
  2413. if(s->picture_structure == s->field_select[dir][0] + 1 || s->pict_type == B_TYPE || s->first_field){
  2414. ref2picture= ref_picture;
  2415. offset= s->field_select[dir][0] ? s->linesize : 0;
  2416. }else{
  2417. ref2picture= s->current_picture.data;
  2418. offset= s->field_select[dir][0] ? s->linesize : -s->linesize;
  2419. }
  2420. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2421. ref2picture, offset,
  2422. 0, pix_op,
  2423. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  2424. if(s->picture_structure == s->field_select[dir][1] + 1 || s->pict_type == B_TYPE || s->first_field){
  2425. ref2picture= ref_picture;
  2426. offset= s->field_select[dir][1] ? s->linesize : 0;
  2427. }else{
  2428. ref2picture= s->current_picture.data;
  2429. offset= s->field_select[dir][1] ? s->linesize : -s->linesize;
  2430. }
  2431. // I know it is ugly but this is the only way to fool emu_edge without rewrite mpeg_motion
  2432. mpeg_motion(s, dest_y+16*s->linesize, dest_cb+8*s->uvlinesize, dest_cr+8*s->uvlinesize,
  2433. 0,
  2434. ref2picture, offset,
  2435. 0, pix_op,
  2436. s->mv[dir][1][0], s->mv[dir][1][1]+16, 8);
  2437. }
  2438. break;
  2439. case MV_TYPE_DMV:
  2440. {
  2441. op_pixels_func (*dmv_pix_op)[4];
  2442. int offset;
  2443. dmv_pix_op = s->dsp.put_pixels_tab;
  2444. if(s->picture_structure == PICT_FRAME){
  2445. //put top field from top field
  2446. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2447. ref_picture, 0,
  2448. 1, dmv_pix_op,
  2449. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  2450. //put bottom field from bottom field
  2451. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  2452. ref_picture, s->linesize,
  2453. 1, dmv_pix_op,
  2454. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  2455. dmv_pix_op = s->dsp.avg_pixels_tab;
  2456. //avg top field from bottom field
  2457. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2458. ref_picture, s->linesize,
  2459. 1, dmv_pix_op,
  2460. s->mv[dir][2][0], s->mv[dir][2][1], 8);
  2461. //avg bottom field from top field
  2462. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  2463. ref_picture, 0,
  2464. 1, dmv_pix_op,
  2465. s->mv[dir][3][0], s->mv[dir][3][1], 8);
  2466. }else{
  2467. offset=(s->picture_structure == PICT_BOTTOM_FIELD)?
  2468. s->linesize : 0;
  2469. //put field from the same parity
  2470. //same parity is never in the same frame
  2471. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  2472. ref_picture,offset,
  2473. 0,dmv_pix_op,
  2474. s->mv[dir][0][0],s->mv[dir][0][1],16);
  2475. // after put we make avg of the same block
  2476. dmv_pix_op=s->dsp.avg_pixels_tab;
  2477. //opposite parity is always in the same frame if this is second field
  2478. if(!s->first_field){
  2479. ref_picture = s->current_picture.data;
  2480. //top field is one linesize from frame beginig
  2481. offset=(s->picture_structure == PICT_BOTTOM_FIELD)?
  2482. -s->linesize : s->linesize;
  2483. }else
  2484. offset=(s->picture_structure == PICT_BOTTOM_FIELD)?
  2485. 0 : s->linesize;
  2486. //avg field from the opposite parity
  2487. mpeg_motion(s, dest_y, dest_cb, dest_cr,0,
  2488. ref_picture, offset,
  2489. 0,dmv_pix_op,
  2490. s->mv[dir][2][0],s->mv[dir][2][1],16);
  2491. }
  2492. }
  2493. break;
  2494. default: assert(0);
  2495. }
  2496. }
  2497. /* put block[] to dest[] */
  2498. static inline void put_dct(MpegEncContext *s,
  2499. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  2500. {
  2501. s->dct_unquantize_intra(s, block, i, qscale);
  2502. s->dsp.idct_put (dest, line_size, block);
  2503. }
  2504. /* add block[] to dest[] */
  2505. static inline void add_dct(MpegEncContext *s,
  2506. DCTELEM *block, int i, uint8_t *dest, int line_size)
  2507. {
  2508. if (s->block_last_index[i] >= 0) {
  2509. s->dsp.idct_add (dest, line_size, block);
  2510. }
  2511. }
  2512. static inline void add_dequant_dct(MpegEncContext *s,
  2513. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  2514. {
  2515. if (s->block_last_index[i] >= 0) {
  2516. s->dct_unquantize_inter(s, block, i, qscale);
  2517. s->dsp.idct_add (dest, line_size, block);
  2518. }
  2519. }
  2520. /**
  2521. * cleans dc, ac, coded_block for the current non intra MB
  2522. */
  2523. void ff_clean_intra_table_entries(MpegEncContext *s)
  2524. {
  2525. int wrap = s->block_wrap[0];
  2526. int xy = s->block_index[0];
  2527. s->dc_val[0][xy ] =
  2528. s->dc_val[0][xy + 1 ] =
  2529. s->dc_val[0][xy + wrap] =
  2530. s->dc_val[0][xy + 1 + wrap] = 1024;
  2531. /* ac pred */
  2532. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  2533. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  2534. if (s->msmpeg4_version>=3) {
  2535. s->coded_block[xy ] =
  2536. s->coded_block[xy + 1 ] =
  2537. s->coded_block[xy + wrap] =
  2538. s->coded_block[xy + 1 + wrap] = 0;
  2539. }
  2540. /* chroma */
  2541. wrap = s->block_wrap[4];
  2542. xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
  2543. s->dc_val[1][xy] =
  2544. s->dc_val[2][xy] = 1024;
  2545. /* ac pred */
  2546. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  2547. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  2548. s->mbintra_table[s->mb_x + s->mb_y*s->mb_stride]= 0;
  2549. }
  2550. /* generic function called after a macroblock has been parsed by the
  2551. decoder or after it has been encoded by the encoder.
  2552. Important variables used:
  2553. s->mb_intra : true if intra macroblock
  2554. s->mv_dir : motion vector direction
  2555. s->mv_type : motion vector type
  2556. s->mv : motion vector
  2557. s->interlaced_dct : true if interlaced dct used (mpeg2)
  2558. */
  2559. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  2560. {
  2561. int mb_x, mb_y;
  2562. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  2563. #ifdef HAVE_XVMC
  2564. if(s->avctx->xvmc_acceleration){
  2565. XVMC_decode_mb(s);//xvmc uses pblocks
  2566. return;
  2567. }
  2568. #endif
  2569. mb_x = s->mb_x;
  2570. mb_y = s->mb_y;
  2571. s->current_picture.qscale_table[mb_xy]= s->qscale;
  2572. /* update DC predictors for P macroblocks */
  2573. if (!s->mb_intra) {
  2574. if (s->h263_pred || s->h263_aic) {
  2575. if(s->mbintra_table[mb_xy])
  2576. ff_clean_intra_table_entries(s);
  2577. } else {
  2578. s->last_dc[0] =
  2579. s->last_dc[1] =
  2580. s->last_dc[2] = 128 << s->intra_dc_precision;
  2581. }
  2582. }
  2583. else if (s->h263_pred || s->h263_aic)
  2584. s->mbintra_table[mb_xy]=1;
  2585. if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc
  2586. uint8_t *dest_y, *dest_cb, *dest_cr;
  2587. int dct_linesize, dct_offset;
  2588. op_pixels_func (*op_pix)[4];
  2589. qpel_mc_func (*op_qpix)[16];
  2590. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
  2591. const int uvlinesize= s->current_picture.linesize[1];
  2592. const int readable= s->pict_type != B_TYPE || s->encoding || s->avctx->draw_horiz_band;
  2593. /* avoid copy if macroblock skipped in last frame too */
  2594. /* skip only during decoding as we might trash the buffers during encoding a bit */
  2595. if(!s->encoding){
  2596. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  2597. const int age= s->current_picture.age;
  2598. assert(age);
  2599. if (s->mb_skiped) {
  2600. s->mb_skiped= 0;
  2601. assert(s->pict_type!=I_TYPE);
  2602. (*mbskip_ptr) ++; /* indicate that this time we skiped it */
  2603. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  2604. /* if previous was skipped too, then nothing to do ! */
  2605. if (*mbskip_ptr >= age && s->current_picture.reference){
  2606. return;
  2607. }
  2608. } else if(!s->current_picture.reference){
  2609. (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
  2610. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  2611. } else{
  2612. *mbskip_ptr = 0; /* not skipped */
  2613. }
  2614. }
  2615. if (s->interlaced_dct) {
  2616. dct_linesize = linesize * 2;
  2617. dct_offset = linesize;
  2618. } else {
  2619. dct_linesize = linesize;
  2620. dct_offset = linesize * 8;
  2621. }
  2622. if(readable){
  2623. dest_y= s->dest[0];
  2624. dest_cb= s->dest[1];
  2625. dest_cr= s->dest[2];
  2626. }else{
  2627. dest_y = s->edge_emu_buffer+32; //FIXME cleanup scratchpad pointers
  2628. dest_cb= s->edge_emu_buffer+48;
  2629. dest_cr= s->edge_emu_buffer+56;
  2630. }
  2631. if (!s->mb_intra) {
  2632. /* motion handling */
  2633. /* decoding or more than one mb_type (MC was allready done otherwise) */
  2634. if(!s->encoding){
  2635. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  2636. op_pix = s->dsp.put_pixels_tab;
  2637. op_qpix= s->dsp.put_qpel_pixels_tab;
  2638. }else{
  2639. op_pix = s->dsp.put_no_rnd_pixels_tab;
  2640. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  2641. }
  2642. if (s->mv_dir & MV_DIR_FORWARD) {
  2643. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  2644. op_pix = s->dsp.avg_pixels_tab;
  2645. op_qpix= s->dsp.avg_qpel_pixels_tab;
  2646. }
  2647. if (s->mv_dir & MV_DIR_BACKWARD) {
  2648. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  2649. }
  2650. }
  2651. /* skip dequant / idct if we are really late ;) */
  2652. if(s->hurry_up>1) return;
  2653. /* add dct residue */
  2654. if(s->encoding || !( s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
  2655. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  2656. add_dequant_dct(s, block[0], 0, dest_y, dct_linesize, s->qscale);
  2657. add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize, s->qscale);
  2658. add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize, s->qscale);
  2659. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize, s->qscale);
  2660. if(!(s->flags&CODEC_FLAG_GRAY)){
  2661. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2662. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2663. }
  2664. } else if(s->codec_id != CODEC_ID_WMV2){
  2665. add_dct(s, block[0], 0, dest_y, dct_linesize);
  2666. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  2667. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  2668. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  2669. if(!(s->flags&CODEC_FLAG_GRAY)){
  2670. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  2671. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  2672. }
  2673. }
  2674. #ifdef CONFIG_RISKY
  2675. else{
  2676. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  2677. }
  2678. #endif
  2679. } else {
  2680. /* dct only in intra block */
  2681. if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
  2682. put_dct(s, block[0], 0, dest_y, dct_linesize, s->qscale);
  2683. put_dct(s, block[1], 1, dest_y + 8, dct_linesize, s->qscale);
  2684. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize, s->qscale);
  2685. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize, s->qscale);
  2686. if(!(s->flags&CODEC_FLAG_GRAY)){
  2687. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  2688. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  2689. }
  2690. }else{
  2691. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  2692. s->dsp.idct_put(dest_y + 8, dct_linesize, block[1]);
  2693. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  2694. s->dsp.idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]);
  2695. if(!(s->flags&CODEC_FLAG_GRAY)){
  2696. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  2697. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  2698. }
  2699. }
  2700. }
  2701. if(!readable){
  2702. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2703. s->dsp.put_pixels_tab[1][0](s->dest[1], dest_cb, uvlinesize, 8);
  2704. s->dsp.put_pixels_tab[1][0](s->dest[2], dest_cr, uvlinesize, 8);
  2705. }
  2706. }
  2707. }
  2708. #ifdef CONFIG_ENCODERS
  2709. static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
  2710. {
  2711. static const char tab[64]=
  2712. {3,2,2,1,1,1,1,1,
  2713. 1,1,1,1,1,1,1,1,
  2714. 1,1,1,1,1,1,1,1,
  2715. 0,0,0,0,0,0,0,0,
  2716. 0,0,0,0,0,0,0,0,
  2717. 0,0,0,0,0,0,0,0,
  2718. 0,0,0,0,0,0,0,0,
  2719. 0,0,0,0,0,0,0,0};
  2720. int score=0;
  2721. int run=0;
  2722. int i;
  2723. DCTELEM *block= s->block[n];
  2724. const int last_index= s->block_last_index[n];
  2725. int skip_dc;
  2726. if(threshold<0){
  2727. skip_dc=0;
  2728. threshold= -threshold;
  2729. }else
  2730. skip_dc=1;
  2731. /* are all which we could set to zero are allready zero? */
  2732. if(last_index<=skip_dc - 1) return;
  2733. for(i=0; i<=last_index; i++){
  2734. const int j = s->intra_scantable.permutated[i];
  2735. const int level = ABS(block[j]);
  2736. if(level==1){
  2737. if(skip_dc && i==0) continue;
  2738. score+= tab[run];
  2739. run=0;
  2740. }else if(level>1){
  2741. return;
  2742. }else{
  2743. run++;
  2744. }
  2745. }
  2746. if(score >= threshold) return;
  2747. for(i=skip_dc; i<=last_index; i++){
  2748. const int j = s->intra_scantable.permutated[i];
  2749. block[j]=0;
  2750. }
  2751. if(block[0]) s->block_last_index[n]= 0;
  2752. else s->block_last_index[n]= -1;
  2753. }
  2754. static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
  2755. {
  2756. int i;
  2757. const int maxlevel= s->max_qcoeff;
  2758. const int minlevel= s->min_qcoeff;
  2759. int overflow=0;
  2760. if(s->mb_intra){
  2761. i=1; //skip clipping of intra dc
  2762. }else
  2763. i=0;
  2764. for(;i<=last_index; i++){
  2765. const int j= s->intra_scantable.permutated[i];
  2766. int level = block[j];
  2767. if (level>maxlevel){
  2768. level=maxlevel;
  2769. overflow++;
  2770. }else if(level<minlevel){
  2771. level=minlevel;
  2772. overflow++;
  2773. }
  2774. block[j]= level;
  2775. }
  2776. if(overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
  2777. av_log(s->avctx, AV_LOG_INFO, "warning, cliping %d dct coefficents to %d..%d\n", overflow, minlevel, maxlevel);
  2778. }
  2779. #endif //CONFIG_ENCODERS
  2780. /**
  2781. *
  2782. * @param h is the normal height, this will be reduced automatically if needed for the last row
  2783. */
  2784. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  2785. if (s->avctx->draw_horiz_band) {
  2786. AVFrame *src;
  2787. int offset[4];
  2788. if(s->picture_structure != PICT_FRAME){
  2789. h <<= 1;
  2790. y <<= 1;
  2791. if(s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  2792. }
  2793. h= FFMIN(h, s->height - y);
  2794. if(s->pict_type==B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  2795. src= (AVFrame*)s->current_picture_ptr;
  2796. else if(s->last_picture_ptr)
  2797. src= (AVFrame*)s->last_picture_ptr;
  2798. else
  2799. return;
  2800. if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  2801. offset[0]=
  2802. offset[1]=
  2803. offset[2]=
  2804. offset[3]= 0;
  2805. }else{
  2806. offset[0]= y * s->linesize;;
  2807. offset[1]=
  2808. offset[2]= (y>>1) * s->uvlinesize;;
  2809. offset[3]= 0;
  2810. }
  2811. emms_c();
  2812. s->avctx->draw_horiz_band(s->avctx, src, offset,
  2813. y, s->picture_structure, h);
  2814. }
  2815. }
  2816. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2817. const int linesize= s->current_picture.linesize[0]; //not s->linesize as this woulnd be wrong for field pics
  2818. const int uvlinesize= s->current_picture.linesize[1];
  2819. s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2820. s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1) + s->mb_x*2;
  2821. s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1 + s->mb_x*2;
  2822. s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2) + s->mb_x*2;
  2823. s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2) + s->mb_x;
  2824. 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;
  2825. if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME){
  2826. s->dest[0] = s->current_picture.data[0] + s->mb_x * 16 - 16;
  2827. s->dest[1] = s->current_picture.data[1] + s->mb_x * 8 - 8;
  2828. s->dest[2] = s->current_picture.data[2] + s->mb_x * 8 - 8;
  2829. }else{
  2830. s->dest[0] = s->current_picture.data[0] + (s->mb_y * 16* linesize ) + s->mb_x * 16 - 16;
  2831. s->dest[1] = s->current_picture.data[1] + (s->mb_y * 8 * uvlinesize) + s->mb_x * 8 - 8;
  2832. s->dest[2] = s->current_picture.data[2] + (s->mb_y * 8 * uvlinesize) + s->mb_x * 8 - 8;
  2833. }
  2834. }
  2835. #ifdef CONFIG_ENCODERS
  2836. static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
  2837. {
  2838. const int mb_x= s->mb_x;
  2839. const int mb_y= s->mb_y;
  2840. int i;
  2841. int skip_dct[6];
  2842. int dct_offset = s->linesize*8; //default for progressive frames
  2843. for(i=0; i<6; i++) skip_dct[i]=0;
  2844. if(s->adaptive_quant){
  2845. const int last_qp= s->qscale;
  2846. const int mb_xy= mb_x + mb_y*s->mb_stride;
  2847. s->lambda= s->lambda_table[mb_xy];
  2848. update_qscale(s);
  2849. if(!(s->flags&CODEC_FLAG_QP_RD)){
  2850. s->dquant= s->qscale - last_qp;
  2851. if(s->out_format==FMT_H263)
  2852. s->dquant= clip(s->dquant, -2, 2); //FIXME RD
  2853. if(s->codec_id==CODEC_ID_MPEG4){
  2854. if(!s->mb_intra){
  2855. if((s->mv_dir&MV_DIRECT) || s->mv_type==MV_TYPE_8X8)
  2856. s->dquant=0;
  2857. }
  2858. }
  2859. }
  2860. ff_set_qscale(s, last_qp + s->dquant);
  2861. }
  2862. if (s->mb_intra) {
  2863. uint8_t *ptr;
  2864. int wrap_y;
  2865. int emu=0;
  2866. wrap_y = s->linesize;
  2867. ptr = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  2868. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  2869. ff_emulated_edge_mc(s->edge_emu_buffer, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
  2870. ptr= s->edge_emu_buffer;
  2871. emu=1;
  2872. }
  2873. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  2874. int progressive_score, interlaced_score;
  2875. s->interlaced_dct=0;
  2876. progressive_score= s->dsp.ildct_cmp[4](s, ptr , NULL, wrap_y, 8)
  2877. +s->dsp.ildct_cmp[4](s, ptr + wrap_y*8, NULL, wrap_y, 8) - 400;
  2878. if(progressive_score > 0){
  2879. interlaced_score = s->dsp.ildct_cmp[4](s, ptr , NULL, wrap_y*2, 8)
  2880. +s->dsp.ildct_cmp[4](s, ptr + wrap_y , NULL, wrap_y*2, 8);
  2881. if(progressive_score > interlaced_score){
  2882. s->interlaced_dct=1;
  2883. dct_offset= wrap_y;
  2884. wrap_y<<=1;
  2885. }
  2886. }
  2887. }
  2888. s->dsp.get_pixels(s->block[0], ptr , wrap_y);
  2889. s->dsp.get_pixels(s->block[1], ptr + 8, wrap_y);
  2890. s->dsp.get_pixels(s->block[2], ptr + dct_offset , wrap_y);
  2891. s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y);
  2892. if(s->flags&CODEC_FLAG_GRAY){
  2893. skip_dct[4]= 1;
  2894. skip_dct[5]= 1;
  2895. }else{
  2896. int wrap_c = s->uvlinesize;
  2897. ptr = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2898. if(emu){
  2899. 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);
  2900. ptr= s->edge_emu_buffer;
  2901. }
  2902. s->dsp.get_pixels(s->block[4], ptr, wrap_c);
  2903. ptr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2904. if(emu){
  2905. 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);
  2906. ptr= s->edge_emu_buffer;
  2907. }
  2908. s->dsp.get_pixels(s->block[5], ptr, wrap_c);
  2909. }
  2910. }else{
  2911. op_pixels_func (*op_pix)[4];
  2912. qpel_mc_func (*op_qpix)[16];
  2913. uint8_t *dest_y, *dest_cb, *dest_cr;
  2914. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  2915. int wrap_y, wrap_c;
  2916. int emu=0;
  2917. dest_y = s->dest[0];
  2918. dest_cb = s->dest[1];
  2919. dest_cr = s->dest[2];
  2920. wrap_y = s->linesize;
  2921. wrap_c = s->uvlinesize;
  2922. ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
  2923. ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2924. ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
  2925. if ((!s->no_rounding) || s->pict_type==B_TYPE){
  2926. op_pix = s->dsp.put_pixels_tab;
  2927. op_qpix= s->dsp.put_qpel_pixels_tab;
  2928. }else{
  2929. op_pix = s->dsp.put_no_rnd_pixels_tab;
  2930. op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
  2931. }
  2932. if (s->mv_dir & MV_DIR_FORWARD) {
  2933. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
  2934. op_pix = s->dsp.avg_pixels_tab;
  2935. op_qpix= s->dsp.avg_qpel_pixels_tab;
  2936. }
  2937. if (s->mv_dir & MV_DIR_BACKWARD) {
  2938. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
  2939. }
  2940. if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
  2941. ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height);
  2942. ptr_y= s->edge_emu_buffer;
  2943. emu=1;
  2944. }
  2945. if(s->flags&CODEC_FLAG_INTERLACED_DCT){
  2946. int progressive_score, interlaced_score;
  2947. s->interlaced_dct=0;
  2948. progressive_score= s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y, 8)
  2949. +s->dsp.ildct_cmp[0](s, dest_y + wrap_y*8, ptr_y + wrap_y*8, wrap_y, 8) - 400;
  2950. if(s->avctx->ildct_cmp == FF_CMP_VSSE) progressive_score -= 400;
  2951. if(progressive_score>0){
  2952. interlaced_score = s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y*2, 8)
  2953. +s->dsp.ildct_cmp[0](s, dest_y + wrap_y , ptr_y + wrap_y , wrap_y*2, 8);
  2954. if(progressive_score > interlaced_score){
  2955. s->interlaced_dct=1;
  2956. dct_offset= wrap_y;
  2957. wrap_y<<=1;
  2958. }
  2959. }
  2960. }
  2961. s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
  2962. s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
  2963. s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
  2964. s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
  2965. if(s->flags&CODEC_FLAG_GRAY){
  2966. skip_dct[4]= 1;
  2967. skip_dct[5]= 1;
  2968. }else{
  2969. if(emu){
  2970. 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);
  2971. ptr_cb= s->edge_emu_buffer;
  2972. }
  2973. s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
  2974. if(emu){
  2975. 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);
  2976. ptr_cr= s->edge_emu_buffer;
  2977. }
  2978. s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
  2979. }
  2980. /* pre quantization */
  2981. if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){
  2982. //FIXME optimize
  2983. if(s->dsp.sad[1](NULL, ptr_y , dest_y , wrap_y, 8) < 20*s->qscale) skip_dct[0]= 1;
  2984. if(s->dsp.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20*s->qscale) skip_dct[1]= 1;
  2985. if(s->dsp.sad[1](NULL, ptr_y +dct_offset , dest_y +dct_offset , wrap_y, 8) < 20*s->qscale) skip_dct[2]= 1;
  2986. 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;
  2987. if(s->dsp.sad[1](NULL, ptr_cb , dest_cb , wrap_c, 8) < 20*s->qscale) skip_dct[4]= 1;
  2988. if(s->dsp.sad[1](NULL, ptr_cr , dest_cr , wrap_c, 8) < 20*s->qscale) skip_dct[5]= 1;
  2989. #if 0
  2990. {
  2991. static int stat[7];
  2992. int num=0;
  2993. for(i=0; i<6; i++)
  2994. if(skip_dct[i]) num++;
  2995. stat[num]++;
  2996. if(s->mb_x==0 && s->mb_y==0){
  2997. for(i=0; i<7; i++){
  2998. printf("%6d %1d\n", stat[i], i);
  2999. }
  3000. }
  3001. }
  3002. #endif
  3003. }
  3004. }
  3005. /* DCT & quantize */
  3006. if(s->out_format==FMT_MJPEG){
  3007. for(i=0;i<6;i++) {
  3008. int overflow;
  3009. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow);
  3010. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  3011. }
  3012. }else{
  3013. for(i=0;i<6;i++) {
  3014. if(!skip_dct[i]){
  3015. int overflow;
  3016. s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
  3017. // FIXME we could decide to change to quantizer instead of clipping
  3018. // JS: I don't think that would be a good idea it could lower quality instead
  3019. // of improve it. Just INTRADC clipping deserves changes in quantizer
  3020. if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
  3021. }else
  3022. s->block_last_index[i]= -1;
  3023. }
  3024. if(s->luma_elim_threshold && !s->mb_intra)
  3025. for(i=0; i<4; i++)
  3026. dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
  3027. if(s->chroma_elim_threshold && !s->mb_intra)
  3028. for(i=4; i<6; i++)
  3029. dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
  3030. if(s->flags & CODEC_FLAG_CBP_RD){
  3031. for(i=0;i<6;i++) {
  3032. if(s->block_last_index[i] == -1)
  3033. s->coded_score[i]= INT_MAX/256;
  3034. }
  3035. }
  3036. }
  3037. if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
  3038. s->block_last_index[4]=
  3039. s->block_last_index[5]= 0;
  3040. s->block[4][0]=
  3041. s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
  3042. }
  3043. //non c quantize code returns incorrect block_last_index FIXME
  3044. if(s->alternate_scan && s->dct_quantize != dct_quantize_c){
  3045. for(i=0; i<6; i++){
  3046. int j;
  3047. if(s->block_last_index[i]>0){
  3048. for(j=63; j>0; j--){
  3049. if(s->block[i][ s->intra_scantable.permutated[j] ]) break;
  3050. }
  3051. s->block_last_index[i]= j;
  3052. }
  3053. }
  3054. }
  3055. /* huffman encode */
  3056. switch(s->codec_id){ //FIXME funct ptr could be slightly faster
  3057. case CODEC_ID_MPEG1VIDEO:
  3058. case CODEC_ID_MPEG2VIDEO:
  3059. mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
  3060. #ifdef CONFIG_RISKY
  3061. case CODEC_ID_MPEG4:
  3062. mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  3063. case CODEC_ID_MSMPEG4V2:
  3064. case CODEC_ID_MSMPEG4V3:
  3065. case CODEC_ID_WMV1:
  3066. msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
  3067. case CODEC_ID_WMV2:
  3068. ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
  3069. case CODEC_ID_H263:
  3070. case CODEC_ID_H263P:
  3071. case CODEC_ID_FLV1:
  3072. case CODEC_ID_RV10:
  3073. h263_encode_mb(s, s->block, motion_x, motion_y); break;
  3074. #endif
  3075. case CODEC_ID_MJPEG:
  3076. mjpeg_encode_mb(s, s->block); break;
  3077. default:
  3078. assert(0);
  3079. }
  3080. }
  3081. #endif //CONFIG_ENCODERS
  3082. /**
  3083. * combines the (truncated) bitstream to a complete frame
  3084. * @returns -1 if no complete frame could be created
  3085. */
  3086. int ff_combine_frame( MpegEncContext *s, int next, uint8_t **buf, int *buf_size){
  3087. ParseContext *pc= &s->parse_context;
  3088. #if 0
  3089. if(pc->overread){
  3090. printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
  3091. printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
  3092. }
  3093. #endif
  3094. /* copy overreaded byes from last frame into buffer */
  3095. for(; pc->overread>0; pc->overread--){
  3096. pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
  3097. }
  3098. pc->last_index= pc->index;
  3099. /* copy into buffer end return */
  3100. if(next == END_NOT_FOUND){
  3101. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  3102. memcpy(&pc->buffer[pc->index], *buf, *buf_size);
  3103. pc->index += *buf_size;
  3104. return -1;
  3105. }
  3106. *buf_size=
  3107. pc->overread_index= pc->index + next;
  3108. /* append to buffer */
  3109. if(pc->index){
  3110. pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
  3111. memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
  3112. pc->index = 0;
  3113. *buf= pc->buffer;
  3114. }
  3115. /* store overread bytes */
  3116. for(;next < 0; next++){
  3117. pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
  3118. pc->overread++;
  3119. }
  3120. #if 0
  3121. if(pc->overread){
  3122. printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
  3123. printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
  3124. }
  3125. #endif
  3126. return 0;
  3127. }
  3128. void ff_mpeg_flush(AVCodecContext *avctx){
  3129. int i;
  3130. MpegEncContext *s = avctx->priv_data;
  3131. if(s==NULL || s->picture==NULL)
  3132. return;
  3133. for(i=0; i<MAX_PICTURE_COUNT; i++){
  3134. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  3135. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  3136. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  3137. }
  3138. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  3139. s->parse_context.state= -1;
  3140. s->parse_context.frame_start_found= 0;
  3141. s->parse_context.overread= 0;
  3142. s->parse_context.overread_index= 0;
  3143. s->parse_context.index= 0;
  3144. s->parse_context.last_index= 0;
  3145. }
  3146. #ifdef CONFIG_ENCODERS
  3147. void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
  3148. {
  3149. int bytes= length>>4;
  3150. int bits= length&15;
  3151. int i;
  3152. if(length==0) return;
  3153. for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
  3154. put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
  3155. }
  3156. static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
  3157. int i;
  3158. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  3159. /* mpeg1 */
  3160. d->mb_skip_run= s->mb_skip_run;
  3161. for(i=0; i<3; i++)
  3162. d->last_dc[i]= s->last_dc[i];
  3163. /* statistics */
  3164. d->mv_bits= s->mv_bits;
  3165. d->i_tex_bits= s->i_tex_bits;
  3166. d->p_tex_bits= s->p_tex_bits;
  3167. d->i_count= s->i_count;
  3168. d->f_count= s->f_count;
  3169. d->b_count= s->b_count;
  3170. d->skip_count= s->skip_count;
  3171. d->misc_bits= s->misc_bits;
  3172. d->last_bits= 0;
  3173. d->mb_skiped= 0;
  3174. d->qscale= s->qscale;
  3175. d->dquant= s->dquant;
  3176. }
  3177. static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
  3178. int i;
  3179. memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
  3180. memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
  3181. /* mpeg1 */
  3182. d->mb_skip_run= s->mb_skip_run;
  3183. for(i=0; i<3; i++)
  3184. d->last_dc[i]= s->last_dc[i];
  3185. /* statistics */
  3186. d->mv_bits= s->mv_bits;
  3187. d->i_tex_bits= s->i_tex_bits;
  3188. d->p_tex_bits= s->p_tex_bits;
  3189. d->i_count= s->i_count;
  3190. d->f_count= s->f_count;
  3191. d->b_count= s->b_count;
  3192. d->skip_count= s->skip_count;
  3193. d->misc_bits= s->misc_bits;
  3194. d->mb_intra= s->mb_intra;
  3195. d->mb_skiped= s->mb_skiped;
  3196. d->mv_type= s->mv_type;
  3197. d->mv_dir= s->mv_dir;
  3198. d->pb= s->pb;
  3199. if(s->data_partitioning){
  3200. d->pb2= s->pb2;
  3201. d->tex_pb= s->tex_pb;
  3202. }
  3203. d->block= s->block;
  3204. for(i=0; i<6; i++)
  3205. d->block_last_index[i]= s->block_last_index[i];
  3206. d->interlaced_dct= s->interlaced_dct;
  3207. d->qscale= s->qscale;
  3208. }
  3209. static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
  3210. PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
  3211. int *dmin, int *next_block, int motion_x, int motion_y)
  3212. {
  3213. int score;
  3214. uint8_t *dest_backup[3];
  3215. copy_context_before_encode(s, backup, type);
  3216. s->block= s->blocks[*next_block];
  3217. s->pb= pb[*next_block];
  3218. if(s->data_partitioning){
  3219. s->pb2 = pb2 [*next_block];
  3220. s->tex_pb= tex_pb[*next_block];
  3221. }
  3222. if(*next_block){
  3223. memcpy(dest_backup, s->dest, sizeof(s->dest));
  3224. s->dest[0] = s->me.scratchpad;
  3225. s->dest[1] = s->me.scratchpad + 16;
  3226. s->dest[2] = s->me.scratchpad + 16 + 8;
  3227. assert(2*s->uvlinesize == s->linesize); //should be no prob for encoding
  3228. assert(s->linesize >= 64); //FIXME
  3229. }
  3230. encode_mb(s, motion_x, motion_y);
  3231. score= get_bit_count(&s->pb);
  3232. if(s->data_partitioning){
  3233. score+= get_bit_count(&s->pb2);
  3234. score+= get_bit_count(&s->tex_pb);
  3235. }
  3236. if(s->avctx->mb_decision == FF_MB_DECISION_RD){
  3237. MPV_decode_mb(s, s->block);
  3238. score *= s->lambda2;
  3239. score += sse_mb(s) << FF_LAMBDA_SHIFT;
  3240. }
  3241. if(*next_block){
  3242. memcpy(s->dest, dest_backup, sizeof(s->dest));
  3243. }
  3244. if(score<*dmin){
  3245. *dmin= score;
  3246. *next_block^=1;
  3247. copy_context_after_encode(best, s, type);
  3248. }
  3249. }
  3250. static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
  3251. uint32_t *sq = squareTbl + 256;
  3252. int acc=0;
  3253. int x,y;
  3254. if(w==16 && h==16)
  3255. return s->dsp.sse[0](NULL, src1, src2, stride, 16);
  3256. else if(w==8 && h==8)
  3257. return s->dsp.sse[1](NULL, src1, src2, stride, 8);
  3258. for(y=0; y<h; y++){
  3259. for(x=0; x<w; x++){
  3260. acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
  3261. }
  3262. }
  3263. assert(acc>=0);
  3264. return acc;
  3265. }
  3266. static int sse_mb(MpegEncContext *s){
  3267. int w= 16;
  3268. int h= 16;
  3269. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  3270. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  3271. if(w==16 && h==16)
  3272. 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)
  3273. +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)
  3274. +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);
  3275. else
  3276. 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)
  3277. +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)
  3278. +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);
  3279. }
  3280. static void encode_picture(MpegEncContext *s, int picture_number)
  3281. {
  3282. int mb_x, mb_y, pdif = 0;
  3283. int i, j;
  3284. int bits;
  3285. MpegEncContext best_s, backup_s;
  3286. uint8_t bit_buf[2][3000];
  3287. uint8_t bit_buf2[2][3000];
  3288. uint8_t bit_buf_tex[2][3000];
  3289. PutBitContext pb[2], pb2[2], tex_pb[2];
  3290. for(i=0; i<2; i++){
  3291. init_put_bits(&pb [i], bit_buf [i], 3000);
  3292. init_put_bits(&pb2 [i], bit_buf2 [i], 3000);
  3293. init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000);
  3294. }
  3295. s->picture_number = picture_number;
  3296. /* Reset the average MB variance */
  3297. s->current_picture.mb_var_sum = 0;
  3298. s->current_picture.mc_mb_var_sum = 0;
  3299. #ifdef CONFIG_RISKY
  3300. /* we need to initialize some time vars before we can encode b-frames */
  3301. // RAL: Condition added for MPEG1VIDEO
  3302. if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->h263_msmpeg4))
  3303. ff_set_mpeg4_time(s, s->picture_number);
  3304. #endif
  3305. s->scene_change_score=0;
  3306. s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME ratedistoration
  3307. if(s->pict_type==I_TYPE){
  3308. if(s->msmpeg4_version >= 3) s->no_rounding=1;
  3309. else s->no_rounding=0;
  3310. }else if(s->pict_type!=B_TYPE){
  3311. if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
  3312. s->no_rounding ^= 1;
  3313. }
  3314. /* Estimate motion for every MB */
  3315. s->mb_intra=0; //for the rate distoration & bit compare functions
  3316. if(s->pict_type != I_TYPE){
  3317. if(s->pict_type != B_TYPE){
  3318. if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
  3319. s->me.pre_pass=1;
  3320. s->me.dia_size= s->avctx->pre_dia_size;
  3321. for(mb_y=s->mb_height-1; mb_y >=0 ; mb_y--) {
  3322. s->mb_y = mb_y;
  3323. for(mb_x=s->mb_width-1; mb_x >=0 ; mb_x--) {
  3324. s->mb_x = mb_x;
  3325. ff_pre_estimate_p_frame_motion(s, mb_x, mb_y);
  3326. }
  3327. }
  3328. s->me.pre_pass=0;
  3329. }
  3330. }
  3331. s->me.dia_size= s->avctx->dia_size;
  3332. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  3333. s->mb_y = mb_y;
  3334. s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
  3335. s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
  3336. s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1;
  3337. s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2);
  3338. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  3339. s->mb_x = mb_x;
  3340. s->block_index[0]+=2;
  3341. s->block_index[1]+=2;
  3342. s->block_index[2]+=2;
  3343. s->block_index[3]+=2;
  3344. /* compute motion vector & mb_type and store in context */
  3345. if(s->pict_type==B_TYPE)
  3346. ff_estimate_b_frame_motion(s, mb_x, mb_y);
  3347. else
  3348. ff_estimate_p_frame_motion(s, mb_x, mb_y);
  3349. }
  3350. }
  3351. }else /* if(s->pict_type == I_TYPE) */{
  3352. /* I-Frame */
  3353. for(i=0; i<s->mb_stride*s->mb_height; i++)
  3354. s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  3355. if(!s->fixed_qscale){
  3356. /* finding spatial complexity for I-frame rate control */
  3357. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  3358. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  3359. int xx = mb_x * 16;
  3360. int yy = mb_y * 16;
  3361. uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  3362. int varc;
  3363. int sum = s->dsp.pix_sum(pix, s->linesize);
  3364. varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
  3365. s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
  3366. s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  3367. s->current_picture.mb_var_sum += varc;
  3368. }
  3369. }
  3370. }
  3371. }
  3372. emms_c();
  3373. if(s->scene_change_score > s->avctx->scenechange_threshold && s->pict_type == P_TYPE){
  3374. s->pict_type= I_TYPE;
  3375. for(i=0; i<s->mb_stride*s->mb_height; i++)
  3376. s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  3377. //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
  3378. }
  3379. if(!s->umvplus){
  3380. if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
  3381. s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
  3382. if(s->flags & CODEC_FLAG_INTERLACED_ME){
  3383. int a,b;
  3384. a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
  3385. b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
  3386. s->f_code= FFMAX(s->f_code, FFMAX(a,b));
  3387. }
  3388. ff_fix_long_p_mvs(s);
  3389. ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
  3390. if(s->flags & CODEC_FLAG_INTERLACED_ME){
  3391. for(i=0; i<2; i++){
  3392. for(j=0; j<2; j++)
  3393. ff_fix_long_mvs(s, s->p_field_select_table[i], j,
  3394. s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
  3395. }
  3396. }
  3397. }
  3398. if(s->pict_type==B_TYPE){
  3399. int a, b;
  3400. a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
  3401. b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
  3402. s->f_code = FFMAX(a, b);
  3403. a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
  3404. b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
  3405. s->b_code = FFMAX(a, b);
  3406. ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
  3407. ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
  3408. ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
  3409. ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
  3410. if(s->flags & CODEC_FLAG_INTERLACED_ME){
  3411. int dir;
  3412. for(dir=0; dir<2; dir++){
  3413. for(i=0; i<2; i++){
  3414. for(j=0; j<2; j++){
  3415. int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
  3416. : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
  3417. ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
  3418. s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
  3419. }
  3420. }
  3421. }
  3422. }
  3423. }
  3424. }
  3425. if (!s->fixed_qscale)
  3426. s->current_picture.quality = ff_rate_estimate_qscale(s);
  3427. if(s->adaptive_quant){
  3428. #ifdef CONFIG_RISKY
  3429. switch(s->codec_id){
  3430. case CODEC_ID_MPEG4:
  3431. ff_clean_mpeg4_qscales(s);
  3432. break;
  3433. case CODEC_ID_H263:
  3434. case CODEC_ID_H263P:
  3435. case CODEC_ID_FLV1:
  3436. ff_clean_h263_qscales(s);
  3437. break;
  3438. }
  3439. #endif
  3440. s->lambda= s->lambda_table[0];
  3441. //FIXME broken
  3442. }else
  3443. s->lambda= s->current_picture.quality;
  3444. //printf("%d %d\n", s->avctx->global_quality, s->current_picture.quality);
  3445. update_qscale(s);
  3446. if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==I_TYPE && !(s->flags & CODEC_FLAG_QSCALE))
  3447. s->qscale= 3; //reduce cliping problems
  3448. if (s->out_format == FMT_MJPEG) {
  3449. /* for mjpeg, we do include qscale in the matrix */
  3450. s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
  3451. for(i=1;i<64;i++){
  3452. int j= s->dsp.idct_permutation[i];
  3453. s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
  3454. }
  3455. convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
  3456. s->intra_matrix, s->intra_quant_bias, 8, 8);
  3457. }
  3458. //FIXME var duplication
  3459. s->current_picture.key_frame= s->pict_type == I_TYPE;
  3460. s->current_picture.pict_type= s->pict_type;
  3461. if(s->current_picture.key_frame)
  3462. s->picture_in_gop_number=0;
  3463. s->last_bits= get_bit_count(&s->pb);
  3464. switch(s->out_format) {
  3465. case FMT_MJPEG:
  3466. mjpeg_picture_header(s);
  3467. break;
  3468. #ifdef CONFIG_RISKY
  3469. case FMT_H263:
  3470. if (s->codec_id == CODEC_ID_WMV2)
  3471. ff_wmv2_encode_picture_header(s, picture_number);
  3472. else if (s->h263_msmpeg4)
  3473. msmpeg4_encode_picture_header(s, picture_number);
  3474. else if (s->h263_pred)
  3475. mpeg4_encode_picture_header(s, picture_number);
  3476. else if (s->codec_id == CODEC_ID_RV10)
  3477. rv10_encode_picture_header(s, picture_number);
  3478. else if (s->codec_id == CODEC_ID_FLV1)
  3479. ff_flv_encode_picture_header(s, picture_number);
  3480. else
  3481. h263_encode_picture_header(s, picture_number);
  3482. break;
  3483. #endif
  3484. case FMT_MPEG1:
  3485. mpeg1_encode_picture_header(s, picture_number);
  3486. break;
  3487. case FMT_H264:
  3488. break;
  3489. default:
  3490. assert(0);
  3491. }
  3492. bits= get_bit_count(&s->pb);
  3493. s->header_bits= bits - s->last_bits;
  3494. s->last_bits= bits;
  3495. s->mv_bits=0;
  3496. s->misc_bits=0;
  3497. s->i_tex_bits=0;
  3498. s->p_tex_bits=0;
  3499. s->i_count=0;
  3500. s->f_count=0;
  3501. s->b_count=0;
  3502. s->skip_count=0;
  3503. for(i=0; i<3; i++){
  3504. /* init last dc values */
  3505. /* note: quant matrix value (8) is implied here */
  3506. s->last_dc[i] = 128;
  3507. s->current_picture_ptr->error[i] = 0;
  3508. }
  3509. s->mb_skip_run = 0;
  3510. memset(s->last_mv, 0, sizeof(s->last_mv));
  3511. s->last_mv_dir = 0;
  3512. #ifdef CONFIG_RISKY
  3513. switch(s->codec_id){
  3514. case CODEC_ID_H263:
  3515. case CODEC_ID_H263P:
  3516. case CODEC_ID_FLV1:
  3517. s->gob_index = ff_h263_get_gob_height(s);
  3518. break;
  3519. case CODEC_ID_MPEG4:
  3520. if(s->partitioned_frame)
  3521. ff_mpeg4_init_partitions(s);
  3522. break;
  3523. }
  3524. #endif
  3525. s->resync_mb_x=0;
  3526. s->resync_mb_y=0;
  3527. s->first_slice_line = 1;
  3528. s->ptr_lastgob = s->pb.buf;
  3529. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  3530. s->mb_x=0;
  3531. s->mb_y= mb_y;
  3532. ff_set_qscale(s, s->qscale);
  3533. ff_init_block_index(s);
  3534. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  3535. const int xy= mb_y*s->mb_stride + mb_x;
  3536. int mb_type= s->mb_type[xy];
  3537. // int d;
  3538. int dmin= INT_MAX;
  3539. int dir;
  3540. s->mb_x = mb_x;
  3541. ff_update_block_index(s);
  3542. /* write gob / video packet header */
  3543. #ifdef CONFIG_RISKY
  3544. if(s->rtp_mode){
  3545. int current_packet_size, is_gob_start;
  3546. current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
  3547. is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
  3548. switch(s->codec_id){
  3549. case CODEC_ID_H263:
  3550. case CODEC_ID_H263P:
  3551. if(!s->h263_slice_structured)
  3552. if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
  3553. break;
  3554. case CODEC_ID_MPEG2VIDEO:
  3555. if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
  3556. case CODEC_ID_MPEG1VIDEO:
  3557. if(s->mb_skip_run) is_gob_start=0;
  3558. break;
  3559. }
  3560. if(is_gob_start){
  3561. if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame){
  3562. ff_mpeg4_merge_partitions(s);
  3563. ff_mpeg4_init_partitions(s);
  3564. }
  3565. if(s->codec_id==CODEC_ID_MPEG4)
  3566. ff_mpeg4_stuffing(&s->pb);
  3567. align_put_bits(&s->pb);
  3568. flush_put_bits(&s->pb);
  3569. assert((get_bit_count(&s->pb)&7) == 0);
  3570. current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
  3571. if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
  3572. int r= get_bit_count(&s->pb)/8 + s->picture_number + s->codec_id + s->mb_x + s->mb_y;
  3573. int d= 100 / s->avctx->error_rate;
  3574. if(r % d == 0){
  3575. current_packet_size=0;
  3576. #ifndef ALT_BITSTREAM_WRITER
  3577. s->pb.buf_ptr= s->ptr_lastgob;
  3578. #endif
  3579. assert(pbBufPtr(&s->pb) == s->ptr_lastgob);
  3580. }
  3581. }
  3582. if (s->avctx->rtp_callback)
  3583. s->avctx->rtp_callback(s->ptr_lastgob, current_packet_size, 0);
  3584. switch(s->codec_id){
  3585. case CODEC_ID_MPEG4:
  3586. ff_mpeg4_encode_video_packet_header(s);
  3587. ff_mpeg4_clean_buffers(s);
  3588. break;
  3589. case CODEC_ID_MPEG1VIDEO:
  3590. case CODEC_ID_MPEG2VIDEO:
  3591. ff_mpeg1_encode_slice_header(s);
  3592. ff_mpeg1_clean_buffers(s);
  3593. break;
  3594. case CODEC_ID_H263:
  3595. case CODEC_ID_H263P:
  3596. h263_encode_gob_header(s, mb_y);
  3597. break;
  3598. }
  3599. if(s->flags&CODEC_FLAG_PASS1){
  3600. int bits= get_bit_count(&s->pb);
  3601. s->misc_bits+= bits - s->last_bits;
  3602. s->last_bits= bits;
  3603. }
  3604. s->ptr_lastgob += current_packet_size;
  3605. s->first_slice_line=1;
  3606. s->resync_mb_x=mb_x;
  3607. s->resync_mb_y=mb_y;
  3608. }
  3609. }
  3610. #endif
  3611. if( (s->resync_mb_x == s->mb_x)
  3612. && s->resync_mb_y+1 == s->mb_y){
  3613. s->first_slice_line=0;
  3614. }
  3615. s->mb_skiped=0;
  3616. s->dquant=0; //only for QP_RD
  3617. if(mb_type & (mb_type-1) || (s->flags & CODEC_FLAG_QP_RD)){ // more than 1 MB type possible
  3618. int next_block=0;
  3619. int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
  3620. copy_context_before_encode(&backup_s, s, -1);
  3621. backup_s.pb= s->pb;
  3622. best_s.data_partitioning= s->data_partitioning;
  3623. best_s.partitioned_frame= s->partitioned_frame;
  3624. if(s->data_partitioning){
  3625. backup_s.pb2= s->pb2;
  3626. backup_s.tex_pb= s->tex_pb;
  3627. }
  3628. if(mb_type&CANDIDATE_MB_TYPE_INTER){
  3629. s->mv_dir = MV_DIR_FORWARD;
  3630. s->mv_type = MV_TYPE_16X16;
  3631. s->mb_intra= 0;
  3632. s->mv[0][0][0] = s->p_mv_table[xy][0];
  3633. s->mv[0][0][1] = s->p_mv_table[xy][1];
  3634. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
  3635. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  3636. }
  3637. if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
  3638. s->mv_dir = MV_DIR_FORWARD;
  3639. s->mv_type = MV_TYPE_FIELD;
  3640. s->mb_intra= 0;
  3641. for(i=0; i<2; i++){
  3642. j= s->field_select[0][i] = s->p_field_select_table[i][xy];
  3643. s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
  3644. s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
  3645. }
  3646. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
  3647. &dmin, &next_block, 0, 0);
  3648. }
  3649. if(mb_type&CANDIDATE_MB_TYPE_SKIPED){
  3650. s->mv_dir = MV_DIR_FORWARD;
  3651. s->mv_type = MV_TYPE_16X16;
  3652. s->mb_intra= 0;
  3653. s->mv[0][0][0] = 0;
  3654. s->mv[0][0][1] = 0;
  3655. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPED, pb, pb2, tex_pb,
  3656. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  3657. }
  3658. if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
  3659. s->mv_dir = MV_DIR_FORWARD;
  3660. s->mv_type = MV_TYPE_8X8;
  3661. s->mb_intra= 0;
  3662. for(i=0; i<4; i++){
  3663. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
  3664. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
  3665. }
  3666. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
  3667. &dmin, &next_block, 0, 0);
  3668. }
  3669. if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
  3670. s->mv_dir = MV_DIR_FORWARD;
  3671. s->mv_type = MV_TYPE_16X16;
  3672. s->mb_intra= 0;
  3673. s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  3674. s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  3675. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
  3676. &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
  3677. }
  3678. if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
  3679. s->mv_dir = MV_DIR_BACKWARD;
  3680. s->mv_type = MV_TYPE_16X16;
  3681. s->mb_intra= 0;
  3682. s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  3683. s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  3684. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
  3685. &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
  3686. }
  3687. if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
  3688. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  3689. s->mv_type = MV_TYPE_16X16;
  3690. s->mb_intra= 0;
  3691. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  3692. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  3693. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  3694. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  3695. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
  3696. &dmin, &next_block, 0, 0);
  3697. }
  3698. if(mb_type&CANDIDATE_MB_TYPE_DIRECT){
  3699. int mx= s->b_direct_mv_table[xy][0];
  3700. int my= s->b_direct_mv_table[xy][1];
  3701. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  3702. s->mb_intra= 0;
  3703. #ifdef CONFIG_RISKY
  3704. ff_mpeg4_set_direct_mv(s, mx, my);
  3705. #endif
  3706. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
  3707. &dmin, &next_block, mx, my);
  3708. }
  3709. if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
  3710. s->mv_dir = MV_DIR_FORWARD;
  3711. s->mv_type = MV_TYPE_FIELD;
  3712. s->mb_intra= 0;
  3713. for(i=0; i<2; i++){
  3714. j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
  3715. s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
  3716. s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
  3717. }
  3718. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
  3719. &dmin, &next_block, 0, 0);
  3720. }
  3721. if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
  3722. s->mv_dir = MV_DIR_BACKWARD;
  3723. s->mv_type = MV_TYPE_FIELD;
  3724. s->mb_intra= 0;
  3725. for(i=0; i<2; i++){
  3726. j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
  3727. s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
  3728. s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
  3729. }
  3730. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
  3731. &dmin, &next_block, 0, 0);
  3732. }
  3733. if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
  3734. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  3735. s->mv_type = MV_TYPE_FIELD;
  3736. s->mb_intra= 0;
  3737. for(dir=0; dir<2; dir++){
  3738. for(i=0; i<2; i++){
  3739. j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
  3740. s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
  3741. s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
  3742. }
  3743. }
  3744. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
  3745. &dmin, &next_block, 0, 0);
  3746. }
  3747. if(mb_type&CANDIDATE_MB_TYPE_INTRA){
  3748. s->mv_dir = 0;
  3749. s->mv_type = MV_TYPE_16X16;
  3750. s->mb_intra= 1;
  3751. s->mv[0][0][0] = 0;
  3752. s->mv[0][0][1] = 0;
  3753. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
  3754. &dmin, &next_block, 0, 0);
  3755. if(s->h263_pred || s->h263_aic){
  3756. if(best_s.mb_intra)
  3757. s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
  3758. else
  3759. ff_clean_intra_table_entries(s); //old mode?
  3760. }
  3761. }
  3762. if(s->flags & CODEC_FLAG_QP_RD){
  3763. if(best_s.mv_type==MV_TYPE_16X16 && !(best_s.mv_dir&MV_DIRECT)){
  3764. const int last_qp= backup_s.qscale;
  3765. int dquant, dir, qp, dc[6];
  3766. DCTELEM ac[6][16];
  3767. const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
  3768. assert(backup_s.dquant == 0);
  3769. //FIXME intra
  3770. s->mv_dir= best_s.mv_dir;
  3771. s->mv_type = MV_TYPE_16X16;
  3772. s->mb_intra= best_s.mb_intra;
  3773. s->mv[0][0][0] = best_s.mv[0][0][0];
  3774. s->mv[0][0][1] = best_s.mv[0][0][1];
  3775. s->mv[1][0][0] = best_s.mv[1][0][0];
  3776. s->mv[1][0][1] = best_s.mv[1][0][1];
  3777. dir= s->pict_type == B_TYPE ? 2 : 1;
  3778. if(last_qp + dir > s->avctx->qmax) dir= -dir;
  3779. for(dquant= dir; dquant<=2 && dquant>=-2; dquant += dir){
  3780. qp= last_qp + dquant;
  3781. if(qp < s->avctx->qmin || qp > s->avctx->qmax)
  3782. break;
  3783. backup_s.dquant= dquant;
  3784. if(s->mb_intra){
  3785. for(i=0; i<6; i++){
  3786. dc[i]= s->dc_val[0][ s->block_index[i] ];
  3787. memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(DCTELEM)*16);
  3788. }
  3789. }
  3790. encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
  3791. &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
  3792. if(best_s.qscale != qp){
  3793. if(s->mb_intra){
  3794. for(i=0; i<6; i++){
  3795. s->dc_val[0][ s->block_index[i] ]= dc[i];
  3796. memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(DCTELEM)*16);
  3797. }
  3798. }
  3799. if(dir > 0 && dquant==dir){
  3800. dquant= 0;
  3801. dir= -dir;
  3802. }else
  3803. break;
  3804. }
  3805. }
  3806. qp= best_s.qscale;
  3807. s->current_picture.qscale_table[xy]= qp;
  3808. }
  3809. }
  3810. copy_context_after_encode(s, &best_s, -1);
  3811. pb_bits_count= get_bit_count(&s->pb);
  3812. flush_put_bits(&s->pb);
  3813. ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
  3814. s->pb= backup_s.pb;
  3815. if(s->data_partitioning){
  3816. pb2_bits_count= get_bit_count(&s->pb2);
  3817. flush_put_bits(&s->pb2);
  3818. ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
  3819. s->pb2= backup_s.pb2;
  3820. tex_pb_bits_count= get_bit_count(&s->tex_pb);
  3821. flush_put_bits(&s->tex_pb);
  3822. ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
  3823. s->tex_pb= backup_s.tex_pb;
  3824. }
  3825. s->last_bits= get_bit_count(&s->pb);
  3826. #ifdef CONFIG_RISKY
  3827. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
  3828. ff_h263_update_motion_val(s);
  3829. #endif
  3830. if(next_block==0){
  3831. s->dsp.put_pixels_tab[0][0](s->dest[0], s->me.scratchpad , s->linesize ,16);
  3832. s->dsp.put_pixels_tab[1][0](s->dest[1], s->me.scratchpad + 16, s->uvlinesize, 8);
  3833. s->dsp.put_pixels_tab[1][0](s->dest[2], s->me.scratchpad + 24, s->uvlinesize, 8);
  3834. }
  3835. if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
  3836. MPV_decode_mb(s, s->block);
  3837. } else {
  3838. int motion_x, motion_y;
  3839. s->mv_type=MV_TYPE_16X16;
  3840. // only one MB-Type possible
  3841. switch(mb_type){
  3842. case CANDIDATE_MB_TYPE_INTRA:
  3843. s->mv_dir = 0;
  3844. s->mb_intra= 1;
  3845. motion_x= s->mv[0][0][0] = 0;
  3846. motion_y= s->mv[0][0][1] = 0;
  3847. break;
  3848. case CANDIDATE_MB_TYPE_INTER:
  3849. s->mv_dir = MV_DIR_FORWARD;
  3850. s->mb_intra= 0;
  3851. motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
  3852. motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
  3853. break;
  3854. case CANDIDATE_MB_TYPE_INTER_I:
  3855. s->mv_dir = MV_DIR_FORWARD;
  3856. s->mv_type = MV_TYPE_FIELD;
  3857. s->mb_intra= 0;
  3858. for(i=0; i<2; i++){
  3859. j= s->field_select[0][i] = s->p_field_select_table[i][xy];
  3860. s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
  3861. s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
  3862. }
  3863. motion_x = motion_y = 0;
  3864. break;
  3865. case CANDIDATE_MB_TYPE_INTER4V:
  3866. s->mv_dir = MV_DIR_FORWARD;
  3867. s->mv_type = MV_TYPE_8X8;
  3868. s->mb_intra= 0;
  3869. for(i=0; i<4; i++){
  3870. s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
  3871. s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
  3872. }
  3873. motion_x= motion_y= 0;
  3874. break;
  3875. case CANDIDATE_MB_TYPE_DIRECT:
  3876. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
  3877. s->mb_intra= 0;
  3878. motion_x=s->b_direct_mv_table[xy][0];
  3879. motion_y=s->b_direct_mv_table[xy][1];
  3880. #ifdef CONFIG_RISKY
  3881. ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
  3882. #endif
  3883. break;
  3884. case CANDIDATE_MB_TYPE_BIDIR:
  3885. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  3886. s->mb_intra= 0;
  3887. motion_x=0;
  3888. motion_y=0;
  3889. s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
  3890. s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
  3891. s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
  3892. s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
  3893. break;
  3894. case CANDIDATE_MB_TYPE_BACKWARD:
  3895. s->mv_dir = MV_DIR_BACKWARD;
  3896. s->mb_intra= 0;
  3897. motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
  3898. motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
  3899. break;
  3900. case CANDIDATE_MB_TYPE_FORWARD:
  3901. s->mv_dir = MV_DIR_FORWARD;
  3902. s->mb_intra= 0;
  3903. motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
  3904. motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
  3905. // printf(" %d %d ", motion_x, motion_y);
  3906. break;
  3907. case CANDIDATE_MB_TYPE_FORWARD_I:
  3908. s->mv_dir = MV_DIR_FORWARD;
  3909. s->mv_type = MV_TYPE_FIELD;
  3910. s->mb_intra= 0;
  3911. for(i=0; i<2; i++){
  3912. j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
  3913. s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
  3914. s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
  3915. }
  3916. motion_x=motion_y=0;
  3917. break;
  3918. case CANDIDATE_MB_TYPE_BACKWARD_I:
  3919. s->mv_dir = MV_DIR_BACKWARD;
  3920. s->mv_type = MV_TYPE_FIELD;
  3921. s->mb_intra= 0;
  3922. for(i=0; i<2; i++){
  3923. j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
  3924. s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
  3925. s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
  3926. }
  3927. motion_x=motion_y=0;
  3928. break;
  3929. case CANDIDATE_MB_TYPE_BIDIR_I:
  3930. s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
  3931. s->mv_type = MV_TYPE_FIELD;
  3932. s->mb_intra= 0;
  3933. for(dir=0; dir<2; dir++){
  3934. for(i=0; i<2; i++){
  3935. j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
  3936. s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
  3937. s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
  3938. }
  3939. }
  3940. motion_x=motion_y=0;
  3941. break;
  3942. default:
  3943. motion_x=motion_y=0; //gcc warning fix
  3944. av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
  3945. }
  3946. encode_mb(s, motion_x, motion_y);
  3947. // RAL: Update last macrobloc type
  3948. s->last_mv_dir = s->mv_dir;
  3949. #ifdef CONFIG_RISKY
  3950. if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
  3951. ff_h263_update_motion_val(s);
  3952. #endif
  3953. MPV_decode_mb(s, s->block);
  3954. }
  3955. /* clean the MV table in IPS frames for direct mode in B frames */
  3956. if(s->mb_intra /* && I,P,S_TYPE */){
  3957. s->p_mv_table[xy][0]=0;
  3958. s->p_mv_table[xy][1]=0;
  3959. }
  3960. if(s->flags&CODEC_FLAG_PSNR){
  3961. int w= 16;
  3962. int h= 16;
  3963. if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
  3964. if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
  3965. s->current_picture_ptr->error[0] += sse(
  3966. s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
  3967. s->dest[0], w, h, s->linesize);
  3968. s->current_picture_ptr->error[1] += sse(
  3969. s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  3970. s->dest[1], w>>1, h>>1, s->uvlinesize);
  3971. s->current_picture_ptr->error[2] += sse(
  3972. s, s->new_picture .data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
  3973. s->dest[2], w>>1, h>>1, s->uvlinesize);
  3974. }
  3975. if(s->loop_filter)
  3976. ff_h263_loop_filter(s);
  3977. //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, get_bit_count(&s->pb));
  3978. }
  3979. }
  3980. emms_c();
  3981. #ifdef CONFIG_RISKY
  3982. if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame)
  3983. ff_mpeg4_merge_partitions(s);
  3984. if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
  3985. msmpeg4_encode_ext_header(s);
  3986. if(s->codec_id==CODEC_ID_MPEG4)
  3987. ff_mpeg4_stuffing(&s->pb);
  3988. #endif
  3989. /* Send the last GOB if RTP */
  3990. if (s->avctx->rtp_callback) {
  3991. flush_put_bits(&s->pb);
  3992. pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
  3993. /* Call the RTP callback to send the last GOB */
  3994. s->avctx->rtp_callback(s->ptr_lastgob, pdif, 0);
  3995. }
  3996. }
  3997. #endif //CONFIG_ENCODERS
  3998. static void denoise_dct_c(MpegEncContext *s, DCTELEM *block){
  3999. const int intra= s->mb_intra;
  4000. int i;
  4001. s->dct_count[intra]++;
  4002. for(i=0; i<64; i++){
  4003. int level= block[i];
  4004. if(level){
  4005. if(level>0){
  4006. s->dct_error_sum[intra][i] += level;
  4007. level -= s->dct_offset[intra][i];
  4008. if(level<0) level=0;
  4009. }else{
  4010. s->dct_error_sum[intra][i] -= level;
  4011. level += s->dct_offset[intra][i];
  4012. if(level>0) level=0;
  4013. }
  4014. block[i]= level;
  4015. }
  4016. }
  4017. }
  4018. #ifdef CONFIG_ENCODERS
  4019. static int dct_quantize_trellis_c(MpegEncContext *s,
  4020. DCTELEM *block, int n,
  4021. int qscale, int *overflow){
  4022. const int *qmat;
  4023. const uint8_t *scantable= s->intra_scantable.scantable;
  4024. const uint8_t *perm_scantable= s->intra_scantable.permutated;
  4025. int max=0;
  4026. unsigned int threshold1, threshold2;
  4027. int bias=0;
  4028. int run_tab[65];
  4029. int level_tab[65];
  4030. int score_tab[65];
  4031. int survivor[65];
  4032. int survivor_count;
  4033. int last_run=0;
  4034. int last_level=0;
  4035. int last_score= 0;
  4036. int last_i;
  4037. int coeff[2][64];
  4038. int coeff_count[64];
  4039. int qmul, qadd, start_i, last_non_zero, i, dc;
  4040. const int esc_length= s->ac_esc_length;
  4041. uint8_t * length;
  4042. uint8_t * last_length;
  4043. const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
  4044. s->dsp.fdct (block);
  4045. if(s->dct_error_sum)
  4046. s->denoise_dct(s, block);
  4047. qmul= qscale*16;
  4048. qadd= ((qscale-1)|1)*8;
  4049. if (s->mb_intra) {
  4050. int q;
  4051. if (!s->h263_aic) {
  4052. if (n < 4)
  4053. q = s->y_dc_scale;
  4054. else
  4055. q = s->c_dc_scale;
  4056. q = q << 3;
  4057. } else{
  4058. /* For AIC we skip quant/dequant of INTRADC */
  4059. q = 1 << 3;
  4060. qadd=0;
  4061. }
  4062. /* note: block[0] is assumed to be positive */
  4063. block[0] = (block[0] + (q >> 1)) / q;
  4064. start_i = 1;
  4065. last_non_zero = 0;
  4066. qmat = s->q_intra_matrix[qscale];
  4067. if(s->mpeg_quant || s->out_format == FMT_MPEG1)
  4068. bias= 1<<(QMAT_SHIFT-1);
  4069. length = s->intra_ac_vlc_length;
  4070. last_length= s->intra_ac_vlc_last_length;
  4071. } else {
  4072. start_i = 0;
  4073. last_non_zero = -1;
  4074. qmat = s->q_inter_matrix[qscale];
  4075. length = s->inter_ac_vlc_length;
  4076. last_length= s->inter_ac_vlc_last_length;
  4077. }
  4078. last_i= start_i;
  4079. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  4080. threshold2= (threshold1<<1);
  4081. for(i=63; i>=start_i; i--) {
  4082. const int j = scantable[i];
  4083. int level = block[j] * qmat[j];
  4084. if(((unsigned)(level+threshold1))>threshold2){
  4085. last_non_zero = i;
  4086. break;
  4087. }
  4088. }
  4089. for(i=start_i; i<=last_non_zero; i++) {
  4090. const int j = scantable[i];
  4091. int level = block[j] * qmat[j];
  4092. // if( bias+level >= (1<<(QMAT_SHIFT - 3))
  4093. // || bias-level >= (1<<(QMAT_SHIFT - 3))){
  4094. if(((unsigned)(level+threshold1))>threshold2){
  4095. if(level>0){
  4096. level= (bias + level)>>QMAT_SHIFT;
  4097. coeff[0][i]= level;
  4098. coeff[1][i]= level-1;
  4099. // coeff[2][k]= level-2;
  4100. }else{
  4101. level= (bias - level)>>QMAT_SHIFT;
  4102. coeff[0][i]= -level;
  4103. coeff[1][i]= -level+1;
  4104. // coeff[2][k]= -level+2;
  4105. }
  4106. coeff_count[i]= FFMIN(level, 2);
  4107. assert(coeff_count[i]);
  4108. max |=level;
  4109. }else{
  4110. coeff[0][i]= (level>>31)|1;
  4111. coeff_count[i]= 1;
  4112. }
  4113. }
  4114. *overflow= s->max_qcoeff < max; //overflow might have happend
  4115. if(last_non_zero < start_i){
  4116. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  4117. return last_non_zero;
  4118. }
  4119. score_tab[start_i]= 0;
  4120. survivor[0]= start_i;
  4121. survivor_count= 1;
  4122. for(i=start_i; i<=last_non_zero; i++){
  4123. int level_index, j;
  4124. const int dct_coeff= ABS(block[ scantable[i] ]);
  4125. const int zero_distoration= dct_coeff*dct_coeff;
  4126. int best_score=256*256*256*120;
  4127. for(level_index=0; level_index < coeff_count[i]; level_index++){
  4128. int distoration;
  4129. int level= coeff[level_index][i];
  4130. const int alevel= ABS(level);
  4131. int unquant_coeff;
  4132. assert(level);
  4133. if(s->out_format == FMT_H263){
  4134. unquant_coeff= alevel*qmul + qadd;
  4135. }else{ //MPEG1
  4136. j= s->dsp.idct_permutation[ scantable[i] ]; //FIXME optimize
  4137. if(s->mb_intra){
  4138. unquant_coeff = (int)( alevel * qscale * s->intra_matrix[j]) >> 3;
  4139. unquant_coeff = (unquant_coeff - 1) | 1;
  4140. }else{
  4141. unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
  4142. unquant_coeff = (unquant_coeff - 1) | 1;
  4143. }
  4144. unquant_coeff<<= 3;
  4145. }
  4146. distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distoration;
  4147. level+=64;
  4148. if((level&(~127)) == 0){
  4149. for(j=survivor_count-1; j>=0; j--){
  4150. int run= i - survivor[j];
  4151. int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  4152. score += score_tab[i-run];
  4153. if(score < best_score){
  4154. best_score= score;
  4155. run_tab[i+1]= run;
  4156. level_tab[i+1]= level-64;
  4157. }
  4158. }
  4159. if(s->out_format == FMT_H263){
  4160. for(j=survivor_count-1; j>=0; j--){
  4161. int run= i - survivor[j];
  4162. int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
  4163. score += score_tab[i-run];
  4164. if(score < last_score){
  4165. last_score= score;
  4166. last_run= run;
  4167. last_level= level-64;
  4168. last_i= i+1;
  4169. }
  4170. }
  4171. }
  4172. }else{
  4173. distoration += esc_length*lambda;
  4174. for(j=survivor_count-1; j>=0; j--){
  4175. int run= i - survivor[j];
  4176. int score= distoration + score_tab[i-run];
  4177. if(score < best_score){
  4178. best_score= score;
  4179. run_tab[i+1]= run;
  4180. level_tab[i+1]= level-64;
  4181. }
  4182. }
  4183. if(s->out_format == FMT_H263){
  4184. for(j=survivor_count-1; j>=0; j--){
  4185. int run= i - survivor[j];
  4186. int score= distoration + score_tab[i-run];
  4187. if(score < last_score){
  4188. last_score= score;
  4189. last_run= run;
  4190. last_level= level-64;
  4191. last_i= i+1;
  4192. }
  4193. }
  4194. }
  4195. }
  4196. }
  4197. score_tab[i+1]= best_score;
  4198. //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
  4199. if(last_non_zero <= 27){
  4200. for(; survivor_count; survivor_count--){
  4201. if(score_tab[ survivor[survivor_count-1] ] <= best_score)
  4202. break;
  4203. }
  4204. }else{
  4205. for(; survivor_count; survivor_count--){
  4206. if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
  4207. break;
  4208. }
  4209. }
  4210. survivor[ survivor_count++ ]= i+1;
  4211. }
  4212. if(s->out_format != FMT_H263){
  4213. last_score= 256*256*256*120;
  4214. for(i= survivor[0]; i<=last_non_zero + 1; i++){
  4215. int score= score_tab[i];
  4216. if(i) score += lambda*2; //FIXME exacter?
  4217. if(score < last_score){
  4218. last_score= score;
  4219. last_i= i;
  4220. last_level= level_tab[i];
  4221. last_run= run_tab[i];
  4222. }
  4223. }
  4224. }
  4225. s->coded_score[n] = last_score;
  4226. dc= ABS(block[0]);
  4227. last_non_zero= last_i - 1;
  4228. memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
  4229. if(last_non_zero < start_i)
  4230. return last_non_zero;
  4231. if(last_non_zero == 0 && start_i == 0){
  4232. int best_level= 0;
  4233. int best_score= dc * dc;
  4234. for(i=0; i<coeff_count[0]; i++){
  4235. int level= coeff[i][0];
  4236. int alevel= ABS(level);
  4237. int unquant_coeff, score, distortion;
  4238. if(s->out_format == FMT_H263){
  4239. unquant_coeff= (alevel*qmul + qadd)>>3;
  4240. }else{ //MPEG1
  4241. unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
  4242. unquant_coeff = (unquant_coeff - 1) | 1;
  4243. }
  4244. unquant_coeff = (unquant_coeff + 4) >> 3;
  4245. unquant_coeff<<= 3 + 3;
  4246. distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
  4247. level+=64;
  4248. if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
  4249. else score= distortion + esc_length*lambda;
  4250. if(score < best_score){
  4251. best_score= score;
  4252. best_level= level - 64;
  4253. }
  4254. }
  4255. block[0]= best_level;
  4256. s->coded_score[n] = best_score - dc*dc;
  4257. if(best_level == 0) return -1;
  4258. else return last_non_zero;
  4259. }
  4260. i= last_i;
  4261. assert(last_level);
  4262. block[ perm_scantable[last_non_zero] ]= last_level;
  4263. i -= last_run + 1;
  4264. for(; i>start_i; i -= run_tab[i] + 1){
  4265. block[ perm_scantable[i-1] ]= level_tab[i];
  4266. }
  4267. return last_non_zero;
  4268. }
  4269. static int dct_quantize_c(MpegEncContext *s,
  4270. DCTELEM *block, int n,
  4271. int qscale, int *overflow)
  4272. {
  4273. int i, j, level, last_non_zero, q, start_i;
  4274. const int *qmat;
  4275. const uint8_t *scantable= s->intra_scantable.scantable;
  4276. int bias;
  4277. int max=0;
  4278. unsigned int threshold1, threshold2;
  4279. s->dsp.fdct (block);
  4280. if(s->dct_error_sum)
  4281. s->denoise_dct(s, block);
  4282. if (s->mb_intra) {
  4283. if (!s->h263_aic) {
  4284. if (n < 4)
  4285. q = s->y_dc_scale;
  4286. else
  4287. q = s->c_dc_scale;
  4288. q = q << 3;
  4289. } else
  4290. /* For AIC we skip quant/dequant of INTRADC */
  4291. q = 1 << 3;
  4292. /* note: block[0] is assumed to be positive */
  4293. block[0] = (block[0] + (q >> 1)) / q;
  4294. start_i = 1;
  4295. last_non_zero = 0;
  4296. qmat = s->q_intra_matrix[qscale];
  4297. bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  4298. } else {
  4299. start_i = 0;
  4300. last_non_zero = -1;
  4301. qmat = s->q_inter_matrix[qscale];
  4302. bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
  4303. }
  4304. threshold1= (1<<QMAT_SHIFT) - bias - 1;
  4305. threshold2= (threshold1<<1);
  4306. for(i=63;i>=start_i;i--) {
  4307. j = scantable[i];
  4308. level = block[j] * qmat[j];
  4309. if(((unsigned)(level+threshold1))>threshold2){
  4310. last_non_zero = i;
  4311. break;
  4312. }else{
  4313. block[j]=0;
  4314. }
  4315. }
  4316. for(i=start_i; i<=last_non_zero; i++) {
  4317. j = scantable[i];
  4318. level = block[j] * qmat[j];
  4319. // if( bias+level >= (1<<QMAT_SHIFT)
  4320. // || bias-level >= (1<<QMAT_SHIFT)){
  4321. if(((unsigned)(level+threshold1))>threshold2){
  4322. if(level>0){
  4323. level= (bias + level)>>QMAT_SHIFT;
  4324. block[j]= level;
  4325. }else{
  4326. level= (bias - level)>>QMAT_SHIFT;
  4327. block[j]= -level;
  4328. }
  4329. max |=level;
  4330. }else{
  4331. block[j]=0;
  4332. }
  4333. }
  4334. *overflow= s->max_qcoeff < max; //overflow might have happend
  4335. /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
  4336. if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
  4337. ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
  4338. return last_non_zero;
  4339. }
  4340. #endif //CONFIG_ENCODERS
  4341. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  4342. DCTELEM *block, int n, int qscale)
  4343. {
  4344. int i, level, nCoeffs;
  4345. const uint16_t *quant_matrix;
  4346. nCoeffs= s->block_last_index[n];
  4347. if (n < 4)
  4348. block[0] = block[0] * s->y_dc_scale;
  4349. else
  4350. block[0] = block[0] * s->c_dc_scale;
  4351. /* XXX: only mpeg1 */
  4352. quant_matrix = s->intra_matrix;
  4353. for(i=1;i<=nCoeffs;i++) {
  4354. int j= s->intra_scantable.permutated[i];
  4355. level = block[j];
  4356. if (level) {
  4357. if (level < 0) {
  4358. level = -level;
  4359. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  4360. level = (level - 1) | 1;
  4361. level = -level;
  4362. } else {
  4363. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  4364. level = (level - 1) | 1;
  4365. }
  4366. block[j] = level;
  4367. }
  4368. }
  4369. }
  4370. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  4371. DCTELEM *block, int n, int qscale)
  4372. {
  4373. int i, level, nCoeffs;
  4374. const uint16_t *quant_matrix;
  4375. nCoeffs= s->block_last_index[n];
  4376. quant_matrix = s->inter_matrix;
  4377. for(i=0; i<=nCoeffs; i++) {
  4378. int j= s->intra_scantable.permutated[i];
  4379. level = block[j];
  4380. if (level) {
  4381. if (level < 0) {
  4382. level = -level;
  4383. level = (((level << 1) + 1) * qscale *
  4384. ((int) (quant_matrix[j]))) >> 4;
  4385. level = (level - 1) | 1;
  4386. level = -level;
  4387. } else {
  4388. level = (((level << 1) + 1) * qscale *
  4389. ((int) (quant_matrix[j]))) >> 4;
  4390. level = (level - 1) | 1;
  4391. }
  4392. block[j] = level;
  4393. }
  4394. }
  4395. }
  4396. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  4397. DCTELEM *block, int n, int qscale)
  4398. {
  4399. int i, level, nCoeffs;
  4400. const uint16_t *quant_matrix;
  4401. if(s->alternate_scan) nCoeffs= 63;
  4402. else nCoeffs= s->block_last_index[n];
  4403. if (n < 4)
  4404. block[0] = block[0] * s->y_dc_scale;
  4405. else
  4406. block[0] = block[0] * s->c_dc_scale;
  4407. quant_matrix = s->intra_matrix;
  4408. for(i=1;i<=nCoeffs;i++) {
  4409. int j= s->intra_scantable.permutated[i];
  4410. level = block[j];
  4411. if (level) {
  4412. if (level < 0) {
  4413. level = -level;
  4414. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  4415. level = -level;
  4416. } else {
  4417. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  4418. }
  4419. block[j] = level;
  4420. }
  4421. }
  4422. }
  4423. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  4424. DCTELEM *block, int n, int qscale)
  4425. {
  4426. int i, level, nCoeffs;
  4427. const uint16_t *quant_matrix;
  4428. int sum=-1;
  4429. if(s->alternate_scan) nCoeffs= 63;
  4430. else nCoeffs= s->block_last_index[n];
  4431. quant_matrix = s->inter_matrix;
  4432. for(i=0; i<=nCoeffs; i++) {
  4433. int j= s->intra_scantable.permutated[i];
  4434. level = block[j];
  4435. if (level) {
  4436. if (level < 0) {
  4437. level = -level;
  4438. level = (((level << 1) + 1) * qscale *
  4439. ((int) (quant_matrix[j]))) >> 4;
  4440. level = -level;
  4441. } else {
  4442. level = (((level << 1) + 1) * qscale *
  4443. ((int) (quant_matrix[j]))) >> 4;
  4444. }
  4445. block[j] = level;
  4446. sum+=level;
  4447. }
  4448. }
  4449. block[63]^=sum&1;
  4450. }
  4451. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  4452. DCTELEM *block, int n, int qscale)
  4453. {
  4454. int i, level, qmul, qadd;
  4455. int nCoeffs;
  4456. assert(s->block_last_index[n]>=0);
  4457. qmul = qscale << 1;
  4458. if (!s->h263_aic) {
  4459. if (n < 4)
  4460. block[0] = block[0] * s->y_dc_scale;
  4461. else
  4462. block[0] = block[0] * s->c_dc_scale;
  4463. qadd = (qscale - 1) | 1;
  4464. }else{
  4465. qadd = 0;
  4466. }
  4467. if(s->ac_pred)
  4468. nCoeffs=63;
  4469. else
  4470. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  4471. for(i=1; i<=nCoeffs; i++) {
  4472. level = block[i];
  4473. if (level) {
  4474. if (level < 0) {
  4475. level = level * qmul - qadd;
  4476. } else {
  4477. level = level * qmul + qadd;
  4478. }
  4479. block[i] = level;
  4480. }
  4481. }
  4482. }
  4483. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  4484. DCTELEM *block, int n, int qscale)
  4485. {
  4486. int i, level, qmul, qadd;
  4487. int nCoeffs;
  4488. assert(s->block_last_index[n]>=0);
  4489. qadd = (qscale - 1) | 1;
  4490. qmul = qscale << 1;
  4491. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  4492. for(i=0; i<=nCoeffs; i++) {
  4493. level = block[i];
  4494. if (level) {
  4495. if (level < 0) {
  4496. level = level * qmul - qadd;
  4497. } else {
  4498. level = level * qmul + qadd;
  4499. }
  4500. block[i] = level;
  4501. }
  4502. }
  4503. }
  4504. static const AVOption mpeg4_options[] =
  4505. {
  4506. AVOPTION_CODEC_INT("bitrate", "desired video bitrate", bit_rate, 4, 240000000, 800000),
  4507. AVOPTION_CODEC_INT("ratetol", "number of bits the bitstream is allowed to diverge from the reference"
  4508. "the reference can be CBR (for CBR pass1) or VBR (for pass2)",
  4509. bit_rate_tolerance, 4, 240000000, 8000),
  4510. AVOPTION_CODEC_INT("qmin", "minimum quantizer", qmin, 1, 31, 2),
  4511. AVOPTION_CODEC_INT("qmax", "maximum quantizer", qmax, 1, 31, 31),
  4512. AVOPTION_CODEC_STRING("rc_eq", "rate control equation",
  4513. rc_eq, "tex^qComp,option1,options2", 0),
  4514. AVOPTION_CODEC_INT("rc_minrate", "rate control minimum bitrate",
  4515. rc_min_rate, 4, 24000000, 0),
  4516. AVOPTION_CODEC_INT("rc_maxrate", "rate control maximum bitrate",
  4517. rc_max_rate, 4, 24000000, 0),
  4518. AVOPTION_CODEC_DOUBLE("rc_buf_aggresivity", "rate control buffer aggresivity",
  4519. rc_buffer_aggressivity, 4, 24000000, 0),
  4520. AVOPTION_CODEC_DOUBLE("rc_initial_cplx", "initial complexity for pass1 ratecontrol",
  4521. rc_initial_cplx, 0., 9999999., 0),
  4522. AVOPTION_CODEC_DOUBLE("i_quant_factor", "qscale factor between p and i frames",
  4523. i_quant_factor, 0., 0., 0),
  4524. AVOPTION_CODEC_DOUBLE("i_quant_offset", "qscale offset between p and i frames",
  4525. i_quant_factor, -999999., 999999., 0),
  4526. AVOPTION_CODEC_INT("dct_algo", "dct alghorithm",
  4527. dct_algo, 0, 5, 0), // fixme - "Auto,FastInt,Int,MMX,MLib,Altivec"
  4528. AVOPTION_CODEC_DOUBLE("lumi_masking", "luminance masking",
  4529. lumi_masking, 0., 999999., 0),
  4530. AVOPTION_CODEC_DOUBLE("temporal_cplx_masking", "temporary complexity masking",
  4531. temporal_cplx_masking, 0., 999999., 0),
  4532. AVOPTION_CODEC_DOUBLE("spatial_cplx_masking", "spatial complexity masking",
  4533. spatial_cplx_masking, 0., 999999., 0),
  4534. AVOPTION_CODEC_DOUBLE("p_masking", "p block masking",
  4535. p_masking, 0., 999999., 0),
  4536. AVOPTION_CODEC_DOUBLE("dark_masking", "darkness masking",
  4537. dark_masking, 0., 999999., 0),
  4538. AVOPTION_CODEC_INT("idct_algo", "idct alghorithm",
  4539. idct_algo, 0, 8, 0), // fixme - "Auto,Int,Simple,SimpleMMX,LibMPEG2MMX,PS2,MLib,ARM,Altivec"
  4540. AVOPTION_CODEC_INT("mb_qmin", "minimum MB quantizer",
  4541. mb_qmin, 0, 8, 0),
  4542. AVOPTION_CODEC_INT("mb_qmax", "maximum MB quantizer",
  4543. mb_qmin, 0, 8, 0),
  4544. AVOPTION_CODEC_INT("me_cmp", "ME compare function",
  4545. me_cmp, 0, 24000000, 0),
  4546. AVOPTION_CODEC_INT("me_sub_cmp", "subpixel ME compare function",
  4547. me_sub_cmp, 0, 24000000, 0),
  4548. AVOPTION_CODEC_INT("dia_size", "ME diamond size & shape",
  4549. dia_size, 0, 24000000, 0),
  4550. AVOPTION_CODEC_INT("last_predictor_count", "amount of previous MV predictors",
  4551. last_predictor_count, 0, 24000000, 0),
  4552. AVOPTION_CODEC_INT("pre_me", "pre pass for ME",
  4553. pre_me, 0, 24000000, 0),
  4554. AVOPTION_CODEC_INT("me_pre_cmp", "ME pre pass compare function",
  4555. me_pre_cmp, 0, 24000000, 0),
  4556. AVOPTION_CODEC_INT("me_range", "maximum ME search range",
  4557. me_range, 0, 24000000, 0),
  4558. AVOPTION_CODEC_INT("pre_dia_size", "ME pre pass diamod size & shape",
  4559. pre_dia_size, 0, 24000000, 0),
  4560. AVOPTION_CODEC_INT("me_subpel_quality", "subpel ME quality",
  4561. me_subpel_quality, 0, 24000000, 0),
  4562. AVOPTION_CODEC_INT("me_range", "maximum ME search range",
  4563. me_range, 0, 24000000, 0),
  4564. AVOPTION_CODEC_FLAG("psnr", "calculate PSNR of compressed frames",
  4565. flags, CODEC_FLAG_PSNR, 0),
  4566. AVOPTION_CODEC_RCOVERRIDE("rc_override", "ratecontrol override (=startframe,endframe,qscale,quality_factor)",
  4567. rc_override),
  4568. AVOPTION_SUB(avoptions_common),
  4569. AVOPTION_END()
  4570. };
  4571. #ifdef CONFIG_ENCODERS
  4572. #ifdef CONFIG_RISKY
  4573. AVCodec h263_encoder = {
  4574. "h263",
  4575. CODEC_TYPE_VIDEO,
  4576. CODEC_ID_H263,
  4577. sizeof(MpegEncContext),
  4578. MPV_encode_init,
  4579. MPV_encode_picture,
  4580. MPV_encode_end,
  4581. };
  4582. AVCodec h263p_encoder = {
  4583. "h263p",
  4584. CODEC_TYPE_VIDEO,
  4585. CODEC_ID_H263P,
  4586. sizeof(MpegEncContext),
  4587. MPV_encode_init,
  4588. MPV_encode_picture,
  4589. MPV_encode_end,
  4590. };
  4591. AVCodec flv_encoder = {
  4592. "flv",
  4593. CODEC_TYPE_VIDEO,
  4594. CODEC_ID_FLV1,
  4595. sizeof(MpegEncContext),
  4596. MPV_encode_init,
  4597. MPV_encode_picture,
  4598. MPV_encode_end,
  4599. };
  4600. AVCodec rv10_encoder = {
  4601. "rv10",
  4602. CODEC_TYPE_VIDEO,
  4603. CODEC_ID_RV10,
  4604. sizeof(MpegEncContext),
  4605. MPV_encode_init,
  4606. MPV_encode_picture,
  4607. MPV_encode_end,
  4608. };
  4609. AVCodec mpeg4_encoder = {
  4610. "mpeg4",
  4611. CODEC_TYPE_VIDEO,
  4612. CODEC_ID_MPEG4,
  4613. sizeof(MpegEncContext),
  4614. MPV_encode_init,
  4615. MPV_encode_picture,
  4616. MPV_encode_end,
  4617. .options = mpeg4_options,
  4618. };
  4619. AVCodec msmpeg4v1_encoder = {
  4620. "msmpeg4v1",
  4621. CODEC_TYPE_VIDEO,
  4622. CODEC_ID_MSMPEG4V1,
  4623. sizeof(MpegEncContext),
  4624. MPV_encode_init,
  4625. MPV_encode_picture,
  4626. MPV_encode_end,
  4627. .options = mpeg4_options,
  4628. };
  4629. AVCodec msmpeg4v2_encoder = {
  4630. "msmpeg4v2",
  4631. CODEC_TYPE_VIDEO,
  4632. CODEC_ID_MSMPEG4V2,
  4633. sizeof(MpegEncContext),
  4634. MPV_encode_init,
  4635. MPV_encode_picture,
  4636. MPV_encode_end,
  4637. .options = mpeg4_options,
  4638. };
  4639. AVCodec msmpeg4v3_encoder = {
  4640. "msmpeg4",
  4641. CODEC_TYPE_VIDEO,
  4642. CODEC_ID_MSMPEG4V3,
  4643. sizeof(MpegEncContext),
  4644. MPV_encode_init,
  4645. MPV_encode_picture,
  4646. MPV_encode_end,
  4647. .options = mpeg4_options,
  4648. };
  4649. AVCodec wmv1_encoder = {
  4650. "wmv1",
  4651. CODEC_TYPE_VIDEO,
  4652. CODEC_ID_WMV1,
  4653. sizeof(MpegEncContext),
  4654. MPV_encode_init,
  4655. MPV_encode_picture,
  4656. MPV_encode_end,
  4657. .options = mpeg4_options,
  4658. };
  4659. #endif
  4660. AVCodec mjpeg_encoder = {
  4661. "mjpeg",
  4662. CODEC_TYPE_VIDEO,
  4663. CODEC_ID_MJPEG,
  4664. sizeof(MpegEncContext),
  4665. MPV_encode_init,
  4666. MPV_encode_picture,
  4667. MPV_encode_end,
  4668. };
  4669. #endif //CONFIG_ENCODERS