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.

4945 lines
173KB

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