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.

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