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.

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