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.

2630 lines
91KB

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