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.

4407 lines
153KB

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