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.

2675 lines
103KB

  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. * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file
  26. * The simplest mpeg encoder (well, it was the simplest!).
  27. */
  28. #include "libavutil/intmath.h"
  29. #include "libavutil/imgutils.h"
  30. #include "avcodec.h"
  31. #include "dsputil.h"
  32. #include "internal.h"
  33. #include "mpegvideo.h"
  34. #include "mpegvideo_common.h"
  35. #include "mjpegenc.h"
  36. #include "msmpeg4.h"
  37. #include "faandct.h"
  38. #include "xvmc_internal.h"
  39. #include "thread.h"
  40. #include <limits.h>
  41. //#undef NDEBUG
  42. //#include <assert.h>
  43. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  44. DCTELEM *block, int n, int qscale);
  45. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  46. DCTELEM *block, int n, int qscale);
  47. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  48. DCTELEM *block, int n, int qscale);
  49. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  50. DCTELEM *block, int n, int qscale);
  51. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  52. DCTELEM *block, int n, int qscale);
  53. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  54. DCTELEM *block, int n, int qscale);
  55. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  56. DCTELEM *block, int n, int qscale);
  57. /* enable all paranoid tests for rounding, overflows, etc... */
  58. //#define PARANOID
  59. //#define DEBUG
  60. static const uint8_t ff_default_chroma_qscale_table[32]={
  61. // 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
  62. 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
  63. };
  64. const uint8_t ff_mpeg1_dc_scale_table[128]={
  65. // 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
  66. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  67. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  68. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  69. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  70. };
  71. static const uint8_t mpeg2_dc_scale_table1[128]={
  72. // 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
  73. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  74. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  75. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  76. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  77. };
  78. static const uint8_t mpeg2_dc_scale_table2[128]={
  79. // 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
  80. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  81. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  82. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  83. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  84. };
  85. static const uint8_t mpeg2_dc_scale_table3[128]={
  86. // 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
  87. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  88. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  89. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  90. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  91. };
  92. const uint8_t * const ff_mpeg2_dc_scale_table[4]={
  93. ff_mpeg1_dc_scale_table,
  94. mpeg2_dc_scale_table1,
  95. mpeg2_dc_scale_table2,
  96. mpeg2_dc_scale_table3,
  97. };
  98. const enum PixelFormat ff_pixfmt_list_420[] = {
  99. PIX_FMT_YUV420P,
  100. PIX_FMT_NONE
  101. };
  102. const enum PixelFormat ff_hwaccel_pixfmt_list_420[] = {
  103. PIX_FMT_DXVA2_VLD,
  104. PIX_FMT_VAAPI_VLD,
  105. PIX_FMT_YUV420P,
  106. PIX_FMT_NONE
  107. };
  108. const uint8_t *ff_find_start_code(const uint8_t * restrict p, const uint8_t *end, uint32_t * restrict state){
  109. int i;
  110. assert(p<=end);
  111. if(p>=end)
  112. return end;
  113. for(i=0; i<3; i++){
  114. uint32_t tmp= *state << 8;
  115. *state= tmp + *(p++);
  116. if(tmp == 0x100 || p==end)
  117. return p;
  118. }
  119. while(p<end){
  120. if (p[-1] > 1 ) p+= 3;
  121. else if(p[-2] ) p+= 2;
  122. else if(p[-3]|(p[-1]-1)) p++;
  123. else{
  124. p++;
  125. break;
  126. }
  127. }
  128. p= FFMIN(p, end)-4;
  129. *state= AV_RB32(p);
  130. return p+4;
  131. }
  132. /* init common dct for both encoder and decoder */
  133. av_cold int ff_dct_common_init(MpegEncContext *s)
  134. {
  135. dsputil_init(&s->dsp, s->avctx);
  136. s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
  137. s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
  138. s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
  139. s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
  140. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
  141. if(s->flags & CODEC_FLAG_BITEXACT)
  142. s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
  143. s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
  144. #if HAVE_MMX
  145. MPV_common_init_mmx(s);
  146. #elif ARCH_ALPHA
  147. MPV_common_init_axp(s);
  148. #elif CONFIG_MLIB
  149. MPV_common_init_mlib(s);
  150. #elif HAVE_MMI
  151. MPV_common_init_mmi(s);
  152. #elif ARCH_ARM
  153. MPV_common_init_arm(s);
  154. #elif HAVE_ALTIVEC
  155. MPV_common_init_altivec(s);
  156. #elif ARCH_BFIN
  157. MPV_common_init_bfin(s);
  158. #endif
  159. /* load & permutate scantables
  160. note: only wmv uses different ones
  161. */
  162. if(s->alternate_scan){
  163. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  164. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  165. }else{
  166. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  167. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  168. }
  169. ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
  170. ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
  171. return 0;
  172. }
  173. void ff_copy_picture(Picture *dst, Picture *src){
  174. *dst = *src;
  175. dst->f.type= FF_BUFFER_TYPE_COPY;
  176. }
  177. /**
  178. * Release a frame buffer
  179. */
  180. static void free_frame_buffer(MpegEncContext *s, Picture *pic)
  181. {
  182. /* Windows Media Image codecs allocate internal buffers with different
  183. dimensions; ignore user defined callbacks for these */
  184. if (s->codec_id != CODEC_ID_WMV3IMAGE && s->codec_id != CODEC_ID_VC1IMAGE)
  185. ff_thread_release_buffer(s->avctx, (AVFrame*)pic);
  186. else
  187. avcodec_default_release_buffer(s->avctx, (AVFrame*)pic);
  188. av_freep(&pic->f.hwaccel_picture_private);
  189. }
  190. /**
  191. * Allocate a frame buffer
  192. */
  193. static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
  194. {
  195. int r;
  196. if (s->avctx->hwaccel) {
  197. assert(!pic->f.hwaccel_picture_private);
  198. if (s->avctx->hwaccel->priv_data_size) {
  199. pic->f.hwaccel_picture_private = av_mallocz(s->avctx->hwaccel->priv_data_size);
  200. if (!pic->f.hwaccel_picture_private) {
  201. av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  202. return -1;
  203. }
  204. }
  205. }
  206. if (s->codec_id != CODEC_ID_WMV3IMAGE && s->codec_id != CODEC_ID_VC1IMAGE)
  207. r = ff_thread_get_buffer(s->avctx, (AVFrame*)pic);
  208. else
  209. r = avcodec_default_get_buffer(s->avctx, (AVFrame*)pic);
  210. if (r < 0 || !pic->f.age || !pic->f.type || !pic->f.data[0]) {
  211. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n",
  212. r, pic->f.age, pic->f.type, pic->f.data[0]);
  213. av_freep(&pic->f.hwaccel_picture_private);
  214. return -1;
  215. }
  216. if (s->linesize && (s->linesize != pic->f.linesize[0] || s->uvlinesize != pic->f.linesize[1])) {
  217. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
  218. free_frame_buffer(s, pic);
  219. return -1;
  220. }
  221. if (pic->f.linesize[1] != pic->f.linesize[2]) {
  222. av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride mismatch)\n");
  223. free_frame_buffer(s, pic);
  224. return -1;
  225. }
  226. return 0;
  227. }
  228. /**
  229. * allocates a Picture
  230. * The pixels are allocated/set by calling get_buffer() if shared=0
  231. */
  232. int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared){
  233. const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1; //the +1 is needed so memset(,,stride*height) does not sig11
  234. const int mb_array_size= s->mb_stride*s->mb_height;
  235. const int b8_array_size= s->b8_stride*s->mb_height*2;
  236. const int b4_array_size= s->b4_stride*s->mb_height*4;
  237. int i;
  238. int r= -1;
  239. if(shared){
  240. assert(pic->f.data[0]);
  241. assert(pic->f.type == 0 || pic->f.type == FF_BUFFER_TYPE_SHARED);
  242. pic->f.type = FF_BUFFER_TYPE_SHARED;
  243. }else{
  244. assert(!pic->f.data[0]);
  245. if (alloc_frame_buffer(s, pic) < 0)
  246. return -1;
  247. s->linesize = pic->f.linesize[0];
  248. s->uvlinesize = pic->f.linesize[1];
  249. }
  250. if (pic->f.qscale_table == NULL) {
  251. if (s->encoding) {
  252. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_var , mb_array_size * sizeof(int16_t) , fail)
  253. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mc_mb_var, mb_array_size * sizeof(int16_t) , fail)
  254. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_mean , mb_array_size * sizeof(int8_t ) , fail)
  255. }
  256. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.mbskip_table, mb_array_size * sizeof(uint8_t) + 2, fail) //the +2 is for the slice end check
  257. FF_ALLOCZ_OR_GOTO(s->avctx, pic->qscale_table_base , (big_mb_num + s->mb_stride) * sizeof(uint8_t) , fail)
  258. FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_type_base , (big_mb_num + s->mb_stride) * sizeof(uint32_t), fail)
  259. pic->f.mb_type = pic->mb_type_base + 2*s->mb_stride + 1;
  260. pic->f.qscale_table = pic->qscale_table_base + 2*s->mb_stride + 1;
  261. if(s->out_format == FMT_H264){
  262. for(i=0; i<2; i++){
  263. FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i], 2 * (b4_array_size+4) * sizeof(int16_t), fail)
  264. pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
  265. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.ref_index[i], 4*mb_array_size * sizeof(uint8_t), fail)
  266. }
  267. pic->f.motion_subsample_log2 = 2;
  268. }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_MV) || (s->avctx->debug_mv)){
  269. for(i=0; i<2; i++){
  270. FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i], 2 * (b8_array_size+4) * sizeof(int16_t), fail)
  271. pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
  272. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.ref_index[i], 4*mb_array_size * sizeof(uint8_t), fail)
  273. }
  274. pic->f.motion_subsample_log2 = 3;
  275. }
  276. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  277. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.dct_coeff, 64 * mb_array_size * sizeof(DCTELEM) * 6, fail)
  278. }
  279. pic->f.qstride = s->mb_stride;
  280. FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.pan_scan , 1 * sizeof(AVPanScan), fail)
  281. }
  282. /* It might be nicer if the application would keep track of these
  283. * but it would require an API change. */
  284. memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
  285. s->prev_pict_types[0]= s->dropable ? AV_PICTURE_TYPE_B : s->pict_type;
  286. if (pic->f.age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->f.age] == AV_PICTURE_TYPE_B)
  287. pic->f.age = INT_MAX; // Skipped MBs in B-frames are quite rare in MPEG-1/2 and it is a bit tricky to skip them anyway.
  288. pic->owner2 = s;
  289. return 0;
  290. fail: //for the FF_ALLOCZ_OR_GOTO macro
  291. if(r>=0)
  292. free_frame_buffer(s, pic);
  293. return -1;
  294. }
  295. /**
  296. * deallocates a picture
  297. */
  298. static void free_picture(MpegEncContext *s, Picture *pic){
  299. int i;
  300. if (pic->f.data[0] && pic->f.type != FF_BUFFER_TYPE_SHARED) {
  301. free_frame_buffer(s, pic);
  302. }
  303. av_freep(&pic->mb_var);
  304. av_freep(&pic->mc_mb_var);
  305. av_freep(&pic->mb_mean);
  306. av_freep(&pic->f.mbskip_table);
  307. av_freep(&pic->qscale_table_base);
  308. av_freep(&pic->mb_type_base);
  309. av_freep(&pic->f.dct_coeff);
  310. av_freep(&pic->f.pan_scan);
  311. pic->f.mb_type = NULL;
  312. for(i=0; i<2; i++){
  313. av_freep(&pic->motion_val_base[i]);
  314. av_freep(&pic->f.ref_index[i]);
  315. }
  316. if (pic->f.type == FF_BUFFER_TYPE_SHARED) {
  317. for(i=0; i<4; i++){
  318. pic->f.base[i] =
  319. pic->f.data[i] = NULL;
  320. }
  321. pic->f.type = 0;
  322. }
  323. }
  324. static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){
  325. int y_size = s->b8_stride * (2 * s->mb_height + 1);
  326. int c_size = s->mb_stride * (s->mb_height + 1);
  327. int yc_size = y_size + 2 * c_size;
  328. int i;
  329. // edge emu needs blocksize + filter length - 1 (=17x17 for halfpel / 21x21 for h264)
  330. FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, (s->width+64)*2*21*2, fail); //(width + edge + align)*interlaced*MBsize*tolerance
  331. //FIXME should be linesize instead of s->width*2 but that is not known before get_buffer()
  332. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, (s->width+64)*4*16*2*sizeof(uint8_t), fail)
  333. s->me.temp= s->me.scratchpad;
  334. s->rd_scratchpad= s->me.scratchpad;
  335. s->b_scratchpad= s->me.scratchpad;
  336. s->obmc_scratchpad= s->me.scratchpad + 16;
  337. if (s->encoding) {
  338. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map , ME_MAP_SIZE*sizeof(uint32_t), fail)
  339. FF_ALLOCZ_OR_GOTO(s->avctx, s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t), fail)
  340. if(s->avctx->noise_reduction){
  341. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_error_sum, 2 * 64 * sizeof(int), fail)
  342. }
  343. }
  344. FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64*12*2 * sizeof(DCTELEM), fail)
  345. s->block= s->blocks[0];
  346. for(i=0;i<12;i++){
  347. s->pblocks[i] = &s->block[i];
  348. }
  349. if (s->out_format == FMT_H263) {
  350. /* ac values */
  351. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_val_base, yc_size * sizeof(int16_t) * 16, fail);
  352. s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
  353. s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
  354. s->ac_val[2] = s->ac_val[1] + c_size;
  355. }
  356. return 0;
  357. fail:
  358. return -1; //free() through MPV_common_end()
  359. }
  360. static void free_duplicate_context(MpegEncContext *s){
  361. if(s==NULL) return;
  362. av_freep(&s->edge_emu_buffer);
  363. av_freep(&s->me.scratchpad);
  364. s->me.temp=
  365. s->rd_scratchpad=
  366. s->b_scratchpad=
  367. s->obmc_scratchpad= NULL;
  368. av_freep(&s->dct_error_sum);
  369. av_freep(&s->me.map);
  370. av_freep(&s->me.score_map);
  371. av_freep(&s->blocks);
  372. av_freep(&s->ac_val_base);
  373. s->block= NULL;
  374. }
  375. static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src){
  376. #define COPY(a) bak->a= src->a
  377. COPY(edge_emu_buffer);
  378. COPY(me.scratchpad);
  379. COPY(me.temp);
  380. COPY(rd_scratchpad);
  381. COPY(b_scratchpad);
  382. COPY(obmc_scratchpad);
  383. COPY(me.map);
  384. COPY(me.score_map);
  385. COPY(blocks);
  386. COPY(block);
  387. COPY(start_mb_y);
  388. COPY(end_mb_y);
  389. COPY(me.map_generation);
  390. COPY(pb);
  391. COPY(dct_error_sum);
  392. COPY(dct_count[0]);
  393. COPY(dct_count[1]);
  394. COPY(ac_val_base);
  395. COPY(ac_val[0]);
  396. COPY(ac_val[1]);
  397. COPY(ac_val[2]);
  398. #undef COPY
  399. }
  400. void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src){
  401. MpegEncContext bak;
  402. int i;
  403. //FIXME copy only needed parts
  404. //START_TIMER
  405. backup_duplicate_context(&bak, dst);
  406. memcpy(dst, src, sizeof(MpegEncContext));
  407. backup_duplicate_context(dst, &bak);
  408. for(i=0;i<12;i++){
  409. dst->pblocks[i] = &dst->block[i];
  410. }
  411. //STOP_TIMER("update_duplicate_context") //about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
  412. }
  413. int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
  414. {
  415. MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
  416. if(dst == src || !s1->context_initialized) return 0;
  417. //FIXME can parameters change on I-frames? in that case dst may need a reinit
  418. if(!s->context_initialized){
  419. memcpy(s, s1, sizeof(MpegEncContext));
  420. s->avctx = dst;
  421. s->picture_range_start += MAX_PICTURE_COUNT;
  422. s->picture_range_end += MAX_PICTURE_COUNT;
  423. s->bitstream_buffer = NULL;
  424. s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
  425. MPV_common_init(s);
  426. }
  427. s->avctx->coded_height = s1->avctx->coded_height;
  428. s->avctx->coded_width = s1->avctx->coded_width;
  429. s->avctx->width = s1->avctx->width;
  430. s->avctx->height = s1->avctx->height;
  431. s->coded_picture_number = s1->coded_picture_number;
  432. s->picture_number = s1->picture_number;
  433. s->input_picture_number = s1->input_picture_number;
  434. memcpy(s->picture, s1->picture, s1->picture_count * sizeof(Picture));
  435. memcpy(&s->last_picture, &s1->last_picture, (char*)&s1->last_picture_ptr - (char*)&s1->last_picture);
  436. s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
  437. s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
  438. s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
  439. memcpy(s->prev_pict_types, s1->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
  440. //Error/bug resilience
  441. s->next_p_frame_damaged = s1->next_p_frame_damaged;
  442. s->workaround_bugs = s1->workaround_bugs;
  443. //MPEG4 timing info
  444. memcpy(&s->time_increment_bits, &s1->time_increment_bits, (char*)&s1->shape - (char*)&s1->time_increment_bits);
  445. //B-frame info
  446. s->max_b_frames = s1->max_b_frames;
  447. s->low_delay = s1->low_delay;
  448. s->dropable = s1->dropable;
  449. //DivX handling (doesn't work)
  450. s->divx_packed = s1->divx_packed;
  451. if(s1->bitstream_buffer){
  452. if (s1->bitstream_buffer_size + FF_INPUT_BUFFER_PADDING_SIZE > s->allocated_bitstream_buffer_size)
  453. av_fast_malloc(&s->bitstream_buffer, &s->allocated_bitstream_buffer_size, s1->allocated_bitstream_buffer_size);
  454. s->bitstream_buffer_size = s1->bitstream_buffer_size;
  455. memcpy(s->bitstream_buffer, s1->bitstream_buffer, s1->bitstream_buffer_size);
  456. memset(s->bitstream_buffer+s->bitstream_buffer_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  457. }
  458. //MPEG2/interlacing info
  459. memcpy(&s->progressive_sequence, &s1->progressive_sequence, (char*)&s1->rtp_mode - (char*)&s1->progressive_sequence);
  460. if(!s1->first_field){
  461. s->last_pict_type= s1->pict_type;
  462. if (s1->current_picture_ptr) s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f.quality;
  463. if (s1->pict_type != AV_PICTURE_TYPE_B) {
  464. s->last_non_b_pict_type= s1->pict_type;
  465. }
  466. }
  467. return 0;
  468. }
  469. /**
  470. * sets the given MpegEncContext to common defaults (same for encoding and decoding).
  471. * the changed fields will not depend upon the prior state of the MpegEncContext.
  472. */
  473. void MPV_common_defaults(MpegEncContext *s){
  474. s->y_dc_scale_table=
  475. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  476. s->chroma_qscale_table= ff_default_chroma_qscale_table;
  477. s->progressive_frame= 1;
  478. s->progressive_sequence= 1;
  479. s->picture_structure= PICT_FRAME;
  480. s->coded_picture_number = 0;
  481. s->picture_number = 0;
  482. s->input_picture_number = 0;
  483. s->picture_in_gop_number = 0;
  484. s->f_code = 1;
  485. s->b_code = 1;
  486. s->picture_range_start = 0;
  487. s->picture_range_end = MAX_PICTURE_COUNT;
  488. }
  489. /**
  490. * sets the given MpegEncContext to defaults for decoding.
  491. * the changed fields will not depend upon the prior state of the MpegEncContext.
  492. */
  493. void MPV_decode_defaults(MpegEncContext *s){
  494. MPV_common_defaults(s);
  495. }
  496. /**
  497. * init common structure for both encoder and decoder.
  498. * this assumes that some variables like width/height are already set
  499. */
  500. av_cold int MPV_common_init(MpegEncContext *s)
  501. {
  502. int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y,
  503. threads = (s->encoding ||
  504. (HAVE_THREADS &&
  505. s->avctx->active_thread_type & FF_THREAD_SLICE)) ?
  506. s->avctx->thread_count : 1;
  507. if(s->codec_id == CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
  508. s->mb_height = (s->height + 31) / 32 * 2;
  509. else if (s->codec_id != CODEC_ID_H264)
  510. s->mb_height = (s->height + 15) / 16;
  511. if(s->avctx->pix_fmt == PIX_FMT_NONE){
  512. av_log(s->avctx, AV_LOG_ERROR, "decoding to PIX_FMT_NONE is not supported.\n");
  513. return -1;
  514. }
  515. if((s->encoding || (s->avctx->active_thread_type & FF_THREAD_SLICE)) &&
  516. (s->avctx->thread_count > MAX_THREADS || (s->avctx->thread_count > s->mb_height && s->mb_height))){
  517. int max_threads = FFMIN(MAX_THREADS, s->mb_height);
  518. av_log(s->avctx, AV_LOG_WARNING, "too many threads (%d), reducing to %d\n",
  519. s->avctx->thread_count, max_threads);
  520. threads = max_threads;
  521. }
  522. if((s->width || s->height) && av_image_check_size(s->width, s->height, 0, s->avctx))
  523. return -1;
  524. ff_dct_common_init(s);
  525. s->flags= s->avctx->flags;
  526. s->flags2= s->avctx->flags2;
  527. s->mb_width = (s->width + 15) / 16;
  528. s->mb_stride = s->mb_width + 1;
  529. s->b8_stride = s->mb_width*2 + 1;
  530. s->b4_stride = s->mb_width*4 + 1;
  531. mb_array_size= s->mb_height * s->mb_stride;
  532. mv_table_size= (s->mb_height+2) * s->mb_stride + 1;
  533. /* set chroma shifts */
  534. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,&(s->chroma_x_shift),
  535. &(s->chroma_y_shift) );
  536. /* set default edge pos, will be overriden in decode_header if needed */
  537. s->h_edge_pos= s->mb_width*16;
  538. s->v_edge_pos= s->mb_height*16;
  539. s->mb_num = s->mb_width * s->mb_height;
  540. s->block_wrap[0]=
  541. s->block_wrap[1]=
  542. s->block_wrap[2]=
  543. s->block_wrap[3]= s->b8_stride;
  544. s->block_wrap[4]=
  545. s->block_wrap[5]= s->mb_stride;
  546. y_size = s->b8_stride * (2 * s->mb_height + 1);
  547. c_size = s->mb_stride * (s->mb_height + 1);
  548. yc_size = y_size + 2 * c_size;
  549. /* convert fourcc to upper case */
  550. s->codec_tag = ff_toupper4(s->avctx->codec_tag);
  551. s->stream_codec_tag = ff_toupper4(s->avctx->stream_codec_tag);
  552. s->avctx->coded_frame= (AVFrame*)&s->current_picture;
  553. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num+1)*sizeof(int), fail) //error ressilience code looks cleaner with this
  554. for(y=0; y<s->mb_height; y++){
  555. for(x=0; x<s->mb_width; x++){
  556. s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
  557. }
  558. }
  559. s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width; //FIXME really needed?
  560. if (s->encoding) {
  561. /* Allocate MV tables */
  562. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  563. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  564. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  565. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  566. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  567. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
  568. s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
  569. s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
  570. s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
  571. s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
  572. s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
  573. s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
  574. if(s->msmpeg4_version){
  575. FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int), fail);
  576. }
  577. FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
  578. /* Allocate MB type table */
  579. FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type , mb_array_size * sizeof(uint16_t), fail) //needed for encoding
  580. FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
  581. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix , 64*32 * sizeof(int), fail)
  582. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix , 64*32 * sizeof(int), fail)
  583. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix , 64*32 * sizeof(int), fail)
  584. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t), fail)
  585. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix16, 64*32*2 * sizeof(uint16_t), fail)
  586. FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t), fail)
  587. FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*), fail)
  588. FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*), fail)
  589. if(s->avctx->noise_reduction){
  590. FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset, 2 * 64 * sizeof(uint16_t), fail)
  591. }
  592. }
  593. s->picture_count = MAX_PICTURE_COUNT * FFMAX(1, s->avctx->thread_count);
  594. FF_ALLOCZ_OR_GOTO(s->avctx, s->picture, s->picture_count * sizeof(Picture), fail)
  595. for(i = 0; i < s->picture_count; i++) {
  596. avcodec_get_frame_defaults((AVFrame *)&s->picture[i]);
  597. }
  598. FF_ALLOCZ_OR_GOTO(s->avctx, s->error_status_table, mb_array_size*sizeof(uint8_t), fail)
  599. if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
  600. /* interlaced direct mode decoding tables */
  601. for(i=0; i<2; i++){
  602. int j, k;
  603. for(j=0; j<2; j++){
  604. for(k=0; k<2; k++){
  605. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_mv_table_base[i][j][k], mv_table_size * 2 * sizeof(int16_t), fail)
  606. s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1;
  607. }
  608. FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
  609. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
  610. s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]+ s->mb_stride + 1;
  611. }
  612. FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
  613. }
  614. }
  615. if (s->out_format == FMT_H263) {
  616. /* cbp values */
  617. FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
  618. s->coded_block= s->coded_block_base + s->b8_stride + 1;
  619. /* cbp, ac_pred, pred_dir */
  620. FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table , mb_array_size * sizeof(uint8_t), fail)
  621. FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail)
  622. }
  623. if (s->h263_pred || s->h263_plus || !s->encoding) {
  624. /* dc values */
  625. //MN: we need these for error resilience of intra-frames
  626. FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
  627. s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
  628. s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
  629. s->dc_val[2] = s->dc_val[1] + c_size;
  630. for(i=0;i<yc_size;i++)
  631. s->dc_val_base[i] = 1024;
  632. }
  633. /* which mb is a intra block */
  634. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
  635. memset(s->mbintra_table, 1, mb_array_size);
  636. /* init macroblock skip table */
  637. FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size+2, fail);
  638. //Note the +1 is for a quicker mpeg4 slice_end detection
  639. FF_ALLOCZ_OR_GOTO(s->avctx, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE, fail);
  640. s->parse_context.state= -1;
  641. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  642. s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  643. s->visualization_buffer[1] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  644. s->visualization_buffer[2] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
  645. }
  646. s->context_initialized = 1;
  647. s->thread_context[0]= s;
  648. if (s->encoding || (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_SLICE)) {
  649. for(i=1; i<threads; i++){
  650. s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
  651. memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
  652. }
  653. for(i=0; i<threads; i++){
  654. if(init_duplicate_context(s->thread_context[i], s) < 0)
  655. goto fail;
  656. s->thread_context[i]->start_mb_y= (s->mb_height*(i ) + s->avctx->thread_count/2) / s->avctx->thread_count;
  657. s->thread_context[i]->end_mb_y = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;
  658. }
  659. } else {
  660. if(init_duplicate_context(s, s) < 0) goto fail;
  661. s->start_mb_y = 0;
  662. s->end_mb_y = s->mb_height;
  663. }
  664. return 0;
  665. fail:
  666. MPV_common_end(s);
  667. return -1;
  668. }
  669. /* init common structure for both encoder and decoder */
  670. void MPV_common_end(MpegEncContext *s)
  671. {
  672. int i, j, k;
  673. if (s->encoding || (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_SLICE)) {
  674. for(i=0; i<s->avctx->thread_count; i++){
  675. free_duplicate_context(s->thread_context[i]);
  676. }
  677. for(i=1; i<s->avctx->thread_count; i++){
  678. av_freep(&s->thread_context[i]);
  679. }
  680. } else free_duplicate_context(s);
  681. av_freep(&s->parse_context.buffer);
  682. s->parse_context.buffer_size=0;
  683. av_freep(&s->mb_type);
  684. av_freep(&s->p_mv_table_base);
  685. av_freep(&s->b_forw_mv_table_base);
  686. av_freep(&s->b_back_mv_table_base);
  687. av_freep(&s->b_bidir_forw_mv_table_base);
  688. av_freep(&s->b_bidir_back_mv_table_base);
  689. av_freep(&s->b_direct_mv_table_base);
  690. s->p_mv_table= NULL;
  691. s->b_forw_mv_table= NULL;
  692. s->b_back_mv_table= NULL;
  693. s->b_bidir_forw_mv_table= NULL;
  694. s->b_bidir_back_mv_table= NULL;
  695. s->b_direct_mv_table= NULL;
  696. for(i=0; i<2; i++){
  697. for(j=0; j<2; j++){
  698. for(k=0; k<2; k++){
  699. av_freep(&s->b_field_mv_table_base[i][j][k]);
  700. s->b_field_mv_table[i][j][k]=NULL;
  701. }
  702. av_freep(&s->b_field_select_table[i][j]);
  703. av_freep(&s->p_field_mv_table_base[i][j]);
  704. s->p_field_mv_table[i][j]=NULL;
  705. }
  706. av_freep(&s->p_field_select_table[i]);
  707. }
  708. av_freep(&s->dc_val_base);
  709. av_freep(&s->coded_block_base);
  710. av_freep(&s->mbintra_table);
  711. av_freep(&s->cbp_table);
  712. av_freep(&s->pred_dir_table);
  713. av_freep(&s->mbskip_table);
  714. av_freep(&s->prev_pict_types);
  715. av_freep(&s->bitstream_buffer);
  716. s->allocated_bitstream_buffer_size=0;
  717. av_freep(&s->avctx->stats_out);
  718. av_freep(&s->ac_stats);
  719. av_freep(&s->error_status_table);
  720. av_freep(&s->mb_index2xy);
  721. av_freep(&s->lambda_table);
  722. if(s->q_chroma_intra_matrix != s->q_intra_matrix ) av_freep(&s->q_chroma_intra_matrix);
  723. if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
  724. s->q_chroma_intra_matrix= NULL;
  725. s->q_chroma_intra_matrix16= NULL;
  726. av_freep(&s->q_intra_matrix);
  727. av_freep(&s->q_inter_matrix);
  728. av_freep(&s->q_intra_matrix16);
  729. av_freep(&s->q_inter_matrix16);
  730. av_freep(&s->input_picture);
  731. av_freep(&s->reordered_input_picture);
  732. av_freep(&s->dct_offset);
  733. if(s->picture && !s->avctx->is_copy){
  734. for(i=0; i<s->picture_count; i++){
  735. free_picture(s, &s->picture[i]);
  736. }
  737. }
  738. av_freep(&s->picture);
  739. s->context_initialized = 0;
  740. s->last_picture_ptr=
  741. s->next_picture_ptr=
  742. s->current_picture_ptr= NULL;
  743. s->linesize= s->uvlinesize= 0;
  744. for(i=0; i<3; i++)
  745. av_freep(&s->visualization_buffer[i]);
  746. if(!(s->avctx->active_thread_type&FF_THREAD_FRAME))
  747. avcodec_default_free_buffers(s->avctx);
  748. }
  749. void init_rl(RLTable *rl, uint8_t static_store[2][2*MAX_RUN + MAX_LEVEL + 3])
  750. {
  751. int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
  752. uint8_t index_run[MAX_RUN+1];
  753. int last, run, level, start, end, i;
  754. /* If table is static, we can quit if rl->max_level[0] is not NULL */
  755. if(static_store && rl->max_level[0])
  756. return;
  757. /* compute max_level[], max_run[] and index_run[] */
  758. for(last=0;last<2;last++) {
  759. if (last == 0) {
  760. start = 0;
  761. end = rl->last;
  762. } else {
  763. start = rl->last;
  764. end = rl->n;
  765. }
  766. memset(max_level, 0, MAX_RUN + 1);
  767. memset(max_run, 0, MAX_LEVEL + 1);
  768. memset(index_run, rl->n, MAX_RUN + 1);
  769. for(i=start;i<end;i++) {
  770. run = rl->table_run[i];
  771. level = rl->table_level[i];
  772. if (index_run[run] == rl->n)
  773. index_run[run] = i;
  774. if (level > max_level[run])
  775. max_level[run] = level;
  776. if (run > max_run[level])
  777. max_run[level] = run;
  778. }
  779. if(static_store)
  780. rl->max_level[last] = static_store[last];
  781. else
  782. rl->max_level[last] = av_malloc(MAX_RUN + 1);
  783. memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
  784. if(static_store)
  785. rl->max_run[last] = static_store[last] + MAX_RUN + 1;
  786. else
  787. rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
  788. memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
  789. if(static_store)
  790. rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
  791. else
  792. rl->index_run[last] = av_malloc(MAX_RUN + 1);
  793. memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
  794. }
  795. }
  796. void init_vlc_rl(RLTable *rl)
  797. {
  798. int i, q;
  799. for(q=0; q<32; q++){
  800. int qmul= q*2;
  801. int qadd= (q-1)|1;
  802. if(q==0){
  803. qmul=1;
  804. qadd=0;
  805. }
  806. for(i=0; i<rl->vlc.table_size; i++){
  807. int code= rl->vlc.table[i][0];
  808. int len = rl->vlc.table[i][1];
  809. int level, run;
  810. if(len==0){ // illegal code
  811. run= 66;
  812. level= MAX_LEVEL;
  813. }else if(len<0){ //more bits needed
  814. run= 0;
  815. level= code;
  816. }else{
  817. if(code==rl->n){ //esc
  818. run= 66;
  819. level= 0;
  820. }else{
  821. run= rl->table_run [code] + 1;
  822. level= rl->table_level[code] * qmul + qadd;
  823. if(code >= rl->last) run+=192;
  824. }
  825. }
  826. rl->rl_vlc[q][i].len= len;
  827. rl->rl_vlc[q][i].level= level;
  828. rl->rl_vlc[q][i].run= run;
  829. }
  830. }
  831. }
  832. void ff_release_unused_pictures(MpegEncContext *s, int remove_current)
  833. {
  834. int i;
  835. /* release non reference frames */
  836. for(i=0; i<s->picture_count; i++){
  837. if (s->picture[i].f.data[0] && !s->picture[i].f.reference
  838. && (!s->picture[i].owner2 || s->picture[i].owner2 == s)
  839. && (remove_current || &s->picture[i] != s->current_picture_ptr)
  840. /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/){
  841. free_frame_buffer(s, &s->picture[i]);
  842. }
  843. }
  844. }
  845. int ff_find_unused_picture(MpegEncContext *s, int shared){
  846. int i;
  847. if(shared){
  848. for(i=s->picture_range_start; i<s->picture_range_end; i++){
  849. if (s->picture[i].f.data[0] == NULL && s->picture[i].f.type == 0)
  850. return i;
  851. }
  852. }else{
  853. for(i=s->picture_range_start; i<s->picture_range_end; i++){
  854. if (s->picture[i].f.data[0] == NULL && s->picture[i].f.type != 0)
  855. return i; //FIXME
  856. }
  857. for(i=s->picture_range_start; i<s->picture_range_end; i++){
  858. if (s->picture[i].f.data[0] == NULL)
  859. return i;
  860. }
  861. }
  862. av_log(s->avctx, AV_LOG_FATAL, "Internal error, picture buffer overflow\n");
  863. /* We could return -1, but the codec would crash trying to draw into a
  864. * non-existing frame anyway. This is safer than waiting for a random crash.
  865. * Also the return of this is never useful, an encoder must only allocate
  866. * as much as allowed in the specification. This has no relationship to how
  867. * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
  868. * enough for such valid streams).
  869. * Plus, a decoder has to check stream validity and remove frames if too
  870. * many reference frames are around. Waiting for "OOM" is not correct at
  871. * all. Similarly, missing reference frames have to be replaced by
  872. * interpolated/MC frames, anything else is a bug in the codec ...
  873. */
  874. abort();
  875. return -1;
  876. }
  877. static void update_noise_reduction(MpegEncContext *s){
  878. int intra, i;
  879. for(intra=0; intra<2; intra++){
  880. if(s->dct_count[intra] > (1<<16)){
  881. for(i=0; i<64; i++){
  882. s->dct_error_sum[intra][i] >>=1;
  883. }
  884. s->dct_count[intra] >>= 1;
  885. }
  886. for(i=0; i<64; i++){
  887. 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);
  888. }
  889. }
  890. }
  891. /**
  892. * generic function for encode/decode called after coding/decoding the header and before a frame is coded/decoded
  893. */
  894. int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
  895. {
  896. int i;
  897. Picture *pic;
  898. s->mb_skipped = 0;
  899. assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
  900. /* mark&release old frames */
  901. if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr && s->last_picture_ptr != s->next_picture_ptr && s->last_picture_ptr->f.data[0]) {
  902. if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
  903. if (s->last_picture_ptr->owner2 == s)
  904. free_frame_buffer(s, s->last_picture_ptr);
  905. /* release forgotten pictures */
  906. /* if(mpeg124/h263) */
  907. if(!s->encoding){
  908. for(i=0; i<s->picture_count; i++){
  909. if (s->picture[i].owner2 == s && s->picture[i].f.data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].f.reference) {
  910. if (!(avctx->active_thread_type & FF_THREAD_FRAME))
  911. av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
  912. free_frame_buffer(s, &s->picture[i]);
  913. }
  914. }
  915. }
  916. }
  917. }
  918. if(!s->encoding){
  919. ff_release_unused_pictures(s, 1);
  920. if (s->current_picture_ptr && s->current_picture_ptr->f.data[0] == NULL)
  921. pic= s->current_picture_ptr; //we already have a unused image (maybe it was set before reading the header)
  922. else{
  923. i= ff_find_unused_picture(s, 0);
  924. pic= &s->picture[i];
  925. }
  926. pic->f.reference = 0;
  927. if (!s->dropable){
  928. if (s->codec_id == CODEC_ID_H264)
  929. pic->f.reference = s->picture_structure;
  930. else if (s->pict_type != AV_PICTURE_TYPE_B)
  931. pic->f.reference = 3;
  932. }
  933. pic->f.coded_picture_number = s->coded_picture_number++;
  934. if(ff_alloc_picture(s, pic, 0) < 0)
  935. return -1;
  936. s->current_picture_ptr= pic;
  937. //FIXME use only the vars from current_pic
  938. s->current_picture_ptr->f.top_field_first = s->top_field_first;
  939. if(s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO) {
  940. if(s->picture_structure != PICT_FRAME)
  941. s->current_picture_ptr->f.top_field_first = (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
  942. }
  943. s->current_picture_ptr->f.interlaced_frame = !s->progressive_frame && !s->progressive_sequence;
  944. s->current_picture_ptr->field_picture = s->picture_structure != PICT_FRAME;
  945. }
  946. s->current_picture_ptr->f.pict_type = s->pict_type;
  947. // if(s->flags && CODEC_FLAG_QSCALE)
  948. // s->current_picture_ptr->quality= s->new_picture_ptr->quality;
  949. s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  950. ff_copy_picture(&s->current_picture, s->current_picture_ptr);
  951. if (s->pict_type != AV_PICTURE_TYPE_B) {
  952. s->last_picture_ptr= s->next_picture_ptr;
  953. if(!s->dropable)
  954. s->next_picture_ptr= s->current_picture_ptr;
  955. }
  956. /* av_log(s->avctx, AV_LOG_DEBUG, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n", s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
  957. s->last_picture_ptr ? s->last_picture_ptr->f.data[0] : NULL,
  958. s->next_picture_ptr ? s->next_picture_ptr->f.data[0] : NULL,
  959. s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
  960. s->pict_type, s->dropable);*/
  961. if(s->codec_id != CODEC_ID_H264){
  962. if ((s->last_picture_ptr == NULL || s->last_picture_ptr->f.data[0] == NULL) &&
  963. (s->pict_type!=AV_PICTURE_TYPE_I || s->picture_structure != PICT_FRAME)){
  964. if (s->pict_type != AV_PICTURE_TYPE_I)
  965. av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
  966. else if (s->picture_structure != PICT_FRAME)
  967. av_log(avctx, AV_LOG_INFO, "allocate dummy last picture for field based first keyframe\n");
  968. /* Allocate a dummy frame */
  969. i= ff_find_unused_picture(s, 0);
  970. s->last_picture_ptr= &s->picture[i];
  971. s->last_picture_ptr->f.key_frame = 0;
  972. if(ff_alloc_picture(s, s->last_picture_ptr, 0) < 0)
  973. return -1;
  974. ff_thread_report_progress((AVFrame*)s->last_picture_ptr, INT_MAX, 0);
  975. ff_thread_report_progress((AVFrame*)s->last_picture_ptr, INT_MAX, 1);
  976. }
  977. if ((s->next_picture_ptr == NULL || s->next_picture_ptr->f.data[0] == NULL) && s->pict_type == AV_PICTURE_TYPE_B) {
  978. /* Allocate a dummy frame */
  979. i= ff_find_unused_picture(s, 0);
  980. s->next_picture_ptr= &s->picture[i];
  981. s->next_picture_ptr->f.key_frame = 0;
  982. if(ff_alloc_picture(s, s->next_picture_ptr, 0) < 0)
  983. return -1;
  984. ff_thread_report_progress((AVFrame*)s->next_picture_ptr, INT_MAX, 0);
  985. ff_thread_report_progress((AVFrame*)s->next_picture_ptr, INT_MAX, 1);
  986. }
  987. }
  988. if(s->last_picture_ptr) ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  989. if(s->next_picture_ptr) ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  990. assert(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr && s->last_picture_ptr->f.data[0]));
  991. if(s->picture_structure!=PICT_FRAME && s->out_format != FMT_H264){
  992. int i;
  993. for(i=0; i<4; i++){
  994. if(s->picture_structure == PICT_BOTTOM_FIELD){
  995. s->current_picture.f.data[i] += s->current_picture.f.linesize[i];
  996. }
  997. s->current_picture.f.linesize[i] *= 2;
  998. s->last_picture.f.linesize[i] *= 2;
  999. s->next_picture.f.linesize[i] *= 2;
  1000. }
  1001. }
  1002. s->error_recognition= avctx->error_recognition;
  1003. /* set dequantizer, we can't do it during init as it might change for mpeg4
  1004. and we can't do it in the header decode as init is not called for mpeg4 there yet */
  1005. if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){
  1006. s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
  1007. s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
  1008. }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
  1009. s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
  1010. s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
  1011. }else{
  1012. s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
  1013. s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
  1014. }
  1015. if(s->dct_error_sum){
  1016. assert(s->avctx->noise_reduction && s->encoding);
  1017. update_noise_reduction(s);
  1018. }
  1019. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1020. return ff_xvmc_field_start(s, avctx);
  1021. return 0;
  1022. }
  1023. /* generic function for encode/decode called after a frame has been coded/decoded */
  1024. void MPV_frame_end(MpegEncContext *s)
  1025. {
  1026. int i;
  1027. /* redraw edges for the frame if decoding didn't complete */
  1028. //just to make sure that all data is rendered.
  1029. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1030. ff_xvmc_field_end(s);
  1031. }else if((s->error_count || s->encoding || !(s->avctx->codec->capabilities&CODEC_CAP_DRAW_HORIZ_BAND))
  1032. && !s->avctx->hwaccel
  1033. && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  1034. && s->unrestricted_mv
  1035. && s->current_picture.f.reference
  1036. && !s->intra_only
  1037. && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  1038. int hshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_w;
  1039. int vshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_h;
  1040. s->dsp.draw_edges(s->current_picture.f.data[0], s->linesize,
  1041. s->h_edge_pos , s->v_edge_pos,
  1042. EDGE_WIDTH , EDGE_WIDTH , EDGE_TOP | EDGE_BOTTOM);
  1043. s->dsp.draw_edges(s->current_picture.f.data[1], s->uvlinesize,
  1044. s->h_edge_pos>>hshift, s->v_edge_pos>>vshift,
  1045. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, EDGE_TOP | EDGE_BOTTOM);
  1046. s->dsp.draw_edges(s->current_picture.f.data[2], s->uvlinesize,
  1047. s->h_edge_pos>>hshift, s->v_edge_pos>>vshift,
  1048. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, EDGE_TOP | EDGE_BOTTOM);
  1049. }
  1050. emms_c();
  1051. s->last_pict_type = s->pict_type;
  1052. s->last_lambda_for[s->pict_type] = s->current_picture_ptr->f.quality;
  1053. if(s->pict_type!=AV_PICTURE_TYPE_B){
  1054. s->last_non_b_pict_type= s->pict_type;
  1055. }
  1056. #if 0
  1057. /* copy back current_picture variables */
  1058. for(i=0; i<MAX_PICTURE_COUNT; i++){
  1059. if(s->picture[i].f.data[0] == s->current_picture.f.data[0]){
  1060. s->picture[i]= s->current_picture;
  1061. break;
  1062. }
  1063. }
  1064. assert(i<MAX_PICTURE_COUNT);
  1065. #endif
  1066. if(s->encoding){
  1067. /* release non-reference frames */
  1068. for(i=0; i<s->picture_count; i++){
  1069. if (s->picture[i].f.data[0] && !s->picture[i].f.reference /*&& s->picture[i].type != FF_BUFFER_TYPE_SHARED*/) {
  1070. free_frame_buffer(s, &s->picture[i]);
  1071. }
  1072. }
  1073. }
  1074. // clear copies, to avoid confusion
  1075. #if 0
  1076. memset(&s->last_picture, 0, sizeof(Picture));
  1077. memset(&s->next_picture, 0, sizeof(Picture));
  1078. memset(&s->current_picture, 0, sizeof(Picture));
  1079. #endif
  1080. s->avctx->coded_frame= (AVFrame*)s->current_picture_ptr;
  1081. if (s->codec_id != CODEC_ID_H264 && s->current_picture.f.reference) {
  1082. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, s->mb_height-1, 0);
  1083. }
  1084. }
  1085. /**
  1086. * draws an line from (ex, ey) -> (sx, sy).
  1087. * @param w width of the image
  1088. * @param h height of the image
  1089. * @param stride stride/linesize of the image
  1090. * @param color color of the arrow
  1091. */
  1092. static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  1093. int x, y, fr, f;
  1094. sx= av_clip(sx, 0, w-1);
  1095. sy= av_clip(sy, 0, h-1);
  1096. ex= av_clip(ex, 0, w-1);
  1097. ey= av_clip(ey, 0, h-1);
  1098. buf[sy*stride + sx]+= color;
  1099. if(FFABS(ex - sx) > FFABS(ey - sy)){
  1100. if(sx > ex){
  1101. FFSWAP(int, sx, ex);
  1102. FFSWAP(int, sy, ey);
  1103. }
  1104. buf+= sx + sy*stride;
  1105. ex-= sx;
  1106. f= ((ey-sy)<<16)/ex;
  1107. for(x= 0; x <= ex; x++){
  1108. y = (x*f)>>16;
  1109. fr= (x*f)&0xFFFF;
  1110. buf[ y *stride + x]+= (color*(0x10000-fr))>>16;
  1111. buf[(y+1)*stride + x]+= (color* fr )>>16;
  1112. }
  1113. }else{
  1114. if(sy > ey){
  1115. FFSWAP(int, sx, ex);
  1116. FFSWAP(int, sy, ey);
  1117. }
  1118. buf+= sx + sy*stride;
  1119. ey-= sy;
  1120. if(ey) f= ((ex-sx)<<16)/ey;
  1121. else f= 0;
  1122. for(y= 0; y <= ey; y++){
  1123. x = (y*f)>>16;
  1124. fr= (y*f)&0xFFFF;
  1125. buf[y*stride + x ]+= (color*(0x10000-fr))>>16;
  1126. buf[y*stride + x+1]+= (color* fr )>>16;
  1127. }
  1128. }
  1129. }
  1130. /**
  1131. * draws an arrow from (ex, ey) -> (sx, sy).
  1132. * @param w width of the image
  1133. * @param h height of the image
  1134. * @param stride stride/linesize of the image
  1135. * @param color color of the arrow
  1136. */
  1137. static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
  1138. int dx,dy;
  1139. sx= av_clip(sx, -100, w+100);
  1140. sy= av_clip(sy, -100, h+100);
  1141. ex= av_clip(ex, -100, w+100);
  1142. ey= av_clip(ey, -100, h+100);
  1143. dx= ex - sx;
  1144. dy= ey - sy;
  1145. if(dx*dx + dy*dy > 3*3){
  1146. int rx= dx + dy;
  1147. int ry= -dx + dy;
  1148. int length= ff_sqrt((rx*rx + ry*ry)<<8);
  1149. //FIXME subpixel accuracy
  1150. rx= ROUNDED_DIV(rx*3<<4, length);
  1151. ry= ROUNDED_DIV(ry*3<<4, length);
  1152. draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
  1153. draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
  1154. }
  1155. draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
  1156. }
  1157. /**
  1158. * prints debuging info for the given picture.
  1159. */
  1160. void ff_print_debug_info(MpegEncContext *s, AVFrame *pict){
  1161. if(s->avctx->hwaccel || !pict || !pict->mb_type) return;
  1162. if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
  1163. int x,y;
  1164. av_log(s->avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
  1165. av_get_picture_type_char(pict->pict_type));
  1166. for(y=0; y<s->mb_height; y++){
  1167. for(x=0; x<s->mb_width; x++){
  1168. if(s->avctx->debug&FF_DEBUG_SKIP){
  1169. int count= s->mbskip_table[x + y*s->mb_stride];
  1170. if(count>9) count=9;
  1171. av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
  1172. }
  1173. if(s->avctx->debug&FF_DEBUG_QP){
  1174. av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
  1175. }
  1176. if(s->avctx->debug&FF_DEBUG_MB_TYPE){
  1177. int mb_type= pict->mb_type[x + y*s->mb_stride];
  1178. //Type & MV direction
  1179. if(IS_PCM(mb_type))
  1180. av_log(s->avctx, AV_LOG_DEBUG, "P");
  1181. else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
  1182. av_log(s->avctx, AV_LOG_DEBUG, "A");
  1183. else if(IS_INTRA4x4(mb_type))
  1184. av_log(s->avctx, AV_LOG_DEBUG, "i");
  1185. else if(IS_INTRA16x16(mb_type))
  1186. av_log(s->avctx, AV_LOG_DEBUG, "I");
  1187. else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
  1188. av_log(s->avctx, AV_LOG_DEBUG, "d");
  1189. else if(IS_DIRECT(mb_type))
  1190. av_log(s->avctx, AV_LOG_DEBUG, "D");
  1191. else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
  1192. av_log(s->avctx, AV_LOG_DEBUG, "g");
  1193. else if(IS_GMC(mb_type))
  1194. av_log(s->avctx, AV_LOG_DEBUG, "G");
  1195. else if(IS_SKIP(mb_type))
  1196. av_log(s->avctx, AV_LOG_DEBUG, "S");
  1197. else if(!USES_LIST(mb_type, 1))
  1198. av_log(s->avctx, AV_LOG_DEBUG, ">");
  1199. else if(!USES_LIST(mb_type, 0))
  1200. av_log(s->avctx, AV_LOG_DEBUG, "<");
  1201. else{
  1202. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1203. av_log(s->avctx, AV_LOG_DEBUG, "X");
  1204. }
  1205. //segmentation
  1206. if(IS_8X8(mb_type))
  1207. av_log(s->avctx, AV_LOG_DEBUG, "+");
  1208. else if(IS_16X8(mb_type))
  1209. av_log(s->avctx, AV_LOG_DEBUG, "-");
  1210. else if(IS_8X16(mb_type))
  1211. av_log(s->avctx, AV_LOG_DEBUG, "|");
  1212. else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
  1213. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1214. else
  1215. av_log(s->avctx, AV_LOG_DEBUG, "?");
  1216. if(IS_INTERLACED(mb_type))
  1217. av_log(s->avctx, AV_LOG_DEBUG, "=");
  1218. else
  1219. av_log(s->avctx, AV_LOG_DEBUG, " ");
  1220. }
  1221. // av_log(s->avctx, AV_LOG_DEBUG, " ");
  1222. }
  1223. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1224. }
  1225. }
  1226. if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
  1227. const int shift= 1 + s->quarter_sample;
  1228. int mb_y;
  1229. uint8_t *ptr;
  1230. int i;
  1231. int h_chroma_shift, v_chroma_shift, block_height;
  1232. const int width = s->avctx->width;
  1233. const int height= s->avctx->height;
  1234. const int mv_sample_log2= 4 - pict->motion_subsample_log2;
  1235. const int mv_stride= (s->mb_width << mv_sample_log2) + (s->codec_id == CODEC_ID_H264 ? 0 : 1);
  1236. s->low_delay=0; //needed to see the vectors without trashing the buffers
  1237. avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  1238. for(i=0; i<3; i++){
  1239. memcpy(s->visualization_buffer[i], pict->data[i], (i==0) ? pict->linesize[i]*height:pict->linesize[i]*height >> v_chroma_shift);
  1240. pict->data[i]= s->visualization_buffer[i];
  1241. }
  1242. pict->type= FF_BUFFER_TYPE_COPY;
  1243. pict->opaque= NULL;
  1244. ptr= pict->data[0];
  1245. block_height = 16>>v_chroma_shift;
  1246. for(mb_y=0; mb_y<s->mb_height; mb_y++){
  1247. int mb_x;
  1248. for(mb_x=0; mb_x<s->mb_width; mb_x++){
  1249. const int mb_index= mb_x + mb_y*s->mb_stride;
  1250. if((s->avctx->debug_mv) && pict->motion_val){
  1251. int type;
  1252. for(type=0; type<3; type++){
  1253. int direction = 0;
  1254. switch (type) {
  1255. case 0: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_P_FOR)) || (pict->pict_type!=AV_PICTURE_TYPE_P))
  1256. continue;
  1257. direction = 0;
  1258. break;
  1259. case 1: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_FOR)) || (pict->pict_type!=AV_PICTURE_TYPE_B))
  1260. continue;
  1261. direction = 0;
  1262. break;
  1263. case 2: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_BACK)) || (pict->pict_type!=AV_PICTURE_TYPE_B))
  1264. continue;
  1265. direction = 1;
  1266. break;
  1267. }
  1268. if(!USES_LIST(pict->mb_type[mb_index], direction))
  1269. continue;
  1270. if(IS_8X8(pict->mb_type[mb_index])){
  1271. int i;
  1272. for(i=0; i<4; i++){
  1273. int sx= mb_x*16 + 4 + 8*(i&1);
  1274. int sy= mb_y*16 + 4 + 8*(i>>1);
  1275. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1276. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1277. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1278. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1279. }
  1280. }else if(IS_16X8(pict->mb_type[mb_index])){
  1281. int i;
  1282. for(i=0; i<2; i++){
  1283. int sx=mb_x*16 + 8;
  1284. int sy=mb_y*16 + 4 + 8*i;
  1285. int xy= (mb_x*2 + (mb_y*2 + i)*mv_stride) << (mv_sample_log2-1);
  1286. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1287. int my=(pict->motion_val[direction][xy][1]>>shift);
  1288. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1289. my*=2;
  1290. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1291. }
  1292. }else if(IS_8X16(pict->mb_type[mb_index])){
  1293. int i;
  1294. for(i=0; i<2; i++){
  1295. int sx=mb_x*16 + 4 + 8*i;
  1296. int sy=mb_y*16 + 8;
  1297. int xy= (mb_x*2 + i + mb_y*2*mv_stride) << (mv_sample_log2-1);
  1298. int mx=(pict->motion_val[direction][xy][0]>>shift);
  1299. int my=(pict->motion_val[direction][xy][1]>>shift);
  1300. if(IS_INTERLACED(pict->mb_type[mb_index]))
  1301. my*=2;
  1302. draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
  1303. }
  1304. }else{
  1305. int sx= mb_x*16 + 8;
  1306. int sy= mb_y*16 + 8;
  1307. int xy= (mb_x + mb_y*mv_stride) << mv_sample_log2;
  1308. int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
  1309. int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
  1310. draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
  1311. }
  1312. }
  1313. }
  1314. if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){
  1315. uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL;
  1316. int y;
  1317. for(y=0; y<block_height; y++){
  1318. *(uint64_t*)(pict->data[1] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[1])= c;
  1319. *(uint64_t*)(pict->data[2] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[2])= c;
  1320. }
  1321. }
  1322. if((s->avctx->debug&FF_DEBUG_VIS_MB_TYPE) && pict->motion_val){
  1323. int mb_type= pict->mb_type[mb_index];
  1324. uint64_t u,v;
  1325. int y;
  1326. #define COLOR(theta, r)\
  1327. u= (int)(128 + r*cos(theta*3.141592/180));\
  1328. v= (int)(128 + r*sin(theta*3.141592/180));
  1329. u=v=128;
  1330. if(IS_PCM(mb_type)){
  1331. COLOR(120,48)
  1332. }else if((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) || IS_INTRA16x16(mb_type)){
  1333. COLOR(30,48)
  1334. }else if(IS_INTRA4x4(mb_type)){
  1335. COLOR(90,48)
  1336. }else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type)){
  1337. // COLOR(120,48)
  1338. }else if(IS_DIRECT(mb_type)){
  1339. COLOR(150,48)
  1340. }else if(IS_GMC(mb_type) && IS_SKIP(mb_type)){
  1341. COLOR(170,48)
  1342. }else if(IS_GMC(mb_type)){
  1343. COLOR(190,48)
  1344. }else if(IS_SKIP(mb_type)){
  1345. // COLOR(180,48)
  1346. }else if(!USES_LIST(mb_type, 1)){
  1347. COLOR(240,48)
  1348. }else if(!USES_LIST(mb_type, 0)){
  1349. COLOR(0,48)
  1350. }else{
  1351. assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
  1352. COLOR(300,48)
  1353. }
  1354. u*= 0x0101010101010101ULL;
  1355. v*= 0x0101010101010101ULL;
  1356. for(y=0; y<block_height; y++){
  1357. *(uint64_t*)(pict->data[1] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[1])= u;
  1358. *(uint64_t*)(pict->data[2] + 8*mb_x + (block_height*mb_y + y)*pict->linesize[2])= v;
  1359. }
  1360. //segmentation
  1361. if(IS_8X8(mb_type) || IS_16X8(mb_type)){
  1362. *(uint64_t*)(pict->data[0] + 16*mb_x + 0 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1363. *(uint64_t*)(pict->data[0] + 16*mb_x + 8 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
  1364. }
  1365. if(IS_8X8(mb_type) || IS_8X16(mb_type)){
  1366. for(y=0; y<16; y++)
  1367. pict->data[0][16*mb_x + 8 + (16*mb_y + y)*pict->linesize[0]]^= 0x80;
  1368. }
  1369. if(IS_8X8(mb_type) && mv_sample_log2 >= 2){
  1370. int dm= 1 << (mv_sample_log2-2);
  1371. for(i=0; i<4; i++){
  1372. int sx= mb_x*16 + 8*(i&1);
  1373. int sy= mb_y*16 + 8*(i>>1);
  1374. int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
  1375. //FIXME bidir
  1376. int32_t *mv = (int32_t*)&pict->motion_val[0][xy];
  1377. if(mv[0] != mv[dm] || mv[dm*mv_stride] != mv[dm*(mv_stride+1)])
  1378. for(y=0; y<8; y++)
  1379. pict->data[0][sx + 4 + (sy + y)*pict->linesize[0]]^= 0x80;
  1380. if(mv[0] != mv[dm*mv_stride] || mv[dm] != mv[dm*(mv_stride+1)])
  1381. *(uint64_t*)(pict->data[0] + sx + (sy + 4)*pict->linesize[0])^= 0x8080808080808080ULL;
  1382. }
  1383. }
  1384. if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264){
  1385. // hmm
  1386. }
  1387. }
  1388. s->mbskip_table[mb_index]=0;
  1389. }
  1390. }
  1391. }
  1392. }
  1393. static inline int hpel_motion_lowres(MpegEncContext *s,
  1394. uint8_t *dest, uint8_t *src,
  1395. int field_based, int field_select,
  1396. int src_x, int src_y,
  1397. int width, int height, int stride,
  1398. int h_edge_pos, int v_edge_pos,
  1399. int w, int h, h264_chroma_mc_func *pix_op,
  1400. int motion_x, int motion_y)
  1401. {
  1402. const int lowres= s->avctx->lowres;
  1403. const int op_index= FFMIN(lowres, 2);
  1404. const int s_mask= (2<<lowres)-1;
  1405. int emu=0;
  1406. int sx, sy;
  1407. if(s->quarter_sample){
  1408. motion_x/=2;
  1409. motion_y/=2;
  1410. }
  1411. sx= motion_x & s_mask;
  1412. sy= motion_y & s_mask;
  1413. src_x += motion_x >> (lowres+1);
  1414. src_y += motion_y >> (lowres+1);
  1415. src += src_y * stride + src_x;
  1416. if( (unsigned)src_x > h_edge_pos - (!!sx) - w
  1417. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1418. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
  1419. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1420. src= s->edge_emu_buffer;
  1421. emu=1;
  1422. }
  1423. sx= (sx << 2) >> lowres;
  1424. sy= (sy << 2) >> lowres;
  1425. if(field_select)
  1426. src += s->linesize;
  1427. pix_op[op_index](dest, src, stride, h, sx, sy);
  1428. return emu;
  1429. }
  1430. /* apply one mpeg motion vector to the three components */
  1431. static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
  1432. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1433. int field_based, int bottom_field, int field_select,
  1434. uint8_t **ref_picture, h264_chroma_mc_func *pix_op,
  1435. int motion_x, int motion_y, int h, int mb_y)
  1436. {
  1437. uint8_t *ptr_y, *ptr_cb, *ptr_cr;
  1438. int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy, uvsx, uvsy;
  1439. const int lowres= s->avctx->lowres;
  1440. const int op_index= FFMIN(lowres-1+s->chroma_x_shift, 2);
  1441. const int block_s= 8>>lowres;
  1442. const int s_mask= (2<<lowres)-1;
  1443. const int h_edge_pos = s->h_edge_pos >> lowres;
  1444. const int v_edge_pos = s->v_edge_pos >> lowres;
  1445. linesize = s->current_picture.f.linesize[0] << field_based;
  1446. uvlinesize = s->current_picture.f.linesize[1] << field_based;
  1447. if(s->quarter_sample){ //FIXME obviously not perfect but qpel will not work in lowres anyway
  1448. motion_x/=2;
  1449. motion_y/=2;
  1450. }
  1451. if(field_based){
  1452. motion_y += (bottom_field - field_select)*((1<<lowres)-1);
  1453. }
  1454. sx= motion_x & s_mask;
  1455. sy= motion_y & s_mask;
  1456. src_x = s->mb_x*2*block_s + (motion_x >> (lowres+1));
  1457. src_y =( mb_y*2*block_s>>field_based) + (motion_y >> (lowres+1));
  1458. if (s->out_format == FMT_H263) {
  1459. uvsx = ((motion_x>>1) & s_mask) | (sx&1);
  1460. uvsy = ((motion_y>>1) & s_mask) | (sy&1);
  1461. uvsrc_x = src_x>>1;
  1462. uvsrc_y = src_y>>1;
  1463. }else if(s->out_format == FMT_H261){//even chroma mv's are full pel in H261
  1464. mx = motion_x / 4;
  1465. my = motion_y / 4;
  1466. uvsx = (2*mx) & s_mask;
  1467. uvsy = (2*my) & s_mask;
  1468. uvsrc_x = s->mb_x*block_s + (mx >> lowres);
  1469. uvsrc_y = mb_y*block_s + (my >> lowres);
  1470. } else {
  1471. if(s->chroma_y_shift){
  1472. mx = motion_x / 2;
  1473. my = motion_y / 2;
  1474. uvsx = mx & s_mask;
  1475. uvsy = my & s_mask;
  1476. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1477. uvsrc_y =( mb_y*block_s>>field_based) + (my >> (lowres+1));
  1478. } else {
  1479. if(s->chroma_x_shift){
  1480. //Chroma422
  1481. mx = motion_x / 2;
  1482. uvsx = mx & s_mask;
  1483. uvsy = motion_y & s_mask;
  1484. uvsrc_y = src_y;
  1485. uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
  1486. } else {
  1487. //Chroma444
  1488. uvsx = motion_x & s_mask;
  1489. uvsy = motion_y & s_mask;
  1490. uvsrc_x = src_x;
  1491. uvsrc_y = src_y;
  1492. }
  1493. }
  1494. }
  1495. ptr_y = ref_picture[0] + src_y * linesize + src_x;
  1496. ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
  1497. ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
  1498. if( (unsigned)src_x > h_edge_pos - (!!sx) - 2*block_s
  1499. || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
  1500. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
  1501. src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
  1502. ptr_y = s->edge_emu_buffer;
  1503. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1504. uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
  1505. s->dsp.emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based,
  1506. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1507. s->dsp.emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based,
  1508. uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
  1509. ptr_cb= uvbuf;
  1510. ptr_cr= uvbuf+16;
  1511. }
  1512. }
  1513. if(bottom_field){ //FIXME use this for field pix too instead of the obnoxious hack which changes picture.f.data
  1514. dest_y += s->linesize;
  1515. dest_cb+= s->uvlinesize;
  1516. dest_cr+= s->uvlinesize;
  1517. }
  1518. if(field_select){
  1519. ptr_y += s->linesize;
  1520. ptr_cb+= s->uvlinesize;
  1521. ptr_cr+= s->uvlinesize;
  1522. }
  1523. sx= (sx << 2) >> lowres;
  1524. sy= (sy << 2) >> lowres;
  1525. pix_op[lowres-1](dest_y, ptr_y, linesize, h, sx, sy);
  1526. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1527. uvsx= (uvsx << 2) >> lowres;
  1528. uvsy= (uvsy << 2) >> lowres;
  1529. if(h >> s->chroma_y_shift){
  1530. pix_op[op_index](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1531. pix_op[op_index](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
  1532. }
  1533. }
  1534. //FIXME h261 lowres loop filter
  1535. }
  1536. static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
  1537. uint8_t *dest_cb, uint8_t *dest_cr,
  1538. uint8_t **ref_picture,
  1539. h264_chroma_mc_func *pix_op,
  1540. int mx, int my){
  1541. const int lowres= s->avctx->lowres;
  1542. const int op_index= FFMIN(lowres, 2);
  1543. const int block_s= 8>>lowres;
  1544. const int s_mask= (2<<lowres)-1;
  1545. const int h_edge_pos = s->h_edge_pos >> (lowres+1);
  1546. const int v_edge_pos = s->v_edge_pos >> (lowres+1);
  1547. int emu=0, src_x, src_y, offset, sx, sy;
  1548. uint8_t *ptr;
  1549. if(s->quarter_sample){
  1550. mx/=2;
  1551. my/=2;
  1552. }
  1553. /* In case of 8X8, we construct a single chroma motion vector
  1554. with a special rounding */
  1555. mx= ff_h263_round_chroma(mx);
  1556. my= ff_h263_round_chroma(my);
  1557. sx= mx & s_mask;
  1558. sy= my & s_mask;
  1559. src_x = s->mb_x*block_s + (mx >> (lowres+1));
  1560. src_y = s->mb_y*block_s + (my >> (lowres+1));
  1561. offset = src_y * s->uvlinesize + src_x;
  1562. ptr = ref_picture[1] + offset;
  1563. if(s->flags&CODEC_FLAG_EMU_EDGE){
  1564. if( (unsigned)src_x > h_edge_pos - (!!sx) - block_s
  1565. || (unsigned)src_y > v_edge_pos - (!!sy) - block_s){
  1566. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1567. ptr= s->edge_emu_buffer;
  1568. emu=1;
  1569. }
  1570. }
  1571. sx= (sx << 2) >> lowres;
  1572. sy= (sy << 2) >> lowres;
  1573. pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
  1574. ptr = ref_picture[2] + offset;
  1575. if(emu){
  1576. s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
  1577. ptr= s->edge_emu_buffer;
  1578. }
  1579. pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
  1580. }
  1581. /**
  1582. * motion compensation of a single macroblock
  1583. * @param s context
  1584. * @param dest_y luma destination pointer
  1585. * @param dest_cb chroma cb/u destination pointer
  1586. * @param dest_cr chroma cr/v destination pointer
  1587. * @param dir direction (0->forward, 1->backward)
  1588. * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
  1589. * @param pix_op halfpel motion compensation function (average or put normally)
  1590. * the motion vectors are taken from s->mv and the MV type from s->mv_type
  1591. */
  1592. static inline void MPV_motion_lowres(MpegEncContext *s,
  1593. uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
  1594. int dir, uint8_t **ref_picture,
  1595. h264_chroma_mc_func *pix_op)
  1596. {
  1597. int mx, my;
  1598. int mb_x, mb_y, i;
  1599. const int lowres= s->avctx->lowres;
  1600. const int block_s= 8>>lowres;
  1601. mb_x = s->mb_x;
  1602. mb_y = s->mb_y;
  1603. switch(s->mv_type) {
  1604. case MV_TYPE_16X16:
  1605. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1606. 0, 0, 0,
  1607. ref_picture, pix_op,
  1608. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s, mb_y);
  1609. break;
  1610. case MV_TYPE_8X8:
  1611. mx = 0;
  1612. my = 0;
  1613. for(i=0;i<4;i++) {
  1614. hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) * s->linesize)*block_s,
  1615. ref_picture[0], 0, 0,
  1616. (2*mb_x + (i & 1))*block_s, (2*mb_y + (i >>1))*block_s,
  1617. s->width, s->height, s->linesize,
  1618. s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
  1619. block_s, block_s, pix_op,
  1620. s->mv[dir][i][0], s->mv[dir][i][1]);
  1621. mx += s->mv[dir][i][0];
  1622. my += s->mv[dir][i][1];
  1623. }
  1624. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY))
  1625. chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture, pix_op, mx, my);
  1626. break;
  1627. case MV_TYPE_FIELD:
  1628. if (s->picture_structure == PICT_FRAME) {
  1629. /* top field */
  1630. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1631. 1, 0, s->field_select[dir][0],
  1632. ref_picture, pix_op,
  1633. s->mv[dir][0][0], s->mv[dir][0][1], block_s, mb_y);
  1634. /* bottom field */
  1635. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1636. 1, 1, s->field_select[dir][1],
  1637. ref_picture, pix_op,
  1638. s->mv[dir][1][0], s->mv[dir][1][1], block_s, mb_y);
  1639. } else {
  1640. if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field){
  1641. ref_picture = s->current_picture_ptr->f.data;
  1642. }
  1643. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1644. 0, 0, s->field_select[dir][0],
  1645. ref_picture, pix_op,
  1646. s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s, mb_y>>1);
  1647. }
  1648. break;
  1649. case MV_TYPE_16X8:
  1650. for(i=0; i<2; i++){
  1651. uint8_t ** ref2picture;
  1652. if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == AV_PICTURE_TYPE_B || s->first_field){
  1653. ref2picture= ref_picture;
  1654. }else{
  1655. ref2picture = s->current_picture_ptr->f.data;
  1656. }
  1657. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1658. 0, 0, s->field_select[dir][i],
  1659. ref2picture, pix_op,
  1660. s->mv[dir][i][0], s->mv[dir][i][1] + 2*block_s*i, block_s, mb_y>>1);
  1661. dest_y += 2*block_s*s->linesize;
  1662. dest_cb+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1663. dest_cr+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
  1664. }
  1665. break;
  1666. case MV_TYPE_DMV:
  1667. if(s->picture_structure == PICT_FRAME){
  1668. for(i=0; i<2; i++){
  1669. int j;
  1670. for(j=0; j<2; j++){
  1671. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1672. 1, j, j^i,
  1673. ref_picture, pix_op,
  1674. s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], block_s, mb_y);
  1675. }
  1676. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1677. }
  1678. }else{
  1679. for(i=0; i<2; i++){
  1680. mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
  1681. 0, 0, s->picture_structure != i+1,
  1682. ref_picture, pix_op,
  1683. s->mv[dir][2*i][0],s->mv[dir][2*i][1],2*block_s, mb_y>>1);
  1684. // after put we make avg of the same block
  1685. pix_op = s->dsp.avg_h264_chroma_pixels_tab;
  1686. //opposite parity is always in the same frame if this is second field
  1687. if(!s->first_field){
  1688. ref_picture = s->current_picture_ptr->f.data;
  1689. }
  1690. }
  1691. }
  1692. break;
  1693. default: assert(0);
  1694. }
  1695. }
  1696. /**
  1697. * find the lowest MB row referenced in the MVs
  1698. */
  1699. int MPV_lowest_referenced_row(MpegEncContext *s, int dir)
  1700. {
  1701. int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
  1702. int my, off, i, mvs;
  1703. if (s->picture_structure != PICT_FRAME) goto unhandled;
  1704. switch (s->mv_type) {
  1705. case MV_TYPE_16X16:
  1706. mvs = 1;
  1707. break;
  1708. case MV_TYPE_16X8:
  1709. mvs = 2;
  1710. break;
  1711. case MV_TYPE_8X8:
  1712. mvs = 4;
  1713. break;
  1714. default:
  1715. goto unhandled;
  1716. }
  1717. for (i = 0; i < mvs; i++) {
  1718. my = s->mv[dir][i][1]<<qpel_shift;
  1719. my_max = FFMAX(my_max, my);
  1720. my_min = FFMIN(my_min, my);
  1721. }
  1722. off = (FFMAX(-my_min, my_max) + 63) >> 6;
  1723. return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
  1724. unhandled:
  1725. return s->mb_height-1;
  1726. }
  1727. /* put block[] to dest[] */
  1728. static inline void put_dct(MpegEncContext *s,
  1729. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1730. {
  1731. s->dct_unquantize_intra(s, block, i, qscale);
  1732. s->dsp.idct_put (dest, line_size, block);
  1733. }
  1734. /* add block[] to dest[] */
  1735. static inline void add_dct(MpegEncContext *s,
  1736. DCTELEM *block, int i, uint8_t *dest, int line_size)
  1737. {
  1738. if (s->block_last_index[i] >= 0) {
  1739. s->dsp.idct_add (dest, line_size, block);
  1740. }
  1741. }
  1742. static inline void add_dequant_dct(MpegEncContext *s,
  1743. DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
  1744. {
  1745. if (s->block_last_index[i] >= 0) {
  1746. s->dct_unquantize_inter(s, block, i, qscale);
  1747. s->dsp.idct_add (dest, line_size, block);
  1748. }
  1749. }
  1750. /**
  1751. * cleans dc, ac, coded_block for the current non intra MB
  1752. */
  1753. void ff_clean_intra_table_entries(MpegEncContext *s)
  1754. {
  1755. int wrap = s->b8_stride;
  1756. int xy = s->block_index[0];
  1757. s->dc_val[0][xy ] =
  1758. s->dc_val[0][xy + 1 ] =
  1759. s->dc_val[0][xy + wrap] =
  1760. s->dc_val[0][xy + 1 + wrap] = 1024;
  1761. /* ac pred */
  1762. memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
  1763. memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
  1764. if (s->msmpeg4_version>=3) {
  1765. s->coded_block[xy ] =
  1766. s->coded_block[xy + 1 ] =
  1767. s->coded_block[xy + wrap] =
  1768. s->coded_block[xy + 1 + wrap] = 0;
  1769. }
  1770. /* chroma */
  1771. wrap = s->mb_stride;
  1772. xy = s->mb_x + s->mb_y * wrap;
  1773. s->dc_val[1][xy] =
  1774. s->dc_val[2][xy] = 1024;
  1775. /* ac pred */
  1776. memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
  1777. memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
  1778. s->mbintra_table[xy]= 0;
  1779. }
  1780. /* generic function called after a macroblock has been parsed by the
  1781. decoder or after it has been encoded by the encoder.
  1782. Important variables used:
  1783. s->mb_intra : true if intra macroblock
  1784. s->mv_dir : motion vector direction
  1785. s->mv_type : motion vector type
  1786. s->mv : motion vector
  1787. s->interlaced_dct : true if interlaced dct used (mpeg2)
  1788. */
  1789. static av_always_inline
  1790. void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64],
  1791. int lowres_flag, int is_mpeg12)
  1792. {
  1793. const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
  1794. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
  1795. ff_xvmc_decode_mb(s);//xvmc uses pblocks
  1796. return;
  1797. }
  1798. if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
  1799. /* save DCT coefficients */
  1800. int i,j;
  1801. DCTELEM *dct = &s->current_picture.f.dct_coeff[mb_xy * 64 * 6];
  1802. av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
  1803. for(i=0; i<6; i++){
  1804. for(j=0; j<64; j++){
  1805. *dct++ = block[i][s->dsp.idct_permutation[j]];
  1806. av_log(s->avctx, AV_LOG_DEBUG, "%5d", dct[-1]);
  1807. }
  1808. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  1809. }
  1810. }
  1811. s->current_picture.f.qscale_table[mb_xy] = s->qscale;
  1812. /* update DC predictors for P macroblocks */
  1813. if (!s->mb_intra) {
  1814. if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
  1815. if(s->mbintra_table[mb_xy])
  1816. ff_clean_intra_table_entries(s);
  1817. } else {
  1818. s->last_dc[0] =
  1819. s->last_dc[1] =
  1820. s->last_dc[2] = 128 << s->intra_dc_precision;
  1821. }
  1822. }
  1823. else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
  1824. s->mbintra_table[mb_xy]=1;
  1825. if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
  1826. uint8_t *dest_y, *dest_cb, *dest_cr;
  1827. int dct_linesize, dct_offset;
  1828. op_pixels_func (*op_pix)[4];
  1829. qpel_mc_func (*op_qpix)[16];
  1830. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  1831. const int uvlinesize = s->current_picture.f.linesize[1];
  1832. const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
  1833. const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
  1834. /* avoid copy if macroblock skipped in last frame too */
  1835. /* skip only during decoding as we might trash the buffers during encoding a bit */
  1836. if(!s->encoding){
  1837. uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
  1838. const int age = s->current_picture.f.age;
  1839. assert(age);
  1840. if (s->mb_skipped) {
  1841. s->mb_skipped= 0;
  1842. assert(s->pict_type!=AV_PICTURE_TYPE_I);
  1843. (*mbskip_ptr) ++; /* indicate that this time we skipped it */
  1844. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1845. /* if previous was skipped too, then nothing to do ! */
  1846. if (*mbskip_ptr >= age && s->current_picture.f.reference){
  1847. return;
  1848. }
  1849. } else if(!s->current_picture.f.reference) {
  1850. (*mbskip_ptr) ++; /* increase counter so the age can be compared cleanly */
  1851. if(*mbskip_ptr >99) *mbskip_ptr= 99;
  1852. } else{
  1853. *mbskip_ptr = 0; /* not skipped */
  1854. }
  1855. }
  1856. dct_linesize = linesize << s->interlaced_dct;
  1857. dct_offset =(s->interlaced_dct)? linesize : linesize*block_size;
  1858. if(readable){
  1859. dest_y= s->dest[0];
  1860. dest_cb= s->dest[1];
  1861. dest_cr= s->dest[2];
  1862. }else{
  1863. dest_y = s->b_scratchpad;
  1864. dest_cb= s->b_scratchpad+16*linesize;
  1865. dest_cr= s->b_scratchpad+32*linesize;
  1866. }
  1867. if (!s->mb_intra) {
  1868. /* motion handling */
  1869. /* decoding or more than one mb_type (MC was already done otherwise) */
  1870. if(!s->encoding){
  1871. if(HAVE_PTHREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  1872. if (s->mv_dir & MV_DIR_FORWARD) {
  1873. ff_thread_await_progress((AVFrame*)s->last_picture_ptr, MPV_lowest_referenced_row(s, 0), 0);
  1874. }
  1875. if (s->mv_dir & MV_DIR_BACKWARD) {
  1876. ff_thread_await_progress((AVFrame*)s->next_picture_ptr, MPV_lowest_referenced_row(s, 1), 0);
  1877. }
  1878. }
  1879. if(lowres_flag){
  1880. h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
  1881. if (s->mv_dir & MV_DIR_FORWARD) {
  1882. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix);
  1883. op_pix = s->dsp.avg_h264_chroma_pixels_tab;
  1884. }
  1885. if (s->mv_dir & MV_DIR_BACKWARD) {
  1886. MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix);
  1887. }
  1888. }else{
  1889. op_qpix= s->me.qpel_put;
  1890. if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  1891. op_pix = s->dsp.put_pixels_tab;
  1892. }else{
  1893. op_pix = s->dsp.put_no_rnd_pixels_tab;
  1894. }
  1895. if (s->mv_dir & MV_DIR_FORWARD) {
  1896. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
  1897. op_pix = s->dsp.avg_pixels_tab;
  1898. op_qpix= s->me.qpel_avg;
  1899. }
  1900. if (s->mv_dir & MV_DIR_BACKWARD) {
  1901. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
  1902. }
  1903. }
  1904. }
  1905. /* skip dequant / idct if we are really late ;) */
  1906. if(s->avctx->skip_idct){
  1907. if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
  1908. ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
  1909. || s->avctx->skip_idct >= AVDISCARD_ALL)
  1910. goto skip_idct;
  1911. }
  1912. /* add dct residue */
  1913. if(s->encoding || !( s->msmpeg4_version || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
  1914. || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
  1915. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1916. add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1917. add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1918. add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1919. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1920. if (s->chroma_y_shift){
  1921. add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1922. add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1923. }else{
  1924. dct_linesize >>= 1;
  1925. dct_offset >>=1;
  1926. add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1927. add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1928. add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1929. add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1930. }
  1931. }
  1932. } else if(is_mpeg12 || (s->codec_id != CODEC_ID_WMV2)){
  1933. add_dct(s, block[0], 0, dest_y , dct_linesize);
  1934. add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
  1935. add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
  1936. add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
  1937. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1938. if(s->chroma_y_shift){//Chroma420
  1939. add_dct(s, block[4], 4, dest_cb, uvlinesize);
  1940. add_dct(s, block[5], 5, dest_cr, uvlinesize);
  1941. }else{
  1942. //chroma422
  1943. dct_linesize = uvlinesize << s->interlaced_dct;
  1944. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*block_size;
  1945. add_dct(s, block[4], 4, dest_cb, dct_linesize);
  1946. add_dct(s, block[5], 5, dest_cr, dct_linesize);
  1947. add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
  1948. add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
  1949. if(!s->chroma_x_shift){//Chroma444
  1950. add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
  1951. add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
  1952. add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
  1953. add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
  1954. }
  1955. }
  1956. }//fi gray
  1957. }
  1958. else if (CONFIG_WMV2_DECODER || CONFIG_WMV2_ENCODER) {
  1959. ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
  1960. }
  1961. } else {
  1962. /* dct only in intra block */
  1963. if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
  1964. put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
  1965. put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
  1966. put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
  1967. put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
  1968. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1969. if(s->chroma_y_shift){
  1970. put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
  1971. put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
  1972. }else{
  1973. dct_offset >>=1;
  1974. dct_linesize >>=1;
  1975. put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
  1976. put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
  1977. put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
  1978. put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
  1979. }
  1980. }
  1981. }else{
  1982. s->dsp.idct_put(dest_y , dct_linesize, block[0]);
  1983. s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
  1984. s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
  1985. s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
  1986. if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
  1987. if(s->chroma_y_shift){
  1988. s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
  1989. s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
  1990. }else{
  1991. dct_linesize = uvlinesize << s->interlaced_dct;
  1992. dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*block_size;
  1993. s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
  1994. s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
  1995. s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
  1996. s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
  1997. if(!s->chroma_x_shift){//Chroma444
  1998. s->dsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
  1999. s->dsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
  2000. s->dsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
  2001. s->dsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
  2002. }
  2003. }
  2004. }//gray
  2005. }
  2006. }
  2007. skip_idct:
  2008. if(!readable){
  2009. s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
  2010. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
  2011. s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
  2012. }
  2013. }
  2014. }
  2015. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){
  2016. #if !CONFIG_SMALL
  2017. if(s->out_format == FMT_MPEG1) {
  2018. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
  2019. else MPV_decode_mb_internal(s, block, 0, 1);
  2020. } else
  2021. #endif
  2022. if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
  2023. else MPV_decode_mb_internal(s, block, 0, 0);
  2024. }
  2025. /**
  2026. *
  2027. * @param h is the normal height, this will be reduced automatically if needed for the last row
  2028. */
  2029. void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
  2030. const int field_pic= s->picture_structure != PICT_FRAME;
  2031. if(field_pic){
  2032. h <<= 1;
  2033. y <<= 1;
  2034. }
  2035. if (!s->avctx->hwaccel
  2036. && !(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
  2037. && s->unrestricted_mv
  2038. && s->current_picture.f.reference
  2039. && !s->intra_only
  2040. && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
  2041. int sides = 0, edge_h;
  2042. int hshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_w;
  2043. int vshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_h;
  2044. if (y==0) sides |= EDGE_TOP;
  2045. if (y + h >= s->v_edge_pos) sides |= EDGE_BOTTOM;
  2046. edge_h= FFMIN(h, s->v_edge_pos - y);
  2047. s->dsp.draw_edges(s->current_picture_ptr->f.data[0] + y *s->linesize,
  2048. s->linesize, s->h_edge_pos, edge_h,
  2049. EDGE_WIDTH, EDGE_WIDTH, sides);
  2050. s->dsp.draw_edges(s->current_picture_ptr->f.data[1] + (y>>vshift)*s->uvlinesize,
  2051. s->uvlinesize, s->h_edge_pos>>hshift, edge_h>>vshift,
  2052. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, sides);
  2053. s->dsp.draw_edges(s->current_picture_ptr->f.data[2] + (y>>vshift)*s->uvlinesize,
  2054. s->uvlinesize, s->h_edge_pos>>hshift, edge_h>>vshift,
  2055. EDGE_WIDTH>>hshift, EDGE_WIDTH>>vshift, sides);
  2056. }
  2057. h= FFMIN(h, s->avctx->height - y);
  2058. if(field_pic && s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
  2059. if (s->avctx->draw_horiz_band) {
  2060. AVFrame *src;
  2061. int offset[4];
  2062. if(s->pict_type==AV_PICTURE_TYPE_B || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
  2063. src= (AVFrame*)s->current_picture_ptr;
  2064. else if(s->last_picture_ptr)
  2065. src= (AVFrame*)s->last_picture_ptr;
  2066. else
  2067. return;
  2068. if(s->pict_type==AV_PICTURE_TYPE_B && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
  2069. offset[0]=
  2070. offset[1]=
  2071. offset[2]=
  2072. offset[3]= 0;
  2073. }else{
  2074. offset[0]= y * s->linesize;
  2075. offset[1]=
  2076. offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
  2077. offset[3]= 0;
  2078. }
  2079. emms_c();
  2080. s->avctx->draw_horiz_band(s->avctx, src, offset,
  2081. y, s->picture_structure, h);
  2082. }
  2083. }
  2084. void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
  2085. const int linesize = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
  2086. const int uvlinesize = s->current_picture.f.linesize[1];
  2087. const int mb_size= 4 - s->avctx->lowres;
  2088. s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
  2089. s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
  2090. s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
  2091. s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
  2092. s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2093. s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
  2094. //block_index is not used by mpeg2, so it is not affected by chroma_format
  2095. s->dest[0] = s->current_picture.f.data[0] + ((s->mb_x - 1) << mb_size);
  2096. s->dest[1] = s->current_picture.f.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2097. s->dest[2] = s->current_picture.f.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
  2098. if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
  2099. {
  2100. if(s->picture_structure==PICT_FRAME){
  2101. s->dest[0] += s->mb_y * linesize << mb_size;
  2102. s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2103. s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
  2104. }else{
  2105. s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
  2106. s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2107. s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
  2108. assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
  2109. }
  2110. }
  2111. }
  2112. void ff_mpeg_flush(AVCodecContext *avctx){
  2113. int i;
  2114. MpegEncContext *s = avctx->priv_data;
  2115. if(s==NULL || s->picture==NULL)
  2116. return;
  2117. for(i=0; i<s->picture_count; i++){
  2118. if (s->picture[i].f.data[0] &&
  2119. (s->picture[i].f.type == FF_BUFFER_TYPE_INTERNAL ||
  2120. s->picture[i].f.type == FF_BUFFER_TYPE_USER))
  2121. free_frame_buffer(s, &s->picture[i]);
  2122. }
  2123. s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
  2124. s->mb_x= s->mb_y= 0;
  2125. s->closed_gop= 0;
  2126. s->parse_context.state= -1;
  2127. s->parse_context.frame_start_found= 0;
  2128. s->parse_context.overread= 0;
  2129. s->parse_context.overread_index= 0;
  2130. s->parse_context.index= 0;
  2131. s->parse_context.last_index= 0;
  2132. s->bitstream_buffer_size=0;
  2133. s->pp_time=0;
  2134. }
  2135. static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
  2136. DCTELEM *block, int n, int qscale)
  2137. {
  2138. int i, level, nCoeffs;
  2139. const uint16_t *quant_matrix;
  2140. nCoeffs= s->block_last_index[n];
  2141. if (n < 4)
  2142. block[0] = block[0] * s->y_dc_scale;
  2143. else
  2144. block[0] = block[0] * s->c_dc_scale;
  2145. /* XXX: only mpeg1 */
  2146. quant_matrix = s->intra_matrix;
  2147. for(i=1;i<=nCoeffs;i++) {
  2148. int j= s->intra_scantable.permutated[i];
  2149. level = block[j];
  2150. if (level) {
  2151. if (level < 0) {
  2152. level = -level;
  2153. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2154. level = (level - 1) | 1;
  2155. level = -level;
  2156. } else {
  2157. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2158. level = (level - 1) | 1;
  2159. }
  2160. block[j] = level;
  2161. }
  2162. }
  2163. }
  2164. static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
  2165. DCTELEM *block, int n, int qscale)
  2166. {
  2167. int i, level, nCoeffs;
  2168. const uint16_t *quant_matrix;
  2169. nCoeffs= s->block_last_index[n];
  2170. quant_matrix = s->inter_matrix;
  2171. for(i=0; i<=nCoeffs; i++) {
  2172. int j= s->intra_scantable.permutated[i];
  2173. level = block[j];
  2174. if (level) {
  2175. if (level < 0) {
  2176. level = -level;
  2177. level = (((level << 1) + 1) * qscale *
  2178. ((int) (quant_matrix[j]))) >> 4;
  2179. level = (level - 1) | 1;
  2180. level = -level;
  2181. } else {
  2182. level = (((level << 1) + 1) * qscale *
  2183. ((int) (quant_matrix[j]))) >> 4;
  2184. level = (level - 1) | 1;
  2185. }
  2186. block[j] = level;
  2187. }
  2188. }
  2189. }
  2190. static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
  2191. DCTELEM *block, int n, int qscale)
  2192. {
  2193. int i, level, nCoeffs;
  2194. const uint16_t *quant_matrix;
  2195. if(s->alternate_scan) nCoeffs= 63;
  2196. else nCoeffs= s->block_last_index[n];
  2197. if (n < 4)
  2198. block[0] = block[0] * s->y_dc_scale;
  2199. else
  2200. block[0] = block[0] * s->c_dc_scale;
  2201. quant_matrix = s->intra_matrix;
  2202. for(i=1;i<=nCoeffs;i++) {
  2203. int j= s->intra_scantable.permutated[i];
  2204. level = block[j];
  2205. if (level) {
  2206. if (level < 0) {
  2207. level = -level;
  2208. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2209. level = -level;
  2210. } else {
  2211. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2212. }
  2213. block[j] = level;
  2214. }
  2215. }
  2216. }
  2217. static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
  2218. DCTELEM *block, int n, int qscale)
  2219. {
  2220. int i, level, nCoeffs;
  2221. const uint16_t *quant_matrix;
  2222. int sum=-1;
  2223. if(s->alternate_scan) nCoeffs= 63;
  2224. else nCoeffs= s->block_last_index[n];
  2225. if (n < 4)
  2226. block[0] = block[0] * s->y_dc_scale;
  2227. else
  2228. block[0] = block[0] * s->c_dc_scale;
  2229. quant_matrix = s->intra_matrix;
  2230. for(i=1;i<=nCoeffs;i++) {
  2231. int j= s->intra_scantable.permutated[i];
  2232. level = block[j];
  2233. if (level) {
  2234. if (level < 0) {
  2235. level = -level;
  2236. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2237. level = -level;
  2238. } else {
  2239. level = (int)(level * qscale * quant_matrix[j]) >> 3;
  2240. }
  2241. block[j] = level;
  2242. sum+=level;
  2243. }
  2244. }
  2245. block[63]^=sum&1;
  2246. }
  2247. static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
  2248. DCTELEM *block, int n, int qscale)
  2249. {
  2250. int i, level, nCoeffs;
  2251. const uint16_t *quant_matrix;
  2252. int sum=-1;
  2253. if(s->alternate_scan) nCoeffs= 63;
  2254. else nCoeffs= s->block_last_index[n];
  2255. quant_matrix = s->inter_matrix;
  2256. for(i=0; i<=nCoeffs; i++) {
  2257. int j= s->intra_scantable.permutated[i];
  2258. level = block[j];
  2259. if (level) {
  2260. if (level < 0) {
  2261. level = -level;
  2262. level = (((level << 1) + 1) * qscale *
  2263. ((int) (quant_matrix[j]))) >> 4;
  2264. level = -level;
  2265. } else {
  2266. level = (((level << 1) + 1) * qscale *
  2267. ((int) (quant_matrix[j]))) >> 4;
  2268. }
  2269. block[j] = level;
  2270. sum+=level;
  2271. }
  2272. }
  2273. block[63]^=sum&1;
  2274. }
  2275. static void dct_unquantize_h263_intra_c(MpegEncContext *s,
  2276. DCTELEM *block, int n, int qscale)
  2277. {
  2278. int i, level, qmul, qadd;
  2279. int nCoeffs;
  2280. assert(s->block_last_index[n]>=0);
  2281. qmul = qscale << 1;
  2282. if (!s->h263_aic) {
  2283. if (n < 4)
  2284. block[0] = block[0] * s->y_dc_scale;
  2285. else
  2286. block[0] = block[0] * s->c_dc_scale;
  2287. qadd = (qscale - 1) | 1;
  2288. }else{
  2289. qadd = 0;
  2290. }
  2291. if(s->ac_pred)
  2292. nCoeffs=63;
  2293. else
  2294. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2295. for(i=1; i<=nCoeffs; i++) {
  2296. level = block[i];
  2297. if (level) {
  2298. if (level < 0) {
  2299. level = level * qmul - qadd;
  2300. } else {
  2301. level = level * qmul + qadd;
  2302. }
  2303. block[i] = level;
  2304. }
  2305. }
  2306. }
  2307. static void dct_unquantize_h263_inter_c(MpegEncContext *s,
  2308. DCTELEM *block, int n, int qscale)
  2309. {
  2310. int i, level, qmul, qadd;
  2311. int nCoeffs;
  2312. assert(s->block_last_index[n]>=0);
  2313. qadd = (qscale - 1) | 1;
  2314. qmul = qscale << 1;
  2315. nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
  2316. for(i=0; i<=nCoeffs; i++) {
  2317. level = block[i];
  2318. if (level) {
  2319. if (level < 0) {
  2320. level = level * qmul - qadd;
  2321. } else {
  2322. level = level * qmul + qadd;
  2323. }
  2324. block[i] = level;
  2325. }
  2326. }
  2327. }
  2328. /**
  2329. * set qscale and update qscale dependent variables.
  2330. */
  2331. void ff_set_qscale(MpegEncContext * s, int qscale)
  2332. {
  2333. if (qscale < 1)
  2334. qscale = 1;
  2335. else if (qscale > 31)
  2336. qscale = 31;
  2337. s->qscale = qscale;
  2338. s->chroma_qscale= s->chroma_qscale_table[qscale];
  2339. s->y_dc_scale= s->y_dc_scale_table[ qscale ];
  2340. s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
  2341. }
  2342. void MPV_report_decode_progress(MpegEncContext *s)
  2343. {
  2344. if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->error_occurred)
  2345. ff_thread_report_progress((AVFrame*)s->current_picture_ptr, s->mb_y, 0);
  2346. }