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.

2607 lines
79KB

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