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.

5959 lines
211KB

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