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.

2420 lines
72KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "config.h"
  27. #include "libavutil/attributes.h"
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/avstring.h"
  30. #include "libavutil/channel_layout.h"
  31. #include "libavutil/crc.h"
  32. #include "libavutil/frame.h"
  33. #include "libavutil/internal.h"
  34. #include "libavutil/mathematics.h"
  35. #include "libavutil/pixdesc.h"
  36. #include "libavutil/imgutils.h"
  37. #include "libavutil/samplefmt.h"
  38. #include "libavutil/dict.h"
  39. #include "avcodec.h"
  40. #include "libavutil/opt.h"
  41. #include "me_cmp.h"
  42. #include "mpegvideo.h"
  43. #include "thread.h"
  44. #include "internal.h"
  45. #include "bytestream.h"
  46. #include "version.h"
  47. #include <stdlib.h>
  48. #include <stdarg.h>
  49. #include <limits.h>
  50. #include <float.h>
  51. static int volatile entangled_thread_counter = 0;
  52. static int (*lockmgr_cb)(void **mutex, enum AVLockOp op);
  53. static void *codec_mutex;
  54. static void *avformat_mutex;
  55. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  56. {
  57. void **p = ptr;
  58. if (min_size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
  59. av_freep(p);
  60. *size = 0;
  61. return;
  62. }
  63. av_fast_malloc(p, size, min_size + AV_INPUT_BUFFER_PADDING_SIZE);
  64. if (*size)
  65. memset((uint8_t *)*p + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  66. }
  67. /* encoder management */
  68. static AVCodec *first_avcodec = NULL;
  69. AVCodec *av_codec_next(const AVCodec *c)
  70. {
  71. if (c)
  72. return c->next;
  73. else
  74. return first_avcodec;
  75. }
  76. static av_cold void avcodec_init(void)
  77. {
  78. static int initialized = 0;
  79. if (initialized != 0)
  80. return;
  81. initialized = 1;
  82. if (CONFIG_ME_CMP)
  83. ff_me_cmp_init_static();
  84. }
  85. int av_codec_is_encoder(const AVCodec *codec)
  86. {
  87. return codec && (codec->encode_sub || codec->encode2);
  88. }
  89. int av_codec_is_decoder(const AVCodec *codec)
  90. {
  91. return codec && codec->decode;
  92. }
  93. av_cold void avcodec_register(AVCodec *codec)
  94. {
  95. AVCodec **p;
  96. avcodec_init();
  97. p = &first_avcodec;
  98. while (*p)
  99. p = &(*p)->next;
  100. *p = codec;
  101. codec->next = NULL;
  102. if (codec->init_static_data)
  103. codec->init_static_data(codec);
  104. }
  105. #if FF_API_EMU_EDGE
  106. unsigned avcodec_get_edge_width(void)
  107. {
  108. return EDGE_WIDTH;
  109. }
  110. #endif
  111. #if FF_API_SET_DIMENSIONS
  112. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  113. {
  114. ff_set_dimensions(s, width, height);
  115. }
  116. #endif
  117. int ff_set_dimensions(AVCodecContext *s, int width, int height)
  118. {
  119. int ret = av_image_check_size(width, height, 0, s);
  120. if (ret < 0)
  121. width = height = 0;
  122. s->width = s->coded_width = width;
  123. s->height = s->coded_height = height;
  124. return ret;
  125. }
  126. int ff_set_sar(AVCodecContext *avctx, AVRational sar)
  127. {
  128. int ret = av_image_check_sar(avctx->width, avctx->height, sar);
  129. if (ret < 0) {
  130. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %d/%d\n",
  131. sar.num, sar.den);
  132. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  133. return ret;
  134. } else {
  135. avctx->sample_aspect_ratio = sar;
  136. }
  137. return 0;
  138. }
  139. int ff_side_data_update_matrix_encoding(AVFrame *frame,
  140. enum AVMatrixEncoding matrix_encoding)
  141. {
  142. AVFrameSideData *side_data;
  143. enum AVMatrixEncoding *data;
  144. side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_MATRIXENCODING);
  145. if (!side_data)
  146. side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_MATRIXENCODING,
  147. sizeof(enum AVMatrixEncoding));
  148. if (!side_data)
  149. return AVERROR(ENOMEM);
  150. data = (enum AVMatrixEncoding*)side_data->data;
  151. *data = matrix_encoding;
  152. return 0;
  153. }
  154. #if HAVE_SIMD_ALIGN_16
  155. # define STRIDE_ALIGN 16
  156. #else
  157. # define STRIDE_ALIGN 8
  158. #endif
  159. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  160. int linesize_align[AV_NUM_DATA_POINTERS])
  161. {
  162. int i;
  163. int w_align = 1;
  164. int h_align = 1;
  165. switch (s->pix_fmt) {
  166. case AV_PIX_FMT_YUV420P:
  167. case AV_PIX_FMT_YUYV422:
  168. case AV_PIX_FMT_YVYU422:
  169. case AV_PIX_FMT_UYVY422:
  170. case AV_PIX_FMT_YUV422P:
  171. case AV_PIX_FMT_YUV440P:
  172. case AV_PIX_FMT_YUV444P:
  173. case AV_PIX_FMT_GBRP:
  174. case AV_PIX_FMT_GBRAP:
  175. case AV_PIX_FMT_GRAY8:
  176. case AV_PIX_FMT_GRAY16BE:
  177. case AV_PIX_FMT_GRAY16LE:
  178. case AV_PIX_FMT_YUVJ420P:
  179. case AV_PIX_FMT_YUVJ422P:
  180. case AV_PIX_FMT_YUVJ440P:
  181. case AV_PIX_FMT_YUVJ444P:
  182. case AV_PIX_FMT_YUVA420P:
  183. case AV_PIX_FMT_YUVA422P:
  184. case AV_PIX_FMT_YUVA444P:
  185. case AV_PIX_FMT_YUV420P9LE:
  186. case AV_PIX_FMT_YUV420P9BE:
  187. case AV_PIX_FMT_YUV420P10LE:
  188. case AV_PIX_FMT_YUV420P10BE:
  189. case AV_PIX_FMT_YUV422P9LE:
  190. case AV_PIX_FMT_YUV422P9BE:
  191. case AV_PIX_FMT_YUV422P10LE:
  192. case AV_PIX_FMT_YUV422P10BE:
  193. case AV_PIX_FMT_YUVA422P10LE:
  194. case AV_PIX_FMT_YUVA422P10BE:
  195. case AV_PIX_FMT_YUV444P9LE:
  196. case AV_PIX_FMT_YUV444P9BE:
  197. case AV_PIX_FMT_YUV444P10LE:
  198. case AV_PIX_FMT_YUV444P10BE:
  199. case AV_PIX_FMT_YUVA444P10LE:
  200. case AV_PIX_FMT_YUVA444P10BE:
  201. case AV_PIX_FMT_GBRP9LE:
  202. case AV_PIX_FMT_GBRP9BE:
  203. case AV_PIX_FMT_GBRP10LE:
  204. case AV_PIX_FMT_GBRP10BE:
  205. w_align = 16; //FIXME assume 16 pixel per macroblock
  206. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  207. break;
  208. case AV_PIX_FMT_YUV411P:
  209. case AV_PIX_FMT_UYYVYY411:
  210. w_align = 32;
  211. h_align = 8;
  212. break;
  213. case AV_PIX_FMT_YUV410P:
  214. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  215. w_align = 64;
  216. h_align = 64;
  217. }
  218. case AV_PIX_FMT_RGB555:
  219. if (s->codec_id == AV_CODEC_ID_RPZA) {
  220. w_align = 4;
  221. h_align = 4;
  222. }
  223. case AV_PIX_FMT_PAL8:
  224. case AV_PIX_FMT_BGR8:
  225. case AV_PIX_FMT_RGB8:
  226. if (s->codec_id == AV_CODEC_ID_SMC) {
  227. w_align = 4;
  228. h_align = 4;
  229. }
  230. break;
  231. case AV_PIX_FMT_BGR24:
  232. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  233. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  234. w_align = 4;
  235. h_align = 4;
  236. }
  237. break;
  238. default:
  239. w_align = 1;
  240. h_align = 1;
  241. break;
  242. }
  243. *width = FFALIGN(*width, w_align);
  244. *height = FFALIGN(*height, h_align);
  245. if (s->codec_id == AV_CODEC_ID_H264)
  246. // some of the optimized chroma MC reads one line too much
  247. *height += 2;
  248. for (i = 0; i < 4; i++)
  249. linesize_align[i] = STRIDE_ALIGN;
  250. }
  251. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  252. {
  253. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  254. int chroma_shift = desc->log2_chroma_w;
  255. int linesize_align[AV_NUM_DATA_POINTERS];
  256. int align;
  257. avcodec_align_dimensions2(s, width, height, linesize_align);
  258. align = FFMAX(linesize_align[0], linesize_align[3]);
  259. linesize_align[1] <<= chroma_shift;
  260. linesize_align[2] <<= chroma_shift;
  261. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  262. *width = FFALIGN(*width, align);
  263. }
  264. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  265. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  266. int buf_size, int align)
  267. {
  268. int ch, planar, needed_size, ret = 0;
  269. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  270. frame->nb_samples, sample_fmt,
  271. align);
  272. if (buf_size < needed_size)
  273. return AVERROR(EINVAL);
  274. planar = av_sample_fmt_is_planar(sample_fmt);
  275. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  276. if (!(frame->extended_data = av_mallocz(nb_channels *
  277. sizeof(*frame->extended_data))))
  278. return AVERROR(ENOMEM);
  279. } else {
  280. frame->extended_data = frame->data;
  281. }
  282. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  283. buf, nb_channels, frame->nb_samples,
  284. sample_fmt, align)) < 0) {
  285. if (frame->extended_data != frame->data)
  286. av_free(frame->extended_data);
  287. return ret;
  288. }
  289. if (frame->extended_data != frame->data) {
  290. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  291. frame->data[ch] = frame->extended_data[ch];
  292. }
  293. return ret;
  294. }
  295. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  296. {
  297. FramePool *pool = avctx->internal->pool;
  298. int i, ret;
  299. switch (avctx->codec_type) {
  300. case AVMEDIA_TYPE_VIDEO: {
  301. uint8_t *data[4];
  302. int linesize[4];
  303. int size[4] = { 0 };
  304. int w = frame->width;
  305. int h = frame->height;
  306. int tmpsize, unaligned;
  307. if (pool->format == frame->format &&
  308. pool->width == frame->width && pool->height == frame->height)
  309. return 0;
  310. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  311. do {
  312. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  313. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  314. av_image_fill_linesizes(linesize, avctx->pix_fmt, w);
  315. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  316. w += w & ~(w - 1);
  317. unaligned = 0;
  318. for (i = 0; i < 4; i++)
  319. unaligned |= linesize[i] % pool->stride_align[i];
  320. } while (unaligned);
  321. tmpsize = av_image_fill_pointers(data, avctx->pix_fmt, h,
  322. NULL, linesize);
  323. if (tmpsize < 0)
  324. return -1;
  325. for (i = 0; i < 3 && data[i + 1]; i++)
  326. size[i] = data[i + 1] - data[i];
  327. size[i] = tmpsize - (data[i] - data[0]);
  328. for (i = 0; i < 4; i++) {
  329. av_buffer_pool_uninit(&pool->pools[i]);
  330. pool->linesize[i] = linesize[i];
  331. if (size[i]) {
  332. pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
  333. if (!pool->pools[i]) {
  334. ret = AVERROR(ENOMEM);
  335. goto fail;
  336. }
  337. }
  338. }
  339. pool->format = frame->format;
  340. pool->width = frame->width;
  341. pool->height = frame->height;
  342. break;
  343. }
  344. case AVMEDIA_TYPE_AUDIO: {
  345. int ch = av_get_channel_layout_nb_channels(frame->channel_layout);
  346. int planar = av_sample_fmt_is_planar(frame->format);
  347. int planes = planar ? ch : 1;
  348. if (pool->format == frame->format && pool->planes == planes &&
  349. pool->channels == ch && frame->nb_samples == pool->samples)
  350. return 0;
  351. av_buffer_pool_uninit(&pool->pools[0]);
  352. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  353. frame->nb_samples, frame->format, 0);
  354. if (ret < 0)
  355. goto fail;
  356. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  357. if (!pool->pools[0]) {
  358. ret = AVERROR(ENOMEM);
  359. goto fail;
  360. }
  361. pool->format = frame->format;
  362. pool->planes = planes;
  363. pool->channels = ch;
  364. pool->samples = frame->nb_samples;
  365. break;
  366. }
  367. default: av_assert0(0);
  368. }
  369. return 0;
  370. fail:
  371. for (i = 0; i < 4; i++)
  372. av_buffer_pool_uninit(&pool->pools[i]);
  373. pool->format = -1;
  374. pool->planes = pool->channels = pool->samples = 0;
  375. pool->width = pool->height = 0;
  376. return ret;
  377. }
  378. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  379. {
  380. FramePool *pool = avctx->internal->pool;
  381. int planes = pool->planes;
  382. int i;
  383. frame->linesize[0] = pool->linesize[0];
  384. if (planes > AV_NUM_DATA_POINTERS) {
  385. frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
  386. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  387. frame->extended_buf = av_mallocz(frame->nb_extended_buf *
  388. sizeof(*frame->extended_buf));
  389. if (!frame->extended_data || !frame->extended_buf) {
  390. av_freep(&frame->extended_data);
  391. av_freep(&frame->extended_buf);
  392. return AVERROR(ENOMEM);
  393. }
  394. } else
  395. frame->extended_data = frame->data;
  396. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  397. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  398. if (!frame->buf[i])
  399. goto fail;
  400. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  401. }
  402. for (i = 0; i < frame->nb_extended_buf; i++) {
  403. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  404. if (!frame->extended_buf[i])
  405. goto fail;
  406. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  407. }
  408. if (avctx->debug & FF_DEBUG_BUFFERS)
  409. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  410. return 0;
  411. fail:
  412. av_frame_unref(frame);
  413. return AVERROR(ENOMEM);
  414. }
  415. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  416. {
  417. FramePool *pool = s->internal->pool;
  418. int i;
  419. if (pic->data[0]) {
  420. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  421. return -1;
  422. }
  423. memset(pic->data, 0, sizeof(pic->data));
  424. pic->extended_data = pic->data;
  425. for (i = 0; i < 4 && pool->pools[i]; i++) {
  426. pic->linesize[i] = pool->linesize[i];
  427. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  428. if (!pic->buf[i])
  429. goto fail;
  430. pic->data[i] = pic->buf[i]->data;
  431. }
  432. for (; i < AV_NUM_DATA_POINTERS; i++) {
  433. pic->data[i] = NULL;
  434. pic->linesize[i] = 0;
  435. }
  436. if (pic->data[1] && !pic->data[2])
  437. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
  438. if (s->debug & FF_DEBUG_BUFFERS)
  439. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  440. return 0;
  441. fail:
  442. av_frame_unref(pic);
  443. return AVERROR(ENOMEM);
  444. }
  445. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  446. {
  447. int ret;
  448. if ((ret = update_frame_pool(avctx, frame)) < 0)
  449. return ret;
  450. switch (avctx->codec_type) {
  451. case AVMEDIA_TYPE_VIDEO:
  452. return video_get_buffer(avctx, frame);
  453. case AVMEDIA_TYPE_AUDIO:
  454. return audio_get_buffer(avctx, frame);
  455. default:
  456. return -1;
  457. }
  458. }
  459. int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
  460. {
  461. AVPacket *pkt = avctx->internal->pkt;
  462. int i;
  463. struct {
  464. enum AVPacketSideDataType packet;
  465. enum AVFrameSideDataType frame;
  466. } sd[] = {
  467. { AV_PKT_DATA_REPLAYGAIN , AV_FRAME_DATA_REPLAYGAIN },
  468. { AV_PKT_DATA_DISPLAYMATRIX, AV_FRAME_DATA_DISPLAYMATRIX },
  469. { AV_PKT_DATA_STEREO3D, AV_FRAME_DATA_STEREO3D },
  470. { AV_PKT_DATA_AUDIO_SERVICE_TYPE, AV_FRAME_DATA_AUDIO_SERVICE_TYPE },
  471. };
  472. frame->color_primaries = avctx->color_primaries;
  473. frame->color_trc = avctx->color_trc;
  474. frame->colorspace = avctx->colorspace;
  475. frame->color_range = avctx->color_range;
  476. frame->chroma_location = avctx->chroma_sample_location;
  477. frame->reordered_opaque = avctx->reordered_opaque;
  478. if (!pkt) {
  479. frame->pkt_pts = AV_NOPTS_VALUE;
  480. return 0;
  481. }
  482. frame->pkt_pts = pkt->pts;
  483. for (i = 0; i < FF_ARRAY_ELEMS(sd); i++) {
  484. int size;
  485. uint8_t *packet_sd = av_packet_get_side_data(pkt, sd[i].packet, &size);
  486. if (packet_sd) {
  487. AVFrameSideData *frame_sd = av_frame_new_side_data(frame,
  488. sd[i].frame,
  489. size);
  490. if (!frame_sd)
  491. return AVERROR(ENOMEM);
  492. memcpy(frame_sd->data, packet_sd, size);
  493. }
  494. }
  495. return 0;
  496. }
  497. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  498. {
  499. const AVHWAccel *hwaccel = avctx->hwaccel;
  500. int override_dimensions = 1;
  501. int ret;
  502. switch (avctx->codec_type) {
  503. case AVMEDIA_TYPE_VIDEO:
  504. if (frame->width <= 0 || frame->height <= 0) {
  505. frame->width = FFMAX(avctx->width, avctx->coded_width);
  506. frame->height = FFMAX(avctx->height, avctx->coded_height);
  507. override_dimensions = 0;
  508. }
  509. if (frame->format < 0)
  510. frame->format = avctx->pix_fmt;
  511. if (!frame->sample_aspect_ratio.num)
  512. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  513. if (av_image_check_sar(frame->width, frame->height,
  514. frame->sample_aspect_ratio) < 0) {
  515. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  516. frame->sample_aspect_ratio.num,
  517. frame->sample_aspect_ratio.den);
  518. frame->sample_aspect_ratio = (AVRational){ 0, 1 };
  519. }
  520. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
  521. return ret;
  522. break;
  523. case AVMEDIA_TYPE_AUDIO:
  524. if (!frame->sample_rate)
  525. frame->sample_rate = avctx->sample_rate;
  526. if (frame->format < 0)
  527. frame->format = avctx->sample_fmt;
  528. if (!frame->channel_layout) {
  529. if (avctx->channel_layout) {
  530. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  531. avctx->channels) {
  532. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  533. "configuration.\n");
  534. return AVERROR(EINVAL);
  535. }
  536. frame->channel_layout = avctx->channel_layout;
  537. } else {
  538. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  539. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  540. avctx->channels);
  541. return AVERROR(ENOSYS);
  542. }
  543. frame->channel_layout = av_get_default_channel_layout(avctx->channels);
  544. if (!frame->channel_layout)
  545. frame->channel_layout = (1ULL << avctx->channels) - 1;
  546. }
  547. }
  548. break;
  549. default: return AVERROR(EINVAL);
  550. }
  551. ret = ff_decode_frame_props(avctx, frame);
  552. if (ret < 0)
  553. return ret;
  554. if (hwaccel) {
  555. if (hwaccel->alloc_frame) {
  556. ret = hwaccel->alloc_frame(avctx, frame);
  557. goto end;
  558. }
  559. } else
  560. avctx->sw_pix_fmt = avctx->pix_fmt;
  561. ret = avctx->get_buffer2(avctx, frame, flags);
  562. end:
  563. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO && !override_dimensions) {
  564. frame->width = avctx->width;
  565. frame->height = avctx->height;
  566. }
  567. return ret;
  568. }
  569. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  570. {
  571. AVFrame *tmp;
  572. int ret;
  573. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  574. if (!frame->data[0])
  575. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  576. if (av_frame_is_writable(frame))
  577. return ff_decode_frame_props(avctx, frame);
  578. tmp = av_frame_alloc();
  579. if (!tmp)
  580. return AVERROR(ENOMEM);
  581. av_frame_move_ref(tmp, frame);
  582. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  583. if (ret < 0) {
  584. av_frame_free(&tmp);
  585. return ret;
  586. }
  587. av_frame_copy(frame, tmp);
  588. av_frame_free(&tmp);
  589. return 0;
  590. }
  591. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  592. {
  593. int i;
  594. for (i = 0; i < count; i++) {
  595. int r = func(c, (char *)arg + i * size);
  596. if (ret)
  597. ret[i] = r;
  598. }
  599. return 0;
  600. }
  601. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  602. {
  603. int i;
  604. for (i = 0; i < count; i++) {
  605. int r = func(c, arg, i, 0);
  606. if (ret)
  607. ret[i] = r;
  608. }
  609. return 0;
  610. }
  611. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  612. {
  613. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  614. return desc->flags & AV_PIX_FMT_FLAG_HWACCEL;
  615. }
  616. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  617. {
  618. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  619. ++fmt;
  620. return fmt[0];
  621. }
  622. static AVHWAccel *find_hwaccel(enum AVCodecID codec_id,
  623. enum AVPixelFormat pix_fmt)
  624. {
  625. AVHWAccel *hwaccel = NULL;
  626. while ((hwaccel = av_hwaccel_next(hwaccel)))
  627. if (hwaccel->id == codec_id
  628. && hwaccel->pix_fmt == pix_fmt)
  629. return hwaccel;
  630. return NULL;
  631. }
  632. static int setup_hwaccel(AVCodecContext *avctx,
  633. const enum AVPixelFormat fmt,
  634. const char *name)
  635. {
  636. AVHWAccel *hwa = find_hwaccel(avctx->codec_id, fmt);
  637. int ret = 0;
  638. if (!hwa) {
  639. av_log(avctx, AV_LOG_ERROR,
  640. "Could not find an AVHWAccel for the pixel format: %s",
  641. name);
  642. return AVERROR(ENOENT);
  643. }
  644. if (hwa->priv_data_size) {
  645. avctx->internal->hwaccel_priv_data = av_mallocz(hwa->priv_data_size);
  646. if (!avctx->internal->hwaccel_priv_data)
  647. return AVERROR(ENOMEM);
  648. }
  649. if (hwa->init) {
  650. ret = hwa->init(avctx);
  651. if (ret < 0) {
  652. av_freep(&avctx->internal->hwaccel_priv_data);
  653. return ret;
  654. }
  655. }
  656. avctx->hwaccel = hwa;
  657. return 0;
  658. }
  659. int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  660. {
  661. const AVPixFmtDescriptor *desc;
  662. enum AVPixelFormat *choices;
  663. enum AVPixelFormat ret;
  664. unsigned n = 0;
  665. while (fmt[n] != AV_PIX_FMT_NONE)
  666. ++n;
  667. av_assert0(n >= 1);
  668. avctx->sw_pix_fmt = fmt[n - 1];
  669. av_assert2(!is_hwaccel_pix_fmt(avctx->sw_pix_fmt));
  670. choices = av_malloc_array(n + 1, sizeof(*choices));
  671. if (!choices)
  672. return AV_PIX_FMT_NONE;
  673. memcpy(choices, fmt, (n + 1) * sizeof(*choices));
  674. for (;;) {
  675. if (avctx->hwaccel && avctx->hwaccel->uninit)
  676. avctx->hwaccel->uninit(avctx);
  677. av_freep(&avctx->internal->hwaccel_priv_data);
  678. avctx->hwaccel = NULL;
  679. ret = avctx->get_format(avctx, choices);
  680. desc = av_pix_fmt_desc_get(ret);
  681. if (!desc) {
  682. ret = AV_PIX_FMT_NONE;
  683. break;
  684. }
  685. if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
  686. break;
  687. if (!setup_hwaccel(avctx, ret, desc->name))
  688. break;
  689. /* Remove failed hwaccel from choices */
  690. for (n = 0; choices[n] != ret; n++)
  691. av_assert0(choices[n] != AV_PIX_FMT_NONE);
  692. do
  693. choices[n] = choices[n + 1];
  694. while (choices[n++] != AV_PIX_FMT_NONE);
  695. }
  696. av_freep(&choices);
  697. return ret;
  698. }
  699. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  700. {
  701. int ret = 0;
  702. AVDictionary *tmp = NULL;
  703. if (avcodec_is_open(avctx))
  704. return 0;
  705. if ((!codec && !avctx->codec)) {
  706. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  707. return AVERROR(EINVAL);
  708. }
  709. if ((codec && avctx->codec && codec != avctx->codec)) {
  710. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  711. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  712. return AVERROR(EINVAL);
  713. }
  714. if (!codec)
  715. codec = avctx->codec;
  716. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  717. return AVERROR(EINVAL);
  718. if (options)
  719. av_dict_copy(&tmp, *options, 0);
  720. /* If there is a user-supplied mutex locking routine, call it. */
  721. if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) {
  722. if (lockmgr_cb) {
  723. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  724. return -1;
  725. }
  726. entangled_thread_counter++;
  727. if (entangled_thread_counter != 1) {
  728. av_log(avctx, AV_LOG_ERROR,
  729. "Insufficient thread locking. At least %d threads are "
  730. "calling avcodec_open2() at the same time right now.\n",
  731. entangled_thread_counter);
  732. ret = -1;
  733. goto end;
  734. }
  735. }
  736. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  737. if (!avctx->internal) {
  738. ret = AVERROR(ENOMEM);
  739. goto end;
  740. }
  741. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  742. if (!avctx->internal->pool) {
  743. ret = AVERROR(ENOMEM);
  744. goto free_and_end;
  745. }
  746. avctx->internal->to_free = av_frame_alloc();
  747. if (!avctx->internal->to_free) {
  748. ret = AVERROR(ENOMEM);
  749. goto free_and_end;
  750. }
  751. if (codec->priv_data_size > 0) {
  752. if (!avctx->priv_data) {
  753. avctx->priv_data = av_mallocz(codec->priv_data_size);
  754. if (!avctx->priv_data) {
  755. ret = AVERROR(ENOMEM);
  756. goto end;
  757. }
  758. if (codec->priv_class) {
  759. *(const AVClass **)avctx->priv_data = codec->priv_class;
  760. av_opt_set_defaults(avctx->priv_data);
  761. }
  762. }
  763. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  764. goto free_and_end;
  765. } else {
  766. avctx->priv_data = NULL;
  767. }
  768. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  769. goto free_and_end;
  770. if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
  771. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  772. else if (avctx->width && avctx->height)
  773. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  774. if (ret < 0)
  775. goto free_and_end;
  776. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  777. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  778. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  779. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  780. ff_set_dimensions(avctx, 0, 0);
  781. }
  782. if (avctx->width > 0 && avctx->height > 0) {
  783. if (av_image_check_sar(avctx->width, avctx->height,
  784. avctx->sample_aspect_ratio) < 0) {
  785. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  786. avctx->sample_aspect_ratio.num,
  787. avctx->sample_aspect_ratio.den);
  788. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  789. }
  790. }
  791. /* if the decoder init function was already called previously,
  792. * free the already allocated subtitle_header before overwriting it */
  793. if (av_codec_is_decoder(codec))
  794. av_freep(&avctx->subtitle_header);
  795. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  796. ret = AVERROR(EINVAL);
  797. goto free_and_end;
  798. }
  799. avctx->codec = codec;
  800. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  801. avctx->codec_id == AV_CODEC_ID_NONE) {
  802. avctx->codec_type = codec->type;
  803. avctx->codec_id = codec->id;
  804. }
  805. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  806. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  807. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  808. ret = AVERROR(EINVAL);
  809. goto free_and_end;
  810. }
  811. avctx->frame_number = 0;
  812. if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
  813. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  814. ret = AVERROR_EXPERIMENTAL;
  815. goto free_and_end;
  816. }
  817. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  818. (!avctx->time_base.num || !avctx->time_base.den)) {
  819. avctx->time_base.num = 1;
  820. avctx->time_base.den = avctx->sample_rate;
  821. }
  822. if (HAVE_THREADS) {
  823. ret = ff_thread_init(avctx);
  824. if (ret < 0) {
  825. goto free_and_end;
  826. }
  827. }
  828. if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
  829. avctx->thread_count = 1;
  830. if (av_codec_is_encoder(avctx->codec)) {
  831. int i;
  832. #if FF_API_CODED_FRAME
  833. FF_DISABLE_DEPRECATION_WARNINGS
  834. avctx->coded_frame = av_frame_alloc();
  835. if (!avctx->coded_frame) {
  836. ret = AVERROR(ENOMEM);
  837. goto free_and_end;
  838. }
  839. FF_ENABLE_DEPRECATION_WARNINGS
  840. #endif
  841. if (avctx->codec->sample_fmts) {
  842. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  843. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  844. break;
  845. if (avctx->channels == 1 &&
  846. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  847. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  848. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  849. break;
  850. }
  851. }
  852. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  853. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  854. ret = AVERROR(EINVAL);
  855. goto free_and_end;
  856. }
  857. }
  858. if (avctx->codec->pix_fmts) {
  859. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  860. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  861. break;
  862. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
  863. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  864. ret = AVERROR(EINVAL);
  865. goto free_and_end;
  866. }
  867. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
  868. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
  869. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
  870. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
  871. avctx->color_range = AVCOL_RANGE_JPEG;
  872. }
  873. if (avctx->codec->supported_samplerates) {
  874. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  875. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  876. break;
  877. if (avctx->codec->supported_samplerates[i] == 0) {
  878. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  879. ret = AVERROR(EINVAL);
  880. goto free_and_end;
  881. }
  882. }
  883. if (avctx->codec->channel_layouts) {
  884. if (!avctx->channel_layout) {
  885. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  886. } else {
  887. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  888. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  889. break;
  890. if (avctx->codec->channel_layouts[i] == 0) {
  891. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  892. ret = AVERROR(EINVAL);
  893. goto free_and_end;
  894. }
  895. }
  896. }
  897. if (avctx->channel_layout && avctx->channels) {
  898. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  899. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  900. ret = AVERROR(EINVAL);
  901. goto free_and_end;
  902. }
  903. } else if (avctx->channel_layout) {
  904. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  905. }
  906. if (!avctx->rc_initial_buffer_occupancy)
  907. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  908. }
  909. if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  910. ret = avctx->codec->init(avctx);
  911. if (ret < 0) {
  912. goto free_and_end;
  913. }
  914. }
  915. #if FF_API_AUDIOENC_DELAY
  916. if (av_codec_is_encoder(avctx->codec))
  917. avctx->delay = avctx->initial_padding;
  918. #endif
  919. if (av_codec_is_decoder(avctx->codec)) {
  920. /* validate channel layout from the decoder */
  921. if (avctx->channel_layout) {
  922. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  923. if (!avctx->channels)
  924. avctx->channels = channels;
  925. else if (channels != avctx->channels) {
  926. av_log(avctx, AV_LOG_WARNING,
  927. "channel layout does not match number of channels\n");
  928. avctx->channel_layout = 0;
  929. }
  930. }
  931. if (avctx->channels && avctx->channels < 0 ||
  932. avctx->channels > FF_SANE_NB_CHANNELS) {
  933. ret = AVERROR(EINVAL);
  934. goto free_and_end;
  935. }
  936. #if FF_API_AVCTX_TIMEBASE
  937. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  938. avctx->time_base = av_inv_q(avctx->framerate);
  939. #endif
  940. }
  941. end:
  942. if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) {
  943. entangled_thread_counter--;
  944. /* Release any user-supplied mutex. */
  945. if (lockmgr_cb) {
  946. (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  947. }
  948. }
  949. if (options) {
  950. av_dict_free(options);
  951. *options = tmp;
  952. }
  953. return ret;
  954. free_and_end:
  955. if (avctx->codec &&
  956. (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
  957. avctx->codec->close(avctx);
  958. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  959. av_opt_free(avctx->priv_data);
  960. av_opt_free(avctx);
  961. #if FF_API_CODED_FRAME
  962. FF_DISABLE_DEPRECATION_WARNINGS
  963. av_frame_free(&avctx->coded_frame);
  964. FF_ENABLE_DEPRECATION_WARNINGS
  965. #endif
  966. av_dict_free(&tmp);
  967. av_freep(&avctx->priv_data);
  968. if (avctx->internal) {
  969. av_frame_free(&avctx->internal->to_free);
  970. av_freep(&avctx->internal->pool);
  971. }
  972. av_freep(&avctx->internal);
  973. avctx->codec = NULL;
  974. goto end;
  975. }
  976. int ff_alloc_packet(AVPacket *avpkt, int size)
  977. {
  978. if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
  979. return AVERROR(EINVAL);
  980. if (avpkt->data) {
  981. AVBufferRef *buf = avpkt->buf;
  982. if (avpkt->size < size)
  983. return AVERROR(EINVAL);
  984. av_init_packet(avpkt);
  985. avpkt->buf = buf;
  986. avpkt->size = size;
  987. return 0;
  988. } else {
  989. return av_new_packet(avpkt, size);
  990. }
  991. }
  992. /**
  993. * Pad last frame with silence.
  994. */
  995. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  996. {
  997. AVFrame *frame = NULL;
  998. int ret;
  999. if (!(frame = av_frame_alloc()))
  1000. return AVERROR(ENOMEM);
  1001. frame->format = src->format;
  1002. frame->channel_layout = src->channel_layout;
  1003. frame->nb_samples = s->frame_size;
  1004. ret = av_frame_get_buffer(frame, 32);
  1005. if (ret < 0)
  1006. goto fail;
  1007. ret = av_frame_copy_props(frame, src);
  1008. if (ret < 0)
  1009. goto fail;
  1010. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1011. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1012. goto fail;
  1013. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1014. frame->nb_samples - src->nb_samples,
  1015. s->channels, s->sample_fmt)) < 0)
  1016. goto fail;
  1017. *dst = frame;
  1018. return 0;
  1019. fail:
  1020. av_frame_free(&frame);
  1021. return ret;
  1022. }
  1023. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1024. AVPacket *avpkt,
  1025. const AVFrame *frame,
  1026. int *got_packet_ptr)
  1027. {
  1028. AVFrame tmp;
  1029. AVFrame *padded_frame = NULL;
  1030. int ret;
  1031. int user_packet = !!avpkt->data;
  1032. *got_packet_ptr = 0;
  1033. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  1034. av_packet_unref(avpkt);
  1035. av_init_packet(avpkt);
  1036. return 0;
  1037. }
  1038. /* ensure that extended_data is properly set */
  1039. if (frame && !frame->extended_data) {
  1040. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1041. avctx->channels > AV_NUM_DATA_POINTERS) {
  1042. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1043. "with more than %d channels, but extended_data is not set.\n",
  1044. AV_NUM_DATA_POINTERS);
  1045. return AVERROR(EINVAL);
  1046. }
  1047. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1048. tmp = *frame;
  1049. tmp.extended_data = tmp.data;
  1050. frame = &tmp;
  1051. }
  1052. /* extract audio service type metadata */
  1053. if (frame) {
  1054. AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
  1055. if (sd && sd->size >= sizeof(enum AVAudioServiceType))
  1056. avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
  1057. }
  1058. /* check for valid frame size */
  1059. if (frame) {
  1060. if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
  1061. if (frame->nb_samples > avctx->frame_size)
  1062. return AVERROR(EINVAL);
  1063. } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1064. if (frame->nb_samples < avctx->frame_size &&
  1065. !avctx->internal->last_audio_frame) {
  1066. ret = pad_last_frame(avctx, &padded_frame, frame);
  1067. if (ret < 0)
  1068. return ret;
  1069. frame = padded_frame;
  1070. avctx->internal->last_audio_frame = 1;
  1071. }
  1072. if (frame->nb_samples != avctx->frame_size) {
  1073. ret = AVERROR(EINVAL);
  1074. goto end;
  1075. }
  1076. }
  1077. }
  1078. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1079. if (!ret) {
  1080. if (*got_packet_ptr) {
  1081. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
  1082. if (avpkt->pts == AV_NOPTS_VALUE)
  1083. avpkt->pts = frame->pts;
  1084. if (!avpkt->duration)
  1085. avpkt->duration = ff_samples_to_time_base(avctx,
  1086. frame->nb_samples);
  1087. }
  1088. avpkt->dts = avpkt->pts;
  1089. } else {
  1090. avpkt->size = 0;
  1091. }
  1092. if (!user_packet && avpkt->size) {
  1093. ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
  1094. if (ret >= 0)
  1095. avpkt->data = avpkt->buf->data;
  1096. }
  1097. avctx->frame_number++;
  1098. }
  1099. if (ret < 0 || !*got_packet_ptr) {
  1100. av_packet_unref(avpkt);
  1101. av_init_packet(avpkt);
  1102. goto end;
  1103. }
  1104. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1105. * this needs to be moved to the encoders, but for now we can do it
  1106. * here to simplify things */
  1107. avpkt->flags |= AV_PKT_FLAG_KEY;
  1108. end:
  1109. av_frame_free(&padded_frame);
  1110. #if FF_API_AUDIOENC_DELAY
  1111. avctx->delay = avctx->initial_padding;
  1112. #endif
  1113. return ret;
  1114. }
  1115. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1116. AVPacket *avpkt,
  1117. const AVFrame *frame,
  1118. int *got_packet_ptr)
  1119. {
  1120. int ret;
  1121. int user_packet = !!avpkt->data;
  1122. *got_packet_ptr = 0;
  1123. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  1124. av_packet_unref(avpkt);
  1125. av_init_packet(avpkt);
  1126. avpkt->size = 0;
  1127. return 0;
  1128. }
  1129. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1130. return AVERROR(EINVAL);
  1131. av_assert0(avctx->codec->encode2);
  1132. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1133. if (!ret) {
  1134. if (!*got_packet_ptr)
  1135. avpkt->size = 0;
  1136. else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  1137. avpkt->pts = avpkt->dts = frame->pts;
  1138. if (!user_packet && avpkt->size) {
  1139. ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
  1140. if (ret >= 0)
  1141. avpkt->data = avpkt->buf->data;
  1142. }
  1143. avctx->frame_number++;
  1144. }
  1145. if (ret < 0 || !*got_packet_ptr)
  1146. av_packet_unref(avpkt);
  1147. emms_c();
  1148. return ret;
  1149. }
  1150. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1151. const AVSubtitle *sub)
  1152. {
  1153. int ret;
  1154. if (sub->start_display_time) {
  1155. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1156. return -1;
  1157. }
  1158. if (sub->num_rects == 0 || !sub->rects)
  1159. return -1;
  1160. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1161. avctx->frame_number++;
  1162. return ret;
  1163. }
  1164. static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1165. {
  1166. int size = 0, ret;
  1167. const uint8_t *data;
  1168. uint32_t flags;
  1169. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1170. if (!data)
  1171. return 0;
  1172. if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
  1173. av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
  1174. "changes, but PARAM_CHANGE side data was sent to it.\n");
  1175. return AVERROR(EINVAL);
  1176. }
  1177. if (size < 4)
  1178. goto fail;
  1179. flags = bytestream_get_le32(&data);
  1180. size -= 4;
  1181. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1182. if (size < 4)
  1183. goto fail;
  1184. avctx->channels = bytestream_get_le32(&data);
  1185. size -= 4;
  1186. }
  1187. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1188. if (size < 8)
  1189. goto fail;
  1190. avctx->channel_layout = bytestream_get_le64(&data);
  1191. size -= 8;
  1192. }
  1193. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1194. if (size < 4)
  1195. goto fail;
  1196. avctx->sample_rate = bytestream_get_le32(&data);
  1197. size -= 4;
  1198. }
  1199. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1200. if (size < 8)
  1201. goto fail;
  1202. avctx->width = bytestream_get_le32(&data);
  1203. avctx->height = bytestream_get_le32(&data);
  1204. size -= 8;
  1205. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1206. if (ret < 0)
  1207. return ret;
  1208. }
  1209. return 0;
  1210. fail:
  1211. av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
  1212. return AVERROR_INVALIDDATA;
  1213. }
  1214. static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
  1215. {
  1216. int ret;
  1217. /* move the original frame to our backup */
  1218. av_frame_unref(avci->to_free);
  1219. av_frame_move_ref(avci->to_free, frame);
  1220. /* now copy everything except the AVBufferRefs back
  1221. * note that we make a COPY of the side data, so calling av_frame_free() on
  1222. * the caller's frame will work properly */
  1223. ret = av_frame_copy_props(frame, avci->to_free);
  1224. if (ret < 0)
  1225. return ret;
  1226. memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
  1227. memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
  1228. if (avci->to_free->extended_data != avci->to_free->data) {
  1229. int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
  1230. int size = planes * sizeof(*frame->extended_data);
  1231. if (!size) {
  1232. av_frame_unref(frame);
  1233. return AVERROR_BUG;
  1234. }
  1235. frame->extended_data = av_malloc(size);
  1236. if (!frame->extended_data) {
  1237. av_frame_unref(frame);
  1238. return AVERROR(ENOMEM);
  1239. }
  1240. memcpy(frame->extended_data, avci->to_free->extended_data,
  1241. size);
  1242. } else
  1243. frame->extended_data = frame->data;
  1244. frame->format = avci->to_free->format;
  1245. frame->width = avci->to_free->width;
  1246. frame->height = avci->to_free->height;
  1247. frame->channel_layout = avci->to_free->channel_layout;
  1248. frame->nb_samples = avci->to_free->nb_samples;
  1249. return 0;
  1250. }
  1251. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1252. int *got_picture_ptr,
  1253. AVPacket *avpkt)
  1254. {
  1255. AVCodecInternal *avci = avctx->internal;
  1256. int ret;
  1257. *got_picture_ptr = 0;
  1258. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1259. return -1;
  1260. avctx->internal->pkt = avpkt;
  1261. ret = apply_param_change(avctx, avpkt);
  1262. if (ret < 0) {
  1263. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1264. if (avctx->err_recognition & AV_EF_EXPLODE)
  1265. return ret;
  1266. }
  1267. av_frame_unref(picture);
  1268. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size ||
  1269. (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1270. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1271. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1272. avpkt);
  1273. else {
  1274. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1275. avpkt);
  1276. if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
  1277. picture->pkt_dts = avpkt->dts;
  1278. /* get_buffer is supposed to set frame parameters */
  1279. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
  1280. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1281. picture->width = avctx->width;
  1282. picture->height = avctx->height;
  1283. picture->format = avctx->pix_fmt;
  1284. }
  1285. }
  1286. emms_c(); //needed to avoid an emms_c() call before every return;
  1287. if (*got_picture_ptr) {
  1288. if (!avctx->refcounted_frames) {
  1289. int err = unrefcount_frame(avci, picture);
  1290. if (err < 0)
  1291. return err;
  1292. }
  1293. avctx->frame_number++;
  1294. } else
  1295. av_frame_unref(picture);
  1296. } else
  1297. ret = 0;
  1298. #if FF_API_AVCTX_TIMEBASE
  1299. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  1300. avctx->time_base = av_inv_q(avctx->framerate);
  1301. #endif
  1302. return ret;
  1303. }
  1304. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1305. AVFrame *frame,
  1306. int *got_frame_ptr,
  1307. AVPacket *avpkt)
  1308. {
  1309. AVCodecInternal *avci = avctx->internal;
  1310. int ret = 0;
  1311. *got_frame_ptr = 0;
  1312. avctx->internal->pkt = avpkt;
  1313. if (!avpkt->data && avpkt->size) {
  1314. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1315. return AVERROR(EINVAL);
  1316. }
  1317. ret = apply_param_change(avctx, avpkt);
  1318. if (ret < 0) {
  1319. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1320. if (avctx->err_recognition & AV_EF_EXPLODE)
  1321. return ret;
  1322. }
  1323. av_frame_unref(frame);
  1324. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
  1325. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1326. if (ret >= 0 && *got_frame_ptr) {
  1327. avctx->frame_number++;
  1328. frame->pkt_dts = avpkt->dts;
  1329. if (frame->format == AV_SAMPLE_FMT_NONE)
  1330. frame->format = avctx->sample_fmt;
  1331. if (!avctx->refcounted_frames) {
  1332. int err = unrefcount_frame(avci, frame);
  1333. if (err < 0)
  1334. return err;
  1335. }
  1336. } else
  1337. av_frame_unref(frame);
  1338. }
  1339. return ret;
  1340. }
  1341. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1342. int *got_sub_ptr,
  1343. AVPacket *avpkt)
  1344. {
  1345. int ret;
  1346. avctx->internal->pkt = avpkt;
  1347. *got_sub_ptr = 0;
  1348. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1349. if (*got_sub_ptr)
  1350. avctx->frame_number++;
  1351. return ret;
  1352. }
  1353. void avsubtitle_free(AVSubtitle *sub)
  1354. {
  1355. int i;
  1356. for (i = 0; i < sub->num_rects; i++) {
  1357. av_freep(&sub->rects[i]->data[0]);
  1358. av_freep(&sub->rects[i]->data[1]);
  1359. av_freep(&sub->rects[i]->data[2]);
  1360. av_freep(&sub->rects[i]->data[3]);
  1361. av_freep(&sub->rects[i]->text);
  1362. av_freep(&sub->rects[i]->ass);
  1363. av_freep(&sub->rects[i]);
  1364. }
  1365. av_freep(&sub->rects);
  1366. memset(sub, 0, sizeof(AVSubtitle));
  1367. }
  1368. av_cold int avcodec_close(AVCodecContext *avctx)
  1369. {
  1370. int i;
  1371. if (avcodec_is_open(avctx)) {
  1372. FramePool *pool = avctx->internal->pool;
  1373. if (HAVE_THREADS && avctx->internal->thread_ctx)
  1374. ff_thread_free(avctx);
  1375. if (avctx->codec && avctx->codec->close)
  1376. avctx->codec->close(avctx);
  1377. av_frame_free(&avctx->internal->to_free);
  1378. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  1379. av_buffer_pool_uninit(&pool->pools[i]);
  1380. av_freep(&avctx->internal->pool);
  1381. if (avctx->hwaccel && avctx->hwaccel->uninit)
  1382. avctx->hwaccel->uninit(avctx);
  1383. av_freep(&avctx->internal->hwaccel_priv_data);
  1384. av_freep(&avctx->internal);
  1385. }
  1386. for (i = 0; i < avctx->nb_coded_side_data; i++)
  1387. av_freep(&avctx->coded_side_data[i].data);
  1388. av_freep(&avctx->coded_side_data);
  1389. avctx->nb_coded_side_data = 0;
  1390. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1391. av_opt_free(avctx->priv_data);
  1392. av_opt_free(avctx);
  1393. av_freep(&avctx->priv_data);
  1394. if (av_codec_is_encoder(avctx->codec)) {
  1395. av_freep(&avctx->extradata);
  1396. #if FF_API_CODED_FRAME
  1397. FF_DISABLE_DEPRECATION_WARNINGS
  1398. av_frame_free(&avctx->coded_frame);
  1399. FF_ENABLE_DEPRECATION_WARNINGS
  1400. #endif
  1401. }
  1402. avctx->codec = NULL;
  1403. avctx->active_thread_type = 0;
  1404. return 0;
  1405. }
  1406. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  1407. {
  1408. AVCodec *p, *experimental = NULL;
  1409. p = first_avcodec;
  1410. while (p) {
  1411. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  1412. p->id == id) {
  1413. if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
  1414. experimental = p;
  1415. } else
  1416. return p;
  1417. }
  1418. p = p->next;
  1419. }
  1420. return experimental;
  1421. }
  1422. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1423. {
  1424. return find_encdec(id, 1);
  1425. }
  1426. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1427. {
  1428. AVCodec *p;
  1429. if (!name)
  1430. return NULL;
  1431. p = first_avcodec;
  1432. while (p) {
  1433. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1434. return p;
  1435. p = p->next;
  1436. }
  1437. return NULL;
  1438. }
  1439. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1440. {
  1441. return find_encdec(id, 0);
  1442. }
  1443. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1444. {
  1445. AVCodec *p;
  1446. if (!name)
  1447. return NULL;
  1448. p = first_avcodec;
  1449. while (p) {
  1450. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1451. return p;
  1452. p = p->next;
  1453. }
  1454. return NULL;
  1455. }
  1456. static int get_bit_rate(AVCodecContext *ctx)
  1457. {
  1458. int bit_rate;
  1459. int bits_per_sample;
  1460. switch (ctx->codec_type) {
  1461. case AVMEDIA_TYPE_VIDEO:
  1462. case AVMEDIA_TYPE_DATA:
  1463. case AVMEDIA_TYPE_SUBTITLE:
  1464. case AVMEDIA_TYPE_ATTACHMENT:
  1465. bit_rate = ctx->bit_rate;
  1466. break;
  1467. case AVMEDIA_TYPE_AUDIO:
  1468. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1469. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1470. break;
  1471. default:
  1472. bit_rate = 0;
  1473. break;
  1474. }
  1475. return bit_rate;
  1476. }
  1477. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1478. {
  1479. int i, len, ret = 0;
  1480. #define TAG_PRINT(x) \
  1481. (((x) >= '0' && (x) <= '9') || \
  1482. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1483. ((x) == '.' || (x) == ' '))
  1484. for (i = 0; i < 4; i++) {
  1485. len = snprintf(buf, buf_size,
  1486. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1487. buf += len;
  1488. buf_size = buf_size > len ? buf_size - len : 0;
  1489. ret += len;
  1490. codec_tag >>= 8;
  1491. }
  1492. return ret;
  1493. }
  1494. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1495. {
  1496. const char *codec_name;
  1497. const char *profile = NULL;
  1498. char buf1[32];
  1499. int bitrate;
  1500. int new_line = 0;
  1501. AVRational display_aspect_ratio;
  1502. const AVCodecDescriptor *desc = avcodec_descriptor_get(enc->codec_id);
  1503. if (desc) {
  1504. codec_name = desc->name;
  1505. profile = avcodec_profile_name(enc->codec_id, enc->profile);
  1506. } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
  1507. /* fake mpeg2 transport stream codec (currently not
  1508. * registered) */
  1509. codec_name = "mpeg2ts";
  1510. } else {
  1511. /* output avi tags */
  1512. char tag_buf[32];
  1513. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1514. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1515. codec_name = buf1;
  1516. }
  1517. switch (enc->codec_type) {
  1518. case AVMEDIA_TYPE_VIDEO:
  1519. snprintf(buf, buf_size,
  1520. "Video: %s%s",
  1521. codec_name, enc->mb_decision ? " (hq)" : "");
  1522. if (profile)
  1523. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1524. " (%s)", profile);
  1525. if (enc->codec_tag) {
  1526. char tag_buf[32];
  1527. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1528. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1529. " [%s / 0x%04X]", tag_buf, enc->codec_tag);
  1530. }
  1531. av_strlcat(buf, "\n ", buf_size);
  1532. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1533. "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  1534. av_get_pix_fmt_name(enc->pix_fmt));
  1535. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  1536. snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
  1537. av_color_range_name(enc->color_range));
  1538. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  1539. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  1540. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  1541. new_line = 1;
  1542. snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s/%s/%s",
  1543. av_color_space_name(enc->colorspace),
  1544. av_color_primaries_name(enc->color_primaries),
  1545. av_color_transfer_name(enc->color_trc));
  1546. }
  1547. if (av_log_get_level() >= AV_LOG_DEBUG &&
  1548. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  1549. snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
  1550. av_chroma_location_name(enc->chroma_sample_location));
  1551. if (enc->width) {
  1552. av_strlcat(buf, new_line ? "\n " : ", ", buf_size);
  1553. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1554. "%dx%d",
  1555. enc->width, enc->height);
  1556. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  1557. (enc->width != enc->coded_width ||
  1558. enc->height != enc->coded_height))
  1559. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1560. " (%dx%d)", enc->coded_width, enc->coded_height);
  1561. if (enc->sample_aspect_ratio.num) {
  1562. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1563. enc->width * enc->sample_aspect_ratio.num,
  1564. enc->height * enc->sample_aspect_ratio.den,
  1565. 1024 * 1024);
  1566. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1567. " [PAR %d:%d DAR %d:%d]",
  1568. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1569. display_aspect_ratio.num, display_aspect_ratio.den);
  1570. }
  1571. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1572. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1573. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1574. ", %d/%d",
  1575. enc->time_base.num / g, enc->time_base.den / g);
  1576. }
  1577. }
  1578. if (encode) {
  1579. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1580. ", q=%d-%d", enc->qmin, enc->qmax);
  1581. }
  1582. break;
  1583. case AVMEDIA_TYPE_AUDIO:
  1584. snprintf(buf, buf_size,
  1585. "Audio: %s",
  1586. codec_name);
  1587. if (profile)
  1588. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1589. " (%s)", profile);
  1590. if (enc->codec_tag) {
  1591. char tag_buf[32];
  1592. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1593. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1594. " [%s / 0x%04X]", tag_buf, enc->codec_tag);
  1595. }
  1596. av_strlcat(buf, "\n ", buf_size);
  1597. if (enc->sample_rate) {
  1598. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1599. "%d Hz, ", enc->sample_rate);
  1600. }
  1601. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1602. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1603. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1604. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1605. }
  1606. break;
  1607. case AVMEDIA_TYPE_DATA:
  1608. snprintf(buf, buf_size, "Data: %s", codec_name);
  1609. break;
  1610. case AVMEDIA_TYPE_SUBTITLE:
  1611. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1612. break;
  1613. case AVMEDIA_TYPE_ATTACHMENT:
  1614. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1615. break;
  1616. default:
  1617. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1618. return;
  1619. }
  1620. if (encode) {
  1621. if (enc->flags & AV_CODEC_FLAG_PASS1)
  1622. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1623. ", pass 1");
  1624. if (enc->flags & AV_CODEC_FLAG_PASS2)
  1625. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1626. ", pass 2");
  1627. }
  1628. bitrate = get_bit_rate(enc);
  1629. if (bitrate != 0) {
  1630. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1631. ", %d kb/s", bitrate / 1000);
  1632. }
  1633. }
  1634. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1635. {
  1636. const AVProfile *p;
  1637. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1638. return NULL;
  1639. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1640. if (p->profile == profile)
  1641. return p->name;
  1642. return NULL;
  1643. }
  1644. const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
  1645. {
  1646. const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
  1647. const AVProfile *p;
  1648. if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
  1649. return NULL;
  1650. for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1651. if (p->profile == profile)
  1652. return p->name;
  1653. return NULL;
  1654. }
  1655. unsigned avcodec_version(void)
  1656. {
  1657. return LIBAVCODEC_VERSION_INT;
  1658. }
  1659. const char *avcodec_configuration(void)
  1660. {
  1661. return LIBAV_CONFIGURATION;
  1662. }
  1663. const char *avcodec_license(void)
  1664. {
  1665. #define LICENSE_PREFIX "libavcodec license: "
  1666. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1667. }
  1668. void avcodec_flush_buffers(AVCodecContext *avctx)
  1669. {
  1670. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1671. ff_thread_flush(avctx);
  1672. else if (avctx->codec->flush)
  1673. avctx->codec->flush(avctx);
  1674. if (!avctx->refcounted_frames)
  1675. av_frame_unref(avctx->internal->to_free);
  1676. }
  1677. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1678. {
  1679. switch (codec_id) {
  1680. case AV_CODEC_ID_ADPCM_CT:
  1681. case AV_CODEC_ID_ADPCM_IMA_APC:
  1682. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1683. case AV_CODEC_ID_ADPCM_IMA_WS:
  1684. case AV_CODEC_ID_ADPCM_G722:
  1685. case AV_CODEC_ID_ADPCM_YAMAHA:
  1686. return 4;
  1687. case AV_CODEC_ID_PCM_ALAW:
  1688. case AV_CODEC_ID_PCM_MULAW:
  1689. case AV_CODEC_ID_PCM_S8:
  1690. case AV_CODEC_ID_PCM_U8:
  1691. case AV_CODEC_ID_PCM_ZORK:
  1692. return 8;
  1693. case AV_CODEC_ID_PCM_S16BE:
  1694. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  1695. case AV_CODEC_ID_PCM_S16LE:
  1696. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1697. case AV_CODEC_ID_PCM_U16BE:
  1698. case AV_CODEC_ID_PCM_U16LE:
  1699. return 16;
  1700. case AV_CODEC_ID_PCM_S24DAUD:
  1701. case AV_CODEC_ID_PCM_S24BE:
  1702. case AV_CODEC_ID_PCM_S24LE:
  1703. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  1704. case AV_CODEC_ID_PCM_U24BE:
  1705. case AV_CODEC_ID_PCM_U24LE:
  1706. return 24;
  1707. case AV_CODEC_ID_PCM_S32BE:
  1708. case AV_CODEC_ID_PCM_S32LE:
  1709. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  1710. case AV_CODEC_ID_PCM_U32BE:
  1711. case AV_CODEC_ID_PCM_U32LE:
  1712. case AV_CODEC_ID_PCM_F32BE:
  1713. case AV_CODEC_ID_PCM_F32LE:
  1714. return 32;
  1715. case AV_CODEC_ID_PCM_F64BE:
  1716. case AV_CODEC_ID_PCM_F64LE:
  1717. return 64;
  1718. default:
  1719. return 0;
  1720. }
  1721. }
  1722. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1723. {
  1724. switch (codec_id) {
  1725. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1726. return 2;
  1727. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1728. return 3;
  1729. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1730. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1731. case AV_CODEC_ID_ADPCM_IMA_QT:
  1732. case AV_CODEC_ID_ADPCM_SWF:
  1733. case AV_CODEC_ID_ADPCM_MS:
  1734. return 4;
  1735. default:
  1736. return av_get_exact_bits_per_sample(codec_id);
  1737. }
  1738. }
  1739. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1740. {
  1741. int id, sr, ch, ba, tag, bps;
  1742. id = avctx->codec_id;
  1743. sr = avctx->sample_rate;
  1744. ch = avctx->channels;
  1745. ba = avctx->block_align;
  1746. tag = avctx->codec_tag;
  1747. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1748. /* codecs with an exact constant bits per sample */
  1749. if (bps > 0 && ch > 0 && frame_bytes > 0)
  1750. return (frame_bytes * 8) / (bps * ch);
  1751. bps = avctx->bits_per_coded_sample;
  1752. /* codecs with a fixed packet duration */
  1753. switch (id) {
  1754. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1755. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1756. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1757. case AV_CODEC_ID_AMR_NB:
  1758. case AV_CODEC_ID_GSM:
  1759. case AV_CODEC_ID_QCELP:
  1760. case AV_CODEC_ID_RA_144:
  1761. case AV_CODEC_ID_RA_288: return 160;
  1762. case AV_CODEC_ID_IMC: return 256;
  1763. case AV_CODEC_ID_AMR_WB:
  1764. case AV_CODEC_ID_GSM_MS: return 320;
  1765. case AV_CODEC_ID_MP1: return 384;
  1766. case AV_CODEC_ID_ATRAC1: return 512;
  1767. case AV_CODEC_ID_ATRAC3: return 1024;
  1768. case AV_CODEC_ID_MP2:
  1769. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1770. case AV_CODEC_ID_AC3: return 1536;
  1771. }
  1772. if (sr > 0) {
  1773. /* calc from sample rate */
  1774. if (id == AV_CODEC_ID_TTA)
  1775. return 256 * sr / 245;
  1776. if (ch > 0) {
  1777. /* calc from sample rate and channels */
  1778. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  1779. return (480 << (sr / 22050)) / ch;
  1780. }
  1781. }
  1782. if (ba > 0) {
  1783. /* calc from block_align */
  1784. if (id == AV_CODEC_ID_SIPR) {
  1785. switch (ba) {
  1786. case 20: return 160;
  1787. case 19: return 144;
  1788. case 29: return 288;
  1789. case 37: return 480;
  1790. }
  1791. } else if (id == AV_CODEC_ID_ILBC) {
  1792. switch (ba) {
  1793. case 38: return 160;
  1794. case 50: return 240;
  1795. }
  1796. }
  1797. }
  1798. if (frame_bytes > 0) {
  1799. /* calc from frame_bytes only */
  1800. if (id == AV_CODEC_ID_TRUESPEECH)
  1801. return 240 * (frame_bytes / 32);
  1802. if (id == AV_CODEC_ID_NELLYMOSER)
  1803. return 256 * (frame_bytes / 64);
  1804. if (bps > 0) {
  1805. /* calc from frame_bytes and bits_per_coded_sample */
  1806. if (id == AV_CODEC_ID_ADPCM_G726)
  1807. return frame_bytes * 8 / bps;
  1808. }
  1809. if (ch > 0) {
  1810. /* calc from frame_bytes and channels */
  1811. switch (id) {
  1812. case AV_CODEC_ID_ADPCM_4XM:
  1813. case AV_CODEC_ID_ADPCM_IMA_ISS:
  1814. return (frame_bytes - 4 * ch) * 2 / ch;
  1815. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1816. return (frame_bytes - 4) * 2 / ch;
  1817. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1818. return (frame_bytes - 8) * 2 / ch;
  1819. case AV_CODEC_ID_ADPCM_XA:
  1820. return (frame_bytes / 128) * 224 / ch;
  1821. case AV_CODEC_ID_INTERPLAY_DPCM:
  1822. return (frame_bytes - 6 - ch) / ch;
  1823. case AV_CODEC_ID_ROQ_DPCM:
  1824. return (frame_bytes - 8) / ch;
  1825. case AV_CODEC_ID_XAN_DPCM:
  1826. return (frame_bytes - 2 * ch) / ch;
  1827. case AV_CODEC_ID_MACE3:
  1828. return 3 * frame_bytes / ch;
  1829. case AV_CODEC_ID_MACE6:
  1830. return 6 * frame_bytes / ch;
  1831. case AV_CODEC_ID_PCM_LXF:
  1832. return 2 * (frame_bytes / (5 * ch));
  1833. }
  1834. if (tag) {
  1835. /* calc from frame_bytes, channels, and codec_tag */
  1836. if (id == AV_CODEC_ID_SOL_DPCM) {
  1837. if (tag == 3)
  1838. return frame_bytes / ch;
  1839. else
  1840. return frame_bytes * 2 / ch;
  1841. }
  1842. }
  1843. if (ba > 0) {
  1844. /* calc from frame_bytes, channels, and block_align */
  1845. int blocks = frame_bytes / ba;
  1846. switch (avctx->codec_id) {
  1847. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1848. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1849. case AV_CODEC_ID_ADPCM_IMA_DK3:
  1850. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1851. case AV_CODEC_ID_ADPCM_IMA_DK4:
  1852. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1853. case AV_CODEC_ID_ADPCM_MS:
  1854. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1855. }
  1856. }
  1857. if (bps > 0) {
  1858. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1859. switch (avctx->codec_id) {
  1860. case AV_CODEC_ID_PCM_DVD:
  1861. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1862. case AV_CODEC_ID_PCM_BLURAY:
  1863. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1864. case AV_CODEC_ID_S302M:
  1865. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1866. }
  1867. }
  1868. }
  1869. }
  1870. return 0;
  1871. }
  1872. #if !HAVE_THREADS
  1873. int ff_thread_init(AVCodecContext *s)
  1874. {
  1875. return -1;
  1876. }
  1877. #endif
  1878. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1879. {
  1880. unsigned int n = 0;
  1881. while (v >= 0xff) {
  1882. *s++ = 0xff;
  1883. v -= 0xff;
  1884. n++;
  1885. }
  1886. *s = v;
  1887. n++;
  1888. return n;
  1889. }
  1890. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  1891. {
  1892. int i;
  1893. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  1894. return i;
  1895. }
  1896. #if FF_API_MISSING_SAMPLE
  1897. FF_DISABLE_DEPRECATION_WARNINGS
  1898. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1899. {
  1900. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
  1901. "version to the newest one from Git. If the problem still "
  1902. "occurs, it means that your file has a feature which has not "
  1903. "been implemented.\n", feature);
  1904. if(want_sample)
  1905. av_log_ask_for_sample(avc, NULL);
  1906. }
  1907. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1908. {
  1909. va_list argument_list;
  1910. va_start(argument_list, msg);
  1911. if (msg)
  1912. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1913. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1914. "of this file to ftp://upload.libav.org/incoming/ "
  1915. "and contact the libav-devel mailing list.\n");
  1916. va_end(argument_list);
  1917. }
  1918. FF_ENABLE_DEPRECATION_WARNINGS
  1919. #endif /* FF_API_MISSING_SAMPLE */
  1920. static AVHWAccel *first_hwaccel = NULL;
  1921. void av_register_hwaccel(AVHWAccel *hwaccel)
  1922. {
  1923. AVHWAccel **p = &first_hwaccel;
  1924. while (*p)
  1925. p = &(*p)->next;
  1926. *p = hwaccel;
  1927. hwaccel->next = NULL;
  1928. }
  1929. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  1930. {
  1931. return hwaccel ? hwaccel->next : first_hwaccel;
  1932. }
  1933. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1934. {
  1935. if (lockmgr_cb) {
  1936. // There is no good way to rollback a failure to destroy the
  1937. // mutex, so we ignore failures.
  1938. lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
  1939. lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
  1940. lockmgr_cb = NULL;
  1941. codec_mutex = NULL;
  1942. avformat_mutex = NULL;
  1943. }
  1944. if (cb) {
  1945. void *new_codec_mutex = NULL;
  1946. void *new_avformat_mutex = NULL;
  1947. int err;
  1948. if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
  1949. return err > 0 ? AVERROR_UNKNOWN : err;
  1950. }
  1951. if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
  1952. // Ignore failures to destroy the newly created mutex.
  1953. cb(&new_codec_mutex, AV_LOCK_DESTROY);
  1954. return err > 0 ? AVERROR_UNKNOWN : err;
  1955. }
  1956. lockmgr_cb = cb;
  1957. codec_mutex = new_codec_mutex;
  1958. avformat_mutex = new_avformat_mutex;
  1959. }
  1960. return 0;
  1961. }
  1962. int avpriv_lock_avformat(void)
  1963. {
  1964. if (lockmgr_cb) {
  1965. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1966. return -1;
  1967. }
  1968. return 0;
  1969. }
  1970. int avpriv_unlock_avformat(void)
  1971. {
  1972. if (lockmgr_cb) {
  1973. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1974. return -1;
  1975. }
  1976. return 0;
  1977. }
  1978. unsigned int avpriv_toupper4(unsigned int x)
  1979. {
  1980. return av_toupper(x & 0xFF) +
  1981. (av_toupper((x >> 8) & 0xFF) << 8) +
  1982. (av_toupper((x >> 16) & 0xFF) << 16) +
  1983. (av_toupper((x >> 24) & 0xFF) << 24);
  1984. }
  1985. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  1986. {
  1987. int ret;
  1988. dst->owner = src->owner;
  1989. ret = av_frame_ref(dst->f, src->f);
  1990. if (ret < 0)
  1991. return ret;
  1992. if (src->progress &&
  1993. !(dst->progress = av_buffer_ref(src->progress))) {
  1994. ff_thread_release_buffer(dst->owner, dst);
  1995. return AVERROR(ENOMEM);
  1996. }
  1997. return 0;
  1998. }
  1999. #if !HAVE_THREADS
  2000. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  2001. {
  2002. f->owner = avctx;
  2003. return ff_get_buffer(avctx, f->f, flags);
  2004. }
  2005. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  2006. {
  2007. if (f->f)
  2008. av_frame_unref(f->f);
  2009. }
  2010. void ff_thread_finish_setup(AVCodecContext *avctx)
  2011. {
  2012. }
  2013. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  2014. {
  2015. }
  2016. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  2017. {
  2018. }
  2019. #endif
  2020. int avcodec_is_open(AVCodecContext *s)
  2021. {
  2022. return !!s->internal;
  2023. }
  2024. const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
  2025. const uint8_t *end,
  2026. uint32_t * restrict state)
  2027. {
  2028. int i;
  2029. assert(p <= end);
  2030. if (p >= end)
  2031. return end;
  2032. for (i = 0; i < 3; i++) {
  2033. uint32_t tmp = *state << 8;
  2034. *state = tmp + *(p++);
  2035. if (tmp == 0x100 || p == end)
  2036. return p;
  2037. }
  2038. while (p < end) {
  2039. if (p[-1] > 1 ) p += 3;
  2040. else if (p[-2] ) p += 2;
  2041. else if (p[-3]|(p[-1]-1)) p++;
  2042. else {
  2043. p++;
  2044. break;
  2045. }
  2046. }
  2047. p = FFMIN(p, end) - 4;
  2048. *state = AV_RB32(p);
  2049. return p + 4;
  2050. }
  2051. AVCPBProperties *av_cpb_properties_alloc(size_t *size)
  2052. {
  2053. AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
  2054. if (!props)
  2055. return NULL;
  2056. if (size)
  2057. *size = sizeof(*props);
  2058. props->vbv_delay = UINT64_MAX;
  2059. return props;
  2060. }
  2061. AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
  2062. {
  2063. AVPacketSideData *tmp;
  2064. AVCPBProperties *props;
  2065. size_t size;
  2066. props = av_cpb_properties_alloc(&size);
  2067. if (!props)
  2068. return NULL;
  2069. tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
  2070. if (!tmp) {
  2071. av_freep(&props);
  2072. return NULL;
  2073. }
  2074. avctx->coded_side_data = tmp;
  2075. avctx->nb_coded_side_data++;
  2076. avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
  2077. avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
  2078. avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
  2079. return props;
  2080. }