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.

474 lines
16KB

  1. /*
  2. * Mpeg video formats-related picture management functions
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdint.h>
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/common.h"
  23. #include "avcodec.h"
  24. #include "motion_est.h"
  25. #include "mpegpicture.h"
  26. #include "mpegutils.h"
  27. static int make_tables_writable(Picture *pic)
  28. {
  29. int ret, i;
  30. #define MAKE_WRITABLE(table) \
  31. do {\
  32. if (pic->table &&\
  33. (ret = av_buffer_make_writable(&pic->table)) < 0)\
  34. return ret;\
  35. } while (0)
  36. MAKE_WRITABLE(mb_var_buf);
  37. MAKE_WRITABLE(mc_mb_var_buf);
  38. MAKE_WRITABLE(mb_mean_buf);
  39. MAKE_WRITABLE(mbskip_table_buf);
  40. MAKE_WRITABLE(qscale_table_buf);
  41. MAKE_WRITABLE(mb_type_buf);
  42. for (i = 0; i < 2; i++) {
  43. MAKE_WRITABLE(motion_val_buf[i]);
  44. MAKE_WRITABLE(ref_index_buf[i]);
  45. }
  46. return 0;
  47. }
  48. int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me,
  49. ScratchpadContext *sc, int linesize)
  50. {
  51. int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
  52. if (avctx->hwaccel
  53. #if FF_API_CAP_VDPAU
  54. || avctx->codec->capabilities & AV_CODEC_CAP_HWACCEL_VDPAU
  55. #endif
  56. )
  57. return 0;
  58. if (linesize < 24) {
  59. av_log(avctx, AV_LOG_ERROR, "Image too small, temporary buffers cannot function\n");
  60. return AVERROR_PATCHWELCOME;
  61. }
  62. // edge emu needs blocksize + filter length - 1
  63. // (= 17x17 for halfpel / 21x21 for H.264)
  64. // VC-1 computes luma and chroma simultaneously and needs 19X19 + 9x9
  65. // at uvlinesize. It supports only YUV420 so 24x24 is enough
  66. // linesize * interlaced * MBsize
  67. // we also use this buffer for encoding in encode_mb_internal() needig an additional 32 lines
  68. FF_ALLOCZ_ARRAY_OR_GOTO(avctx, sc->edge_emu_buffer, alloc_size, 4 * 70,
  69. fail);
  70. FF_ALLOCZ_ARRAY_OR_GOTO(avctx, me->scratchpad, alloc_size, 4 * 16 * 2,
  71. fail)
  72. me->temp = me->scratchpad;
  73. sc->rd_scratchpad = me->scratchpad;
  74. sc->b_scratchpad = me->scratchpad;
  75. sc->obmc_scratchpad = me->scratchpad + 16;
  76. return 0;
  77. fail:
  78. av_freep(&sc->edge_emu_buffer);
  79. return AVERROR(ENOMEM);
  80. }
  81. /**
  82. * Allocate a frame buffer
  83. */
  84. static int alloc_frame_buffer(AVCodecContext *avctx, Picture *pic,
  85. MotionEstContext *me, ScratchpadContext *sc,
  86. int chroma_x_shift, int chroma_y_shift,
  87. int linesize, int uvlinesize)
  88. {
  89. int edges_needed = av_codec_is_encoder(avctx->codec);
  90. int r, ret;
  91. pic->tf.f = pic->f;
  92. if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  93. avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
  94. avctx->codec_id != AV_CODEC_ID_MSS2) {
  95. if (edges_needed) {
  96. pic->f->width = avctx->width + 2 * EDGE_WIDTH;
  97. pic->f->height = avctx->height + 2 * EDGE_WIDTH;
  98. }
  99. r = ff_thread_get_buffer(avctx, &pic->tf,
  100. pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
  101. } else {
  102. pic->f->width = avctx->width;
  103. pic->f->height = avctx->height;
  104. pic->f->format = avctx->pix_fmt;
  105. r = avcodec_default_get_buffer2(avctx, pic->f, 0);
  106. }
  107. if (r < 0 || !pic->f->buf[0]) {
  108. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
  109. r, pic->f->data[0]);
  110. return -1;
  111. }
  112. if (edges_needed) {
  113. int i;
  114. for (i = 0; pic->f->data[i]; i++) {
  115. int offset = (EDGE_WIDTH >> (i ? chroma_y_shift : 0)) *
  116. pic->f->linesize[i] +
  117. (EDGE_WIDTH >> (i ? chroma_x_shift : 0));
  118. pic->f->data[i] += offset;
  119. }
  120. pic->f->width = avctx->width;
  121. pic->f->height = avctx->height;
  122. }
  123. if (avctx->hwaccel) {
  124. assert(!pic->hwaccel_picture_private);
  125. if (avctx->hwaccel->frame_priv_data_size) {
  126. pic->hwaccel_priv_buf = av_buffer_allocz(avctx->hwaccel->frame_priv_data_size);
  127. if (!pic->hwaccel_priv_buf) {
  128. av_log(avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
  129. return -1;
  130. }
  131. pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
  132. }
  133. }
  134. if (linesize && (linesize != pic->f->linesize[0] ||
  135. uvlinesize != pic->f->linesize[1])) {
  136. av_log(avctx, AV_LOG_ERROR,
  137. "get_buffer() failed (stride changed)\n");
  138. ff_mpeg_unref_picture(avctx, pic);
  139. return -1;
  140. }
  141. if (pic->f->linesize[1] != pic->f->linesize[2]) {
  142. av_log(avctx, AV_LOG_ERROR,
  143. "get_buffer() failed (uv stride mismatch)\n");
  144. ff_mpeg_unref_picture(avctx, pic);
  145. return -1;
  146. }
  147. if (!sc->edge_emu_buffer &&
  148. (ret = ff_mpeg_framesize_alloc(avctx, me, sc,
  149. pic->f->linesize[0])) < 0) {
  150. av_log(avctx, AV_LOG_ERROR,
  151. "get_buffer() failed to allocate context scratch buffers.\n");
  152. ff_mpeg_unref_picture(avctx, pic);
  153. return ret;
  154. }
  155. return 0;
  156. }
  157. static int alloc_picture_tables(AVCodecContext *avctx, Picture *pic, int encoding, int out_format,
  158. int mb_stride, int mb_width, int mb_height, int b8_stride)
  159. {
  160. const int big_mb_num = mb_stride * (mb_height + 1) + 1;
  161. const int mb_array_size = mb_stride * mb_height;
  162. const int b8_array_size = b8_stride * mb_height * 2;
  163. int i;
  164. pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
  165. pic->qscale_table_buf = av_buffer_allocz(big_mb_num + mb_stride);
  166. pic->mb_type_buf = av_buffer_allocz((big_mb_num + mb_stride) *
  167. sizeof(uint32_t));
  168. if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
  169. return AVERROR(ENOMEM);
  170. if (encoding) {
  171. pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  172. pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
  173. pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
  174. if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
  175. return AVERROR(ENOMEM);
  176. }
  177. if (out_format == FMT_H263 || encoding || avctx->debug_mv ||
  178. (avctx->flags2 & AV_CODEC_FLAG2_EXPORT_MVS)) {
  179. int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
  180. int ref_index_size = 4 * mb_array_size;
  181. for (i = 0; mv_size && i < 2; i++) {
  182. pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
  183. pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
  184. if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
  185. return AVERROR(ENOMEM);
  186. }
  187. }
  188. pic->alloc_mb_width = mb_width;
  189. pic->alloc_mb_height = mb_height;
  190. return 0;
  191. }
  192. /**
  193. * Allocate a Picture.
  194. * The pixels are allocated/set by calling get_buffer() if shared = 0
  195. */
  196. int ff_alloc_picture(AVCodecContext *avctx, Picture *pic, MotionEstContext *me,
  197. ScratchpadContext *sc, int shared, int encoding,
  198. int chroma_x_shift, int chroma_y_shift, int out_format,
  199. int mb_stride, int mb_width, int mb_height, int b8_stride,
  200. ptrdiff_t *linesize, ptrdiff_t *uvlinesize)
  201. {
  202. int i, ret;
  203. if (pic->qscale_table_buf)
  204. if ( pic->alloc_mb_width != mb_width
  205. || pic->alloc_mb_height != mb_height)
  206. ff_free_picture_tables(pic);
  207. if (shared) {
  208. av_assert0(pic->f->data[0]);
  209. pic->shared = 1;
  210. } else {
  211. av_assert0(!pic->f->buf[0]);
  212. if (alloc_frame_buffer(avctx, pic, me, sc,
  213. chroma_x_shift, chroma_y_shift,
  214. *linesize, *uvlinesize) < 0)
  215. return -1;
  216. *linesize = pic->f->linesize[0];
  217. *uvlinesize = pic->f->linesize[1];
  218. }
  219. if (!pic->qscale_table_buf)
  220. ret = alloc_picture_tables(avctx, pic, encoding, out_format,
  221. mb_stride, mb_width, mb_height, b8_stride);
  222. else
  223. ret = make_tables_writable(pic);
  224. if (ret < 0)
  225. goto fail;
  226. if (encoding) {
  227. pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
  228. pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
  229. pic->mb_mean = pic->mb_mean_buf->data;
  230. }
  231. pic->mbskip_table = pic->mbskip_table_buf->data;
  232. pic->qscale_table = pic->qscale_table_buf->data + 2 * mb_stride + 1;
  233. pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * mb_stride + 1;
  234. if (pic->motion_val_buf[0]) {
  235. for (i = 0; i < 2; i++) {
  236. pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
  237. pic->ref_index[i] = pic->ref_index_buf[i]->data;
  238. }
  239. }
  240. return 0;
  241. fail:
  242. av_log(avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
  243. ff_mpeg_unref_picture(avctx, pic);
  244. ff_free_picture_tables(pic);
  245. return AVERROR(ENOMEM);
  246. }
  247. /**
  248. * Deallocate a picture.
  249. */
  250. void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *pic)
  251. {
  252. int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
  253. pic->tf.f = pic->f;
  254. /* WM Image / Screen codecs allocate internal buffers with different
  255. * dimensions / colorspaces; ignore user-defined callbacks for these. */
  256. if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
  257. avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
  258. avctx->codec_id != AV_CODEC_ID_MSS2)
  259. ff_thread_release_buffer(avctx, &pic->tf);
  260. else if (pic->f)
  261. av_frame_unref(pic->f);
  262. av_buffer_unref(&pic->hwaccel_priv_buf);
  263. if (pic->needs_realloc)
  264. ff_free_picture_tables(pic);
  265. memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
  266. }
  267. int ff_update_picture_tables(Picture *dst, Picture *src)
  268. {
  269. int i;
  270. #define UPDATE_TABLE(table) \
  271. do { \
  272. if (src->table && \
  273. (!dst->table || dst->table->buffer != src->table->buffer)) { \
  274. av_buffer_unref(&dst->table); \
  275. dst->table = av_buffer_ref(src->table); \
  276. if (!dst->table) { \
  277. ff_free_picture_tables(dst); \
  278. return AVERROR(ENOMEM); \
  279. } \
  280. } \
  281. } while (0)
  282. UPDATE_TABLE(mb_var_buf);
  283. UPDATE_TABLE(mc_mb_var_buf);
  284. UPDATE_TABLE(mb_mean_buf);
  285. UPDATE_TABLE(mbskip_table_buf);
  286. UPDATE_TABLE(qscale_table_buf);
  287. UPDATE_TABLE(mb_type_buf);
  288. for (i = 0; i < 2; i++) {
  289. UPDATE_TABLE(motion_val_buf[i]);
  290. UPDATE_TABLE(ref_index_buf[i]);
  291. }
  292. dst->mb_var = src->mb_var;
  293. dst->mc_mb_var = src->mc_mb_var;
  294. dst->mb_mean = src->mb_mean;
  295. dst->mbskip_table = src->mbskip_table;
  296. dst->qscale_table = src->qscale_table;
  297. dst->mb_type = src->mb_type;
  298. for (i = 0; i < 2; i++) {
  299. dst->motion_val[i] = src->motion_val[i];
  300. dst->ref_index[i] = src->ref_index[i];
  301. }
  302. dst->alloc_mb_width = src->alloc_mb_width;
  303. dst->alloc_mb_height = src->alloc_mb_height;
  304. return 0;
  305. }
  306. int ff_mpeg_ref_picture(AVCodecContext *avctx, Picture *dst, Picture *src)
  307. {
  308. int ret;
  309. av_assert0(!dst->f->buf[0]);
  310. av_assert0(src->f->buf[0]);
  311. src->tf.f = src->f;
  312. dst->tf.f = dst->f;
  313. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  314. if (ret < 0)
  315. goto fail;
  316. ret = ff_update_picture_tables(dst, src);
  317. if (ret < 0)
  318. goto fail;
  319. if (src->hwaccel_picture_private) {
  320. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  321. if (!dst->hwaccel_priv_buf)
  322. goto fail;
  323. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  324. }
  325. dst->field_picture = src->field_picture;
  326. dst->mb_var_sum = src->mb_var_sum;
  327. dst->mc_mb_var_sum = src->mc_mb_var_sum;
  328. dst->b_frame_score = src->b_frame_score;
  329. dst->needs_realloc = src->needs_realloc;
  330. dst->reference = src->reference;
  331. dst->shared = src->shared;
  332. memcpy(dst->encoding_error, src->encoding_error,
  333. sizeof(dst->encoding_error));
  334. return 0;
  335. fail:
  336. ff_mpeg_unref_picture(avctx, dst);
  337. return ret;
  338. }
  339. static inline int pic_is_unused(Picture *pic)
  340. {
  341. if (!pic->f->buf[0])
  342. return 1;
  343. if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
  344. return 1;
  345. return 0;
  346. }
  347. static int find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
  348. {
  349. int i;
  350. if (shared) {
  351. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  352. if (!picture[i].f->buf[0])
  353. return i;
  354. }
  355. } else {
  356. for (i = 0; i < MAX_PICTURE_COUNT; i++) {
  357. if (pic_is_unused(&picture[i]))
  358. return i;
  359. }
  360. }
  361. av_log(avctx, AV_LOG_FATAL,
  362. "Internal error, picture buffer overflow\n");
  363. /* We could return -1, but the codec would crash trying to draw into a
  364. * non-existing frame anyway. This is safer than waiting for a random crash.
  365. * Also the return of this is never useful, an encoder must only allocate
  366. * as much as allowed in the specification. This has no relationship to how
  367. * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
  368. * enough for such valid streams).
  369. * Plus, a decoder has to check stream validity and remove frames if too
  370. * many reference frames are around. Waiting for "OOM" is not correct at
  371. * all. Similarly, missing reference frames have to be replaced by
  372. * interpolated/MC frames, anything else is a bug in the codec ...
  373. */
  374. abort();
  375. return -1;
  376. }
  377. int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
  378. {
  379. int ret = find_unused_picture(avctx, picture, shared);
  380. if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
  381. if (picture[ret].needs_realloc) {
  382. picture[ret].needs_realloc = 0;
  383. ff_free_picture_tables(&picture[ret]);
  384. ff_mpeg_unref_picture(avctx, &picture[ret]);
  385. }
  386. }
  387. return ret;
  388. }
  389. void ff_free_picture_tables(Picture *pic)
  390. {
  391. int i;
  392. pic->alloc_mb_width =
  393. pic->alloc_mb_height = 0;
  394. av_buffer_unref(&pic->mb_var_buf);
  395. av_buffer_unref(&pic->mc_mb_var_buf);
  396. av_buffer_unref(&pic->mb_mean_buf);
  397. av_buffer_unref(&pic->mbskip_table_buf);
  398. av_buffer_unref(&pic->qscale_table_buf);
  399. av_buffer_unref(&pic->mb_type_buf);
  400. for (i = 0; i < 2; i++) {
  401. av_buffer_unref(&pic->motion_val_buf[i]);
  402. av_buffer_unref(&pic->ref_index_buf[i]);
  403. }
  404. }