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.

5206 lines
183KB

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