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.

5056 lines
176KB

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