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.

2891 lines
88KB

  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_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. avctx->internal->buffer_frame = av_frame_alloc();
  753. if (!avctx->internal->buffer_frame) {
  754. ret = AVERROR(ENOMEM);
  755. goto free_and_end;
  756. }
  757. avctx->internal->buffer_pkt = av_packet_alloc();
  758. if (!avctx->internal->buffer_pkt) {
  759. ret = AVERROR(ENOMEM);
  760. goto free_and_end;
  761. }
  762. if (codec->priv_data_size > 0) {
  763. if (!avctx->priv_data) {
  764. avctx->priv_data = av_mallocz(codec->priv_data_size);
  765. if (!avctx->priv_data) {
  766. ret = AVERROR(ENOMEM);
  767. goto end;
  768. }
  769. if (codec->priv_class) {
  770. *(const AVClass **)avctx->priv_data = codec->priv_class;
  771. av_opt_set_defaults(avctx->priv_data);
  772. }
  773. }
  774. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  775. goto free_and_end;
  776. } else {
  777. avctx->priv_data = NULL;
  778. }
  779. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  780. goto free_and_end;
  781. if (avctx->coded_width && avctx->coded_height && !avctx->width && !avctx->height)
  782. ret = ff_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  783. else if (avctx->width && avctx->height)
  784. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  785. if (ret < 0)
  786. goto free_and_end;
  787. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  788. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  789. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  790. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  791. ff_set_dimensions(avctx, 0, 0);
  792. }
  793. if (avctx->width > 0 && avctx->height > 0) {
  794. if (av_image_check_sar(avctx->width, avctx->height,
  795. avctx->sample_aspect_ratio) < 0) {
  796. av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
  797. avctx->sample_aspect_ratio.num,
  798. avctx->sample_aspect_ratio.den);
  799. avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
  800. }
  801. }
  802. /* if the decoder init function was already called previously,
  803. * free the already allocated subtitle_header before overwriting it */
  804. if (av_codec_is_decoder(codec))
  805. av_freep(&avctx->subtitle_header);
  806. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  807. ret = AVERROR(EINVAL);
  808. goto free_and_end;
  809. }
  810. avctx->codec = codec;
  811. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  812. avctx->codec_id == AV_CODEC_ID_NONE) {
  813. avctx->codec_type = codec->type;
  814. avctx->codec_id = codec->id;
  815. }
  816. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  817. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  818. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  819. ret = AVERROR(EINVAL);
  820. goto free_and_end;
  821. }
  822. avctx->frame_number = 0;
  823. if ((avctx->codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
  824. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  825. ret = AVERROR_EXPERIMENTAL;
  826. goto free_and_end;
  827. }
  828. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  829. (!avctx->time_base.num || !avctx->time_base.den)) {
  830. avctx->time_base.num = 1;
  831. avctx->time_base.den = avctx->sample_rate;
  832. }
  833. if (HAVE_THREADS) {
  834. ret = ff_thread_init(avctx);
  835. if (ret < 0) {
  836. goto free_and_end;
  837. }
  838. }
  839. if (!HAVE_THREADS && !(codec->capabilities & AV_CODEC_CAP_AUTO_THREADS))
  840. avctx->thread_count = 1;
  841. if (av_codec_is_encoder(avctx->codec)) {
  842. int i;
  843. #if FF_API_CODED_FRAME
  844. FF_DISABLE_DEPRECATION_WARNINGS
  845. avctx->coded_frame = av_frame_alloc();
  846. if (!avctx->coded_frame) {
  847. ret = AVERROR(ENOMEM);
  848. goto free_and_end;
  849. }
  850. FF_ENABLE_DEPRECATION_WARNINGS
  851. #endif
  852. if (avctx->time_base.num <= 0 || avctx->time_base.den <= 0) {
  853. av_log(avctx, AV_LOG_ERROR, "The encoder timebase is not set.\n");
  854. ret = AVERROR(EINVAL);
  855. goto free_and_end;
  856. }
  857. if (avctx->codec->sample_fmts) {
  858. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  859. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  860. break;
  861. if (avctx->channels == 1 &&
  862. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  863. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  864. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  865. break;
  866. }
  867. }
  868. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  869. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  870. ret = AVERROR(EINVAL);
  871. goto free_and_end;
  872. }
  873. }
  874. if (avctx->codec->pix_fmts) {
  875. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  876. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  877. break;
  878. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
  879. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  880. ret = AVERROR(EINVAL);
  881. goto free_and_end;
  882. }
  883. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
  884. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ422P ||
  885. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ440P ||
  886. avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ444P)
  887. avctx->color_range = AVCOL_RANGE_JPEG;
  888. }
  889. if (avctx->codec->supported_samplerates) {
  890. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  891. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  892. break;
  893. if (avctx->codec->supported_samplerates[i] == 0) {
  894. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  895. ret = AVERROR(EINVAL);
  896. goto free_and_end;
  897. }
  898. }
  899. if (avctx->codec->channel_layouts) {
  900. if (!avctx->channel_layout) {
  901. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  902. } else {
  903. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  904. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  905. break;
  906. if (avctx->codec->channel_layouts[i] == 0) {
  907. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  908. ret = AVERROR(EINVAL);
  909. goto free_and_end;
  910. }
  911. }
  912. }
  913. if (avctx->channel_layout && avctx->channels) {
  914. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  915. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  916. ret = AVERROR(EINVAL);
  917. goto free_and_end;
  918. }
  919. } else if (avctx->channel_layout) {
  920. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  921. }
  922. if (!avctx->rc_initial_buffer_occupancy)
  923. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  924. if (avctx->ticks_per_frame &&
  925. avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
  926. av_log(avctx, AV_LOG_ERROR,
  927. "ticks_per_frame %d too large for the timebase %d/%d.",
  928. avctx->ticks_per_frame,
  929. avctx->time_base.num,
  930. avctx->time_base.den);
  931. goto free_and_end;
  932. }
  933. if (avctx->hw_frames_ctx) {
  934. AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
  935. if (frames_ctx->format != avctx->pix_fmt) {
  936. av_log(avctx, AV_LOG_ERROR,
  937. "Mismatching AVCodecContext.pix_fmt and AVHWFramesContext.format\n");
  938. ret = AVERROR(EINVAL);
  939. goto free_and_end;
  940. }
  941. }
  942. }
  943. if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  944. ret = avctx->codec->init(avctx);
  945. if (ret < 0) {
  946. goto free_and_end;
  947. }
  948. }
  949. #if FF_API_AUDIOENC_DELAY
  950. if (av_codec_is_encoder(avctx->codec))
  951. avctx->delay = avctx->initial_padding;
  952. #endif
  953. if (av_codec_is_decoder(avctx->codec)) {
  954. /* validate channel layout from the decoder */
  955. if (avctx->channel_layout) {
  956. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  957. if (!avctx->channels)
  958. avctx->channels = channels;
  959. else if (channels != avctx->channels) {
  960. av_log(avctx, AV_LOG_WARNING,
  961. "channel layout does not match number of channels\n");
  962. avctx->channel_layout = 0;
  963. }
  964. }
  965. if (avctx->channels && avctx->channels < 0 ||
  966. avctx->channels > FF_SANE_NB_CHANNELS) {
  967. ret = AVERROR(EINVAL);
  968. goto free_and_end;
  969. }
  970. #if FF_API_AVCTX_TIMEBASE
  971. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  972. avctx->time_base = av_inv_q(avctx->framerate);
  973. #endif
  974. }
  975. end:
  976. if (!(codec->caps_internal & FF_CODEC_CAP_INIT_THREADSAFE) && codec->init) {
  977. entangled_thread_counter--;
  978. /* Release any user-supplied mutex. */
  979. if (lockmgr_cb) {
  980. (*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  981. }
  982. }
  983. if (options) {
  984. av_dict_free(options);
  985. *options = tmp;
  986. }
  987. return ret;
  988. free_and_end:
  989. if (avctx->codec &&
  990. (avctx->codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP))
  991. avctx->codec->close(avctx);
  992. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  993. av_opt_free(avctx->priv_data);
  994. av_opt_free(avctx);
  995. #if FF_API_CODED_FRAME
  996. FF_DISABLE_DEPRECATION_WARNINGS
  997. av_frame_free(&avctx->coded_frame);
  998. FF_ENABLE_DEPRECATION_WARNINGS
  999. #endif
  1000. av_dict_free(&tmp);
  1001. av_freep(&avctx->priv_data);
  1002. if (avctx->internal) {
  1003. av_frame_free(&avctx->internal->to_free);
  1004. av_freep(&avctx->internal->pool);
  1005. }
  1006. av_freep(&avctx->internal);
  1007. avctx->codec = NULL;
  1008. goto end;
  1009. }
  1010. int ff_alloc_packet(AVPacket *avpkt, int size)
  1011. {
  1012. if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
  1013. return AVERROR(EINVAL);
  1014. if (avpkt->data) {
  1015. AVBufferRef *buf = avpkt->buf;
  1016. if (avpkt->size < size)
  1017. return AVERROR(EINVAL);
  1018. av_init_packet(avpkt);
  1019. avpkt->buf = buf;
  1020. avpkt->size = size;
  1021. return 0;
  1022. } else {
  1023. return av_new_packet(avpkt, size);
  1024. }
  1025. }
  1026. /**
  1027. * Pad last frame with silence.
  1028. */
  1029. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1030. {
  1031. AVFrame *frame = NULL;
  1032. int ret;
  1033. if (!(frame = av_frame_alloc()))
  1034. return AVERROR(ENOMEM);
  1035. frame->format = src->format;
  1036. frame->channel_layout = src->channel_layout;
  1037. frame->nb_samples = s->frame_size;
  1038. ret = av_frame_get_buffer(frame, 32);
  1039. if (ret < 0)
  1040. goto fail;
  1041. ret = av_frame_copy_props(frame, src);
  1042. if (ret < 0)
  1043. goto fail;
  1044. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1045. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1046. goto fail;
  1047. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1048. frame->nb_samples - src->nb_samples,
  1049. s->channels, s->sample_fmt)) < 0)
  1050. goto fail;
  1051. *dst = frame;
  1052. return 0;
  1053. fail:
  1054. av_frame_free(&frame);
  1055. return ret;
  1056. }
  1057. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1058. AVPacket *avpkt,
  1059. const AVFrame *frame,
  1060. int *got_packet_ptr)
  1061. {
  1062. AVFrame tmp;
  1063. AVFrame *padded_frame = NULL;
  1064. int ret;
  1065. int user_packet = !!avpkt->data;
  1066. *got_packet_ptr = 0;
  1067. if (!avctx->codec->encode2) {
  1068. av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
  1069. return AVERROR(ENOSYS);
  1070. }
  1071. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  1072. av_packet_unref(avpkt);
  1073. av_init_packet(avpkt);
  1074. return 0;
  1075. }
  1076. /* ensure that extended_data is properly set */
  1077. if (frame && !frame->extended_data) {
  1078. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1079. avctx->channels > AV_NUM_DATA_POINTERS) {
  1080. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1081. "with more than %d channels, but extended_data is not set.\n",
  1082. AV_NUM_DATA_POINTERS);
  1083. return AVERROR(EINVAL);
  1084. }
  1085. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1086. tmp = *frame;
  1087. tmp.extended_data = tmp.data;
  1088. frame = &tmp;
  1089. }
  1090. /* extract audio service type metadata */
  1091. if (frame) {
  1092. AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_AUDIO_SERVICE_TYPE);
  1093. if (sd && sd->size >= sizeof(enum AVAudioServiceType))
  1094. avctx->audio_service_type = *(enum AVAudioServiceType*)sd->data;
  1095. }
  1096. /* check for valid frame size */
  1097. if (frame) {
  1098. if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
  1099. if (frame->nb_samples > avctx->frame_size)
  1100. return AVERROR(EINVAL);
  1101. } else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1102. if (frame->nb_samples < avctx->frame_size &&
  1103. !avctx->internal->last_audio_frame) {
  1104. ret = pad_last_frame(avctx, &padded_frame, frame);
  1105. if (ret < 0)
  1106. return ret;
  1107. frame = padded_frame;
  1108. avctx->internal->last_audio_frame = 1;
  1109. }
  1110. if (frame->nb_samples != avctx->frame_size) {
  1111. ret = AVERROR(EINVAL);
  1112. goto end;
  1113. }
  1114. }
  1115. }
  1116. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1117. if (!ret) {
  1118. if (*got_packet_ptr) {
  1119. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
  1120. if (avpkt->pts == AV_NOPTS_VALUE)
  1121. avpkt->pts = frame->pts;
  1122. if (!avpkt->duration)
  1123. avpkt->duration = ff_samples_to_time_base(avctx,
  1124. frame->nb_samples);
  1125. }
  1126. avpkt->dts = avpkt->pts;
  1127. } else {
  1128. avpkt->size = 0;
  1129. }
  1130. if (!user_packet && avpkt->size) {
  1131. ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
  1132. if (ret >= 0)
  1133. avpkt->data = avpkt->buf->data;
  1134. }
  1135. avctx->frame_number++;
  1136. }
  1137. if (ret < 0 || !*got_packet_ptr) {
  1138. av_packet_unref(avpkt);
  1139. av_init_packet(avpkt);
  1140. goto end;
  1141. }
  1142. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1143. * this needs to be moved to the encoders, but for now we can do it
  1144. * here to simplify things */
  1145. avpkt->flags |= AV_PKT_FLAG_KEY;
  1146. end:
  1147. av_frame_free(&padded_frame);
  1148. #if FF_API_AUDIOENC_DELAY
  1149. avctx->delay = avctx->initial_padding;
  1150. #endif
  1151. return ret;
  1152. }
  1153. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1154. AVPacket *avpkt,
  1155. const AVFrame *frame,
  1156. int *got_packet_ptr)
  1157. {
  1158. int ret;
  1159. int user_packet = !!avpkt->data;
  1160. *got_packet_ptr = 0;
  1161. if (!avctx->codec->encode2) {
  1162. av_log(avctx, AV_LOG_ERROR, "This encoder requires using the avcodec_send_frame() API.\n");
  1163. return AVERROR(ENOSYS);
  1164. }
  1165. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY) && !frame) {
  1166. av_packet_unref(avpkt);
  1167. av_init_packet(avpkt);
  1168. avpkt->size = 0;
  1169. return 0;
  1170. }
  1171. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1172. return AVERROR(EINVAL);
  1173. av_assert0(avctx->codec->encode2);
  1174. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1175. if (!ret) {
  1176. if (!*got_packet_ptr)
  1177. avpkt->size = 0;
  1178. else if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  1179. avpkt->pts = avpkt->dts = frame->pts;
  1180. if (!user_packet && avpkt->size) {
  1181. ret = av_buffer_realloc(&avpkt->buf, avpkt->size);
  1182. if (ret >= 0)
  1183. avpkt->data = avpkt->buf->data;
  1184. }
  1185. avctx->frame_number++;
  1186. }
  1187. if (ret < 0 || !*got_packet_ptr)
  1188. av_packet_unref(avpkt);
  1189. emms_c();
  1190. return ret;
  1191. }
  1192. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1193. const AVSubtitle *sub)
  1194. {
  1195. int ret;
  1196. if (sub->start_display_time) {
  1197. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1198. return -1;
  1199. }
  1200. if (sub->num_rects == 0 || !sub->rects)
  1201. return -1;
  1202. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1203. avctx->frame_number++;
  1204. return ret;
  1205. }
  1206. static int apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1207. {
  1208. int size = 0, ret;
  1209. const uint8_t *data;
  1210. uint32_t flags;
  1211. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1212. if (!data)
  1213. return 0;
  1214. if (!(avctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE)) {
  1215. av_log(avctx, AV_LOG_ERROR, "This decoder does not support parameter "
  1216. "changes, but PARAM_CHANGE side data was sent to it.\n");
  1217. ret = AVERROR(EINVAL);
  1218. goto fail2;
  1219. }
  1220. if (size < 4)
  1221. goto fail;
  1222. flags = bytestream_get_le32(&data);
  1223. size -= 4;
  1224. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1225. if (size < 4)
  1226. goto fail;
  1227. avctx->channels = bytestream_get_le32(&data);
  1228. size -= 4;
  1229. }
  1230. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1231. if (size < 8)
  1232. goto fail;
  1233. avctx->channel_layout = bytestream_get_le64(&data);
  1234. size -= 8;
  1235. }
  1236. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1237. if (size < 4)
  1238. goto fail;
  1239. avctx->sample_rate = bytestream_get_le32(&data);
  1240. size -= 4;
  1241. }
  1242. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1243. if (size < 8)
  1244. goto fail;
  1245. avctx->width = bytestream_get_le32(&data);
  1246. avctx->height = bytestream_get_le32(&data);
  1247. size -= 8;
  1248. ret = ff_set_dimensions(avctx, avctx->width, avctx->height);
  1249. if (ret < 0)
  1250. goto fail2;
  1251. }
  1252. return 0;
  1253. fail:
  1254. av_log(avctx, AV_LOG_ERROR, "PARAM_CHANGE side data too small.\n");
  1255. ret = AVERROR_INVALIDDATA;
  1256. fail2:
  1257. if (ret < 0) {
  1258. av_log(avctx, AV_LOG_ERROR, "Error applying parameter changes.\n");
  1259. if (avctx->err_recognition & AV_EF_EXPLODE)
  1260. return ret;
  1261. }
  1262. return 0;
  1263. }
  1264. static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame)
  1265. {
  1266. int ret;
  1267. /* move the original frame to our backup */
  1268. av_frame_unref(avci->to_free);
  1269. av_frame_move_ref(avci->to_free, frame);
  1270. /* now copy everything except the AVBufferRefs back
  1271. * note that we make a COPY of the side data, so calling av_frame_free() on
  1272. * the caller's frame will work properly */
  1273. ret = av_frame_copy_props(frame, avci->to_free);
  1274. if (ret < 0)
  1275. return ret;
  1276. memcpy(frame->data, avci->to_free->data, sizeof(frame->data));
  1277. memcpy(frame->linesize, avci->to_free->linesize, sizeof(frame->linesize));
  1278. if (avci->to_free->extended_data != avci->to_free->data) {
  1279. int planes = av_get_channel_layout_nb_channels(avci->to_free->channel_layout);
  1280. int size = planes * sizeof(*frame->extended_data);
  1281. if (!size) {
  1282. av_frame_unref(frame);
  1283. return AVERROR_BUG;
  1284. }
  1285. frame->extended_data = av_malloc(size);
  1286. if (!frame->extended_data) {
  1287. av_frame_unref(frame);
  1288. return AVERROR(ENOMEM);
  1289. }
  1290. memcpy(frame->extended_data, avci->to_free->extended_data,
  1291. size);
  1292. } else
  1293. frame->extended_data = frame->data;
  1294. frame->format = avci->to_free->format;
  1295. frame->width = avci->to_free->width;
  1296. frame->height = avci->to_free->height;
  1297. frame->channel_layout = avci->to_free->channel_layout;
  1298. frame->nb_samples = avci->to_free->nb_samples;
  1299. return 0;
  1300. }
  1301. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1302. int *got_picture_ptr,
  1303. AVPacket *avpkt)
  1304. {
  1305. AVCodecInternal *avci = avctx->internal;
  1306. int ret;
  1307. *got_picture_ptr = 0;
  1308. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1309. return -1;
  1310. if (!avctx->codec->decode) {
  1311. av_log(avctx, AV_LOG_ERROR, "This decoder requires using the avcodec_send_packet() API.\n");
  1312. return AVERROR(ENOSYS);
  1313. }
  1314. avctx->internal->pkt = avpkt;
  1315. ret = apply_param_change(avctx, avpkt);
  1316. if (ret < 0)
  1317. return ret;
  1318. av_frame_unref(picture);
  1319. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size ||
  1320. (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1321. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1322. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1323. avpkt);
  1324. else {
  1325. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1326. avpkt);
  1327. if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS))
  1328. picture->pkt_dts = avpkt->dts;
  1329. /* get_buffer is supposed to set frame parameters */
  1330. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) {
  1331. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1332. picture->width = avctx->width;
  1333. picture->height = avctx->height;
  1334. picture->format = avctx->pix_fmt;
  1335. }
  1336. }
  1337. emms_c(); //needed to avoid an emms_c() call before every return;
  1338. if (*got_picture_ptr) {
  1339. if (!avctx->refcounted_frames) {
  1340. int err = unrefcount_frame(avci, picture);
  1341. if (err < 0)
  1342. return err;
  1343. }
  1344. avctx->frame_number++;
  1345. } else
  1346. av_frame_unref(picture);
  1347. } else
  1348. ret = 0;
  1349. #if FF_API_AVCTX_TIMEBASE
  1350. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  1351. avctx->time_base = av_inv_q(avctx->framerate);
  1352. #endif
  1353. return ret;
  1354. }
  1355. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1356. AVFrame *frame,
  1357. int *got_frame_ptr,
  1358. AVPacket *avpkt)
  1359. {
  1360. AVCodecInternal *avci = avctx->internal;
  1361. int ret = 0;
  1362. *got_frame_ptr = 0;
  1363. if (!avctx->codec->decode) {
  1364. av_log(avctx, AV_LOG_ERROR, "This decoder requires using the avcodec_send_packet() API.\n");
  1365. return AVERROR(ENOSYS);
  1366. }
  1367. avctx->internal->pkt = avpkt;
  1368. if (!avpkt->data && avpkt->size) {
  1369. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1370. return AVERROR(EINVAL);
  1371. }
  1372. ret = apply_param_change(avctx, avpkt);
  1373. if (ret < 0)
  1374. return ret;
  1375. av_frame_unref(frame);
  1376. if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size) {
  1377. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1378. if (ret >= 0 && *got_frame_ptr) {
  1379. avctx->frame_number++;
  1380. frame->pkt_dts = avpkt->dts;
  1381. if (frame->format == AV_SAMPLE_FMT_NONE)
  1382. frame->format = avctx->sample_fmt;
  1383. if (!avctx->refcounted_frames) {
  1384. int err = unrefcount_frame(avci, frame);
  1385. if (err < 0)
  1386. return err;
  1387. }
  1388. } else
  1389. av_frame_unref(frame);
  1390. }
  1391. return ret;
  1392. }
  1393. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1394. int *got_sub_ptr,
  1395. AVPacket *avpkt)
  1396. {
  1397. int ret;
  1398. avctx->internal->pkt = avpkt;
  1399. *got_sub_ptr = 0;
  1400. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1401. if (*got_sub_ptr)
  1402. avctx->frame_number++;
  1403. return ret;
  1404. }
  1405. void avsubtitle_free(AVSubtitle *sub)
  1406. {
  1407. int i;
  1408. for (i = 0; i < sub->num_rects; i++) {
  1409. av_freep(&sub->rects[i]->data[0]);
  1410. av_freep(&sub->rects[i]->data[1]);
  1411. av_freep(&sub->rects[i]->data[2]);
  1412. av_freep(&sub->rects[i]->data[3]);
  1413. av_freep(&sub->rects[i]->text);
  1414. av_freep(&sub->rects[i]->ass);
  1415. av_freep(&sub->rects[i]);
  1416. }
  1417. av_freep(&sub->rects);
  1418. memset(sub, 0, sizeof(AVSubtitle));
  1419. }
  1420. static int do_decode(AVCodecContext *avctx, AVPacket *pkt)
  1421. {
  1422. int got_frame;
  1423. int ret;
  1424. av_assert0(!avctx->internal->buffer_frame->buf[0]);
  1425. if (!pkt)
  1426. pkt = avctx->internal->buffer_pkt;
  1427. // This is the lesser evil. The field is for compatibility with legacy users
  1428. // of the legacy API, and users using the new API should not be forced to
  1429. // even know about this field.
  1430. avctx->refcounted_frames = 1;
  1431. // Some codecs (at least wma lossless) will crash when feeding drain packets
  1432. // after EOF was signaled.
  1433. if (avctx->internal->draining_done)
  1434. return AVERROR_EOF;
  1435. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1436. ret = avcodec_decode_video2(avctx, avctx->internal->buffer_frame,
  1437. &got_frame, pkt);
  1438. if (ret >= 0)
  1439. ret = pkt->size;
  1440. } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  1441. ret = avcodec_decode_audio4(avctx, avctx->internal->buffer_frame,
  1442. &got_frame, pkt);
  1443. } else {
  1444. ret = AVERROR(EINVAL);
  1445. }
  1446. if (ret < 0)
  1447. return ret;
  1448. if (avctx->internal->draining && !got_frame)
  1449. avctx->internal->draining_done = 1;
  1450. if (ret >= pkt->size) {
  1451. av_packet_unref(avctx->internal->buffer_pkt);
  1452. } else {
  1453. int consumed = ret;
  1454. if (pkt != avctx->internal->buffer_pkt) {
  1455. av_packet_unref(avctx->internal->buffer_pkt);
  1456. if ((ret = av_packet_ref(avctx->internal->buffer_pkt, pkt)) < 0)
  1457. return ret;
  1458. }
  1459. avctx->internal->buffer_pkt->data += consumed;
  1460. avctx->internal->buffer_pkt->size -= consumed;
  1461. avctx->internal->buffer_pkt->pts = AV_NOPTS_VALUE;
  1462. avctx->internal->buffer_pkt->dts = AV_NOPTS_VALUE;
  1463. }
  1464. if (got_frame)
  1465. av_assert0(avctx->internal->buffer_frame->buf[0]);
  1466. return 0;
  1467. }
  1468. int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
  1469. {
  1470. int ret;
  1471. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  1472. return AVERROR(EINVAL);
  1473. if (avctx->internal->draining)
  1474. return AVERROR_EOF;
  1475. if (!avpkt || !avpkt->size) {
  1476. avctx->internal->draining = 1;
  1477. avpkt = NULL;
  1478. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  1479. return 0;
  1480. }
  1481. if (avctx->codec->send_packet) {
  1482. if (avpkt) {
  1483. ret = apply_param_change(avctx, (AVPacket *)avpkt);
  1484. if (ret < 0)
  1485. return ret;
  1486. }
  1487. return avctx->codec->send_packet(avctx, avpkt);
  1488. }
  1489. // Emulation via old API. Assume avpkt is likely not refcounted, while
  1490. // decoder output is always refcounted, and avoid copying.
  1491. if (avctx->internal->buffer_pkt->size || avctx->internal->buffer_frame->buf[0])
  1492. return AVERROR(EAGAIN);
  1493. // The goal is decoding the first frame of the packet without using memcpy,
  1494. // because the common case is having only 1 frame per packet (especially
  1495. // with video, but audio too). In other cases, it can't be avoided, unless
  1496. // the user is feeding refcounted packets.
  1497. return do_decode(avctx, (AVPacket *)avpkt);
  1498. }
  1499. int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  1500. {
  1501. int ret;
  1502. av_frame_unref(frame);
  1503. if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
  1504. return AVERROR(EINVAL);
  1505. if (avctx->codec->receive_frame) {
  1506. if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  1507. return AVERROR_EOF;
  1508. return avctx->codec->receive_frame(avctx, frame);
  1509. }
  1510. // Emulation via old API.
  1511. if (!avctx->internal->buffer_frame->buf[0]) {
  1512. if (!avctx->internal->buffer_pkt->size && !avctx->internal->draining)
  1513. return AVERROR(EAGAIN);
  1514. while (1) {
  1515. if ((ret = do_decode(avctx, avctx->internal->buffer_pkt)) < 0) {
  1516. av_packet_unref(avctx->internal->buffer_pkt);
  1517. return ret;
  1518. }
  1519. // Some audio decoders may consume partial data without returning
  1520. // a frame (fate-wmapro-2ch). There is no way to make the caller
  1521. // call avcodec_receive_frame() again without returning a frame,
  1522. // so try to decode more in these cases.
  1523. if (avctx->internal->buffer_frame->buf[0] ||
  1524. !avctx->internal->buffer_pkt->size)
  1525. break;
  1526. }
  1527. }
  1528. if (!avctx->internal->buffer_frame->buf[0])
  1529. return avctx->internal->draining ? AVERROR_EOF : AVERROR(EAGAIN);
  1530. av_frame_move_ref(frame, avctx->internal->buffer_frame);
  1531. return 0;
  1532. }
  1533. static int do_encode(AVCodecContext *avctx, const AVFrame *frame, int *got_packet)
  1534. {
  1535. int ret;
  1536. *got_packet = 0;
  1537. av_packet_unref(avctx->internal->buffer_pkt);
  1538. avctx->internal->buffer_pkt_valid = 0;
  1539. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  1540. ret = avcodec_encode_video2(avctx, avctx->internal->buffer_pkt,
  1541. frame, got_packet);
  1542. } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  1543. ret = avcodec_encode_audio2(avctx, avctx->internal->buffer_pkt,
  1544. frame, got_packet);
  1545. } else {
  1546. ret = AVERROR(EINVAL);
  1547. }
  1548. if (ret >= 0 && *got_packet) {
  1549. // Encoders must always return ref-counted buffers.
  1550. // Side-data only packets have no data and can be not ref-counted.
  1551. av_assert0(!avctx->internal->buffer_pkt->data || avctx->internal->buffer_pkt->buf);
  1552. avctx->internal->buffer_pkt_valid = 1;
  1553. ret = 0;
  1554. } else {
  1555. av_packet_unref(avctx->internal->buffer_pkt);
  1556. }
  1557. return ret;
  1558. }
  1559. int attribute_align_arg avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
  1560. {
  1561. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  1562. return AVERROR(EINVAL);
  1563. if (avctx->internal->draining)
  1564. return AVERROR_EOF;
  1565. if (!frame) {
  1566. avctx->internal->draining = 1;
  1567. if (!(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  1568. return 0;
  1569. }
  1570. if (avctx->codec->send_frame)
  1571. return avctx->codec->send_frame(avctx, frame);
  1572. // Emulation via old API. Do it here instead of avcodec_receive_packet, because:
  1573. // 1. if the AVFrame is not refcounted, the copying will be much more
  1574. // expensive than copying the packet data
  1575. // 2. assume few users use non-refcounted AVPackets, so usually no copy is
  1576. // needed
  1577. if (avctx->internal->buffer_pkt_valid)
  1578. return AVERROR(EAGAIN);
  1579. return do_encode(avctx, frame, &(int){0});
  1580. }
  1581. int attribute_align_arg avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
  1582. {
  1583. av_packet_unref(avpkt);
  1584. if (!avcodec_is_open(avctx) || !av_codec_is_encoder(avctx->codec))
  1585. return AVERROR(EINVAL);
  1586. if (avctx->codec->receive_packet) {
  1587. if (avctx->internal->draining && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY))
  1588. return AVERROR_EOF;
  1589. return avctx->codec->receive_packet(avctx, avpkt);
  1590. }
  1591. // Emulation via old API.
  1592. if (!avctx->internal->buffer_pkt_valid) {
  1593. int got_packet;
  1594. int ret;
  1595. if (!avctx->internal->draining)
  1596. return AVERROR(EAGAIN);
  1597. ret = do_encode(avctx, NULL, &got_packet);
  1598. if (ret < 0)
  1599. return ret;
  1600. if (ret >= 0 && !got_packet)
  1601. return AVERROR_EOF;
  1602. }
  1603. av_packet_move_ref(avpkt, avctx->internal->buffer_pkt);
  1604. avctx->internal->buffer_pkt_valid = 0;
  1605. return 0;
  1606. }
  1607. av_cold int avcodec_close(AVCodecContext *avctx)
  1608. {
  1609. int i;
  1610. if (avcodec_is_open(avctx)) {
  1611. FramePool *pool = avctx->internal->pool;
  1612. if (HAVE_THREADS && avctx->internal->thread_ctx)
  1613. ff_thread_free(avctx);
  1614. if (avctx->codec && avctx->codec->close)
  1615. avctx->codec->close(avctx);
  1616. av_frame_free(&avctx->internal->to_free);
  1617. av_frame_free(&avctx->internal->buffer_frame);
  1618. av_packet_free(&avctx->internal->buffer_pkt);
  1619. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  1620. av_buffer_pool_uninit(&pool->pools[i]);
  1621. av_freep(&avctx->internal->pool);
  1622. if (avctx->hwaccel && avctx->hwaccel->uninit)
  1623. avctx->hwaccel->uninit(avctx);
  1624. av_freep(&avctx->internal->hwaccel_priv_data);
  1625. av_freep(&avctx->internal);
  1626. }
  1627. for (i = 0; i < avctx->nb_coded_side_data; i++)
  1628. av_freep(&avctx->coded_side_data[i].data);
  1629. av_freep(&avctx->coded_side_data);
  1630. avctx->nb_coded_side_data = 0;
  1631. av_buffer_unref(&avctx->hw_frames_ctx);
  1632. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1633. av_opt_free(avctx->priv_data);
  1634. av_opt_free(avctx);
  1635. av_freep(&avctx->priv_data);
  1636. if (av_codec_is_encoder(avctx->codec)) {
  1637. av_freep(&avctx->extradata);
  1638. #if FF_API_CODED_FRAME
  1639. FF_DISABLE_DEPRECATION_WARNINGS
  1640. av_frame_free(&avctx->coded_frame);
  1641. FF_ENABLE_DEPRECATION_WARNINGS
  1642. #endif
  1643. }
  1644. avctx->codec = NULL;
  1645. avctx->active_thread_type = 0;
  1646. return 0;
  1647. }
  1648. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  1649. {
  1650. AVCodec *p, *experimental = NULL;
  1651. p = first_avcodec;
  1652. while (p) {
  1653. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  1654. p->id == id) {
  1655. if (p->capabilities & AV_CODEC_CAP_EXPERIMENTAL && !experimental) {
  1656. experimental = p;
  1657. } else
  1658. return p;
  1659. }
  1660. p = p->next;
  1661. }
  1662. return experimental;
  1663. }
  1664. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1665. {
  1666. return find_encdec(id, 1);
  1667. }
  1668. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1669. {
  1670. AVCodec *p;
  1671. if (!name)
  1672. return NULL;
  1673. p = first_avcodec;
  1674. while (p) {
  1675. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1676. return p;
  1677. p = p->next;
  1678. }
  1679. return NULL;
  1680. }
  1681. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1682. {
  1683. return find_encdec(id, 0);
  1684. }
  1685. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1686. {
  1687. AVCodec *p;
  1688. if (!name)
  1689. return NULL;
  1690. p = first_avcodec;
  1691. while (p) {
  1692. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1693. return p;
  1694. p = p->next;
  1695. }
  1696. return NULL;
  1697. }
  1698. static int get_bit_rate(AVCodecContext *ctx)
  1699. {
  1700. int bit_rate;
  1701. int bits_per_sample;
  1702. switch (ctx->codec_type) {
  1703. case AVMEDIA_TYPE_VIDEO:
  1704. case AVMEDIA_TYPE_DATA:
  1705. case AVMEDIA_TYPE_SUBTITLE:
  1706. case AVMEDIA_TYPE_ATTACHMENT:
  1707. bit_rate = ctx->bit_rate;
  1708. break;
  1709. case AVMEDIA_TYPE_AUDIO:
  1710. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1711. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1712. break;
  1713. default:
  1714. bit_rate = 0;
  1715. break;
  1716. }
  1717. return bit_rate;
  1718. }
  1719. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1720. {
  1721. int i, len, ret = 0;
  1722. #define TAG_PRINT(x) \
  1723. (((x) >= '0' && (x) <= '9') || \
  1724. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1725. ((x) == '.' || (x) == ' '))
  1726. for (i = 0; i < 4; i++) {
  1727. len = snprintf(buf, buf_size,
  1728. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1729. buf += len;
  1730. buf_size = buf_size > len ? buf_size - len : 0;
  1731. ret += len;
  1732. codec_tag >>= 8;
  1733. }
  1734. return ret;
  1735. }
  1736. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1737. {
  1738. const char *codec_name;
  1739. const char *profile = NULL;
  1740. char buf1[32];
  1741. int bitrate;
  1742. int new_line = 0;
  1743. AVRational display_aspect_ratio;
  1744. const AVCodecDescriptor *desc = avcodec_descriptor_get(enc->codec_id);
  1745. if (desc) {
  1746. codec_name = desc->name;
  1747. profile = avcodec_profile_name(enc->codec_id, enc->profile);
  1748. } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
  1749. /* fake mpeg2 transport stream codec (currently not
  1750. * registered) */
  1751. codec_name = "mpeg2ts";
  1752. } else {
  1753. /* output avi tags */
  1754. char tag_buf[32];
  1755. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1756. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1757. codec_name = buf1;
  1758. }
  1759. switch (enc->codec_type) {
  1760. case AVMEDIA_TYPE_VIDEO:
  1761. snprintf(buf, buf_size,
  1762. "Video: %s%s",
  1763. codec_name, enc->mb_decision ? " (hq)" : "");
  1764. if (profile)
  1765. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1766. " (%s)", profile);
  1767. if (enc->codec_tag) {
  1768. char tag_buf[32];
  1769. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1770. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1771. " [%s / 0x%04X]", tag_buf, enc->codec_tag);
  1772. }
  1773. av_strlcat(buf, "\n ", buf_size);
  1774. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1775. "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
  1776. av_get_pix_fmt_name(enc->pix_fmt));
  1777. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  1778. snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
  1779. av_color_range_name(enc->color_range));
  1780. if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
  1781. enc->color_primaries != AVCOL_PRI_UNSPECIFIED ||
  1782. enc->color_trc != AVCOL_TRC_UNSPECIFIED) {
  1783. new_line = 1;
  1784. snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s/%s/%s",
  1785. av_color_space_name(enc->colorspace),
  1786. av_color_primaries_name(enc->color_primaries),
  1787. av_color_transfer_name(enc->color_trc));
  1788. }
  1789. if (av_log_get_level() >= AV_LOG_DEBUG &&
  1790. enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
  1791. snprintf(buf + strlen(buf), buf_size - strlen(buf), ", %s",
  1792. av_chroma_location_name(enc->chroma_sample_location));
  1793. if (enc->width) {
  1794. av_strlcat(buf, new_line ? "\n " : ", ", buf_size);
  1795. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1796. "%dx%d",
  1797. enc->width, enc->height);
  1798. if (av_log_get_level() >= AV_LOG_VERBOSE &&
  1799. (enc->width != enc->coded_width ||
  1800. enc->height != enc->coded_height))
  1801. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1802. " (%dx%d)", enc->coded_width, enc->coded_height);
  1803. if (enc->sample_aspect_ratio.num) {
  1804. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1805. enc->width * enc->sample_aspect_ratio.num,
  1806. enc->height * enc->sample_aspect_ratio.den,
  1807. 1024 * 1024);
  1808. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1809. " [PAR %d:%d DAR %d:%d]",
  1810. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1811. display_aspect_ratio.num, display_aspect_ratio.den);
  1812. }
  1813. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1814. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1815. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1816. ", %d/%d",
  1817. enc->time_base.num / g, enc->time_base.den / g);
  1818. }
  1819. }
  1820. if (encode) {
  1821. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1822. ", q=%d-%d", enc->qmin, enc->qmax);
  1823. }
  1824. break;
  1825. case AVMEDIA_TYPE_AUDIO:
  1826. snprintf(buf, buf_size,
  1827. "Audio: %s",
  1828. codec_name);
  1829. if (profile)
  1830. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1831. " (%s)", profile);
  1832. if (enc->codec_tag) {
  1833. char tag_buf[32];
  1834. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1835. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1836. " [%s / 0x%04X]", tag_buf, enc->codec_tag);
  1837. }
  1838. av_strlcat(buf, "\n ", buf_size);
  1839. if (enc->sample_rate) {
  1840. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1841. "%d Hz, ", enc->sample_rate);
  1842. }
  1843. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1844. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1845. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1846. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1847. }
  1848. break;
  1849. case AVMEDIA_TYPE_DATA:
  1850. snprintf(buf, buf_size, "Data: %s", codec_name);
  1851. break;
  1852. case AVMEDIA_TYPE_SUBTITLE:
  1853. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1854. break;
  1855. case AVMEDIA_TYPE_ATTACHMENT:
  1856. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1857. break;
  1858. default:
  1859. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1860. return;
  1861. }
  1862. if (encode) {
  1863. if (enc->flags & AV_CODEC_FLAG_PASS1)
  1864. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1865. ", pass 1");
  1866. if (enc->flags & AV_CODEC_FLAG_PASS2)
  1867. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1868. ", pass 2");
  1869. }
  1870. bitrate = get_bit_rate(enc);
  1871. if (bitrate != 0) {
  1872. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1873. ", %d kb/s", bitrate / 1000);
  1874. }
  1875. }
  1876. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1877. {
  1878. const AVProfile *p;
  1879. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1880. return NULL;
  1881. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1882. if (p->profile == profile)
  1883. return p->name;
  1884. return NULL;
  1885. }
  1886. const char *avcodec_profile_name(enum AVCodecID codec_id, int profile)
  1887. {
  1888. const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
  1889. const AVProfile *p;
  1890. if (profile == FF_PROFILE_UNKNOWN || !desc || !desc->profiles)
  1891. return NULL;
  1892. for (p = desc->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1893. if (p->profile == profile)
  1894. return p->name;
  1895. return NULL;
  1896. }
  1897. unsigned avcodec_version(void)
  1898. {
  1899. return LIBAVCODEC_VERSION_INT;
  1900. }
  1901. const char *avcodec_configuration(void)
  1902. {
  1903. return LIBAV_CONFIGURATION;
  1904. }
  1905. const char *avcodec_license(void)
  1906. {
  1907. #define LICENSE_PREFIX "libavcodec license: "
  1908. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1909. }
  1910. void avcodec_flush_buffers(AVCodecContext *avctx)
  1911. {
  1912. avctx->internal->draining = 0;
  1913. avctx->internal->draining_done = 0;
  1914. av_frame_unref(avctx->internal->buffer_frame);
  1915. av_packet_unref(avctx->internal->buffer_pkt);
  1916. avctx->internal->buffer_pkt_valid = 0;
  1917. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1918. ff_thread_flush(avctx);
  1919. else if (avctx->codec->flush)
  1920. avctx->codec->flush(avctx);
  1921. if (!avctx->refcounted_frames)
  1922. av_frame_unref(avctx->internal->to_free);
  1923. }
  1924. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1925. {
  1926. switch (codec_id) {
  1927. case AV_CODEC_ID_ADPCM_CT:
  1928. case AV_CODEC_ID_ADPCM_IMA_APC:
  1929. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1930. case AV_CODEC_ID_ADPCM_IMA_WS:
  1931. case AV_CODEC_ID_ADPCM_G722:
  1932. case AV_CODEC_ID_ADPCM_YAMAHA:
  1933. return 4;
  1934. case AV_CODEC_ID_PCM_ALAW:
  1935. case AV_CODEC_ID_PCM_MULAW:
  1936. case AV_CODEC_ID_PCM_S8:
  1937. case AV_CODEC_ID_PCM_U8:
  1938. case AV_CODEC_ID_PCM_ZORK:
  1939. return 8;
  1940. case AV_CODEC_ID_PCM_S16BE:
  1941. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  1942. case AV_CODEC_ID_PCM_S16LE:
  1943. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1944. case AV_CODEC_ID_PCM_U16BE:
  1945. case AV_CODEC_ID_PCM_U16LE:
  1946. return 16;
  1947. case AV_CODEC_ID_PCM_S24DAUD:
  1948. case AV_CODEC_ID_PCM_S24BE:
  1949. case AV_CODEC_ID_PCM_S24LE:
  1950. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  1951. case AV_CODEC_ID_PCM_U24BE:
  1952. case AV_CODEC_ID_PCM_U24LE:
  1953. return 24;
  1954. case AV_CODEC_ID_PCM_S32BE:
  1955. case AV_CODEC_ID_PCM_S32LE:
  1956. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  1957. case AV_CODEC_ID_PCM_U32BE:
  1958. case AV_CODEC_ID_PCM_U32LE:
  1959. case AV_CODEC_ID_PCM_F32BE:
  1960. case AV_CODEC_ID_PCM_F32LE:
  1961. return 32;
  1962. case AV_CODEC_ID_PCM_F64BE:
  1963. case AV_CODEC_ID_PCM_F64LE:
  1964. return 64;
  1965. default:
  1966. return 0;
  1967. }
  1968. }
  1969. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1970. {
  1971. switch (codec_id) {
  1972. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1973. return 2;
  1974. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1975. return 3;
  1976. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1977. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1978. case AV_CODEC_ID_ADPCM_IMA_QT:
  1979. case AV_CODEC_ID_ADPCM_SWF:
  1980. case AV_CODEC_ID_ADPCM_MS:
  1981. return 4;
  1982. default:
  1983. return av_get_exact_bits_per_sample(codec_id);
  1984. }
  1985. }
  1986. static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
  1987. uint32_t tag, int bits_per_coded_sample, int frame_bytes)
  1988. {
  1989. int bps = av_get_exact_bits_per_sample(id);
  1990. /* codecs with an exact constant bits per sample */
  1991. if (bps > 0 && ch > 0 && frame_bytes > 0)
  1992. return (frame_bytes * 8) / (bps * ch);
  1993. bps = bits_per_coded_sample;
  1994. /* codecs with a fixed packet duration */
  1995. switch (id) {
  1996. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1997. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1998. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1999. case AV_CODEC_ID_AMR_NB:
  2000. case AV_CODEC_ID_GSM:
  2001. case AV_CODEC_ID_QCELP:
  2002. case AV_CODEC_ID_RA_144:
  2003. case AV_CODEC_ID_RA_288: return 160;
  2004. case AV_CODEC_ID_IMC: return 256;
  2005. case AV_CODEC_ID_AMR_WB:
  2006. case AV_CODEC_ID_GSM_MS: return 320;
  2007. case AV_CODEC_ID_MP1: return 384;
  2008. case AV_CODEC_ID_ATRAC1: return 512;
  2009. case AV_CODEC_ID_ATRAC3: return 1024;
  2010. case AV_CODEC_ID_MP2:
  2011. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2012. case AV_CODEC_ID_AC3: return 1536;
  2013. }
  2014. if (sr > 0) {
  2015. /* calc from sample rate */
  2016. if (id == AV_CODEC_ID_TTA)
  2017. return 256 * sr / 245;
  2018. if (ch > 0) {
  2019. /* calc from sample rate and channels */
  2020. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2021. return (480 << (sr / 22050)) / ch;
  2022. }
  2023. }
  2024. if (ba > 0) {
  2025. /* calc from block_align */
  2026. if (id == AV_CODEC_ID_SIPR) {
  2027. switch (ba) {
  2028. case 20: return 160;
  2029. case 19: return 144;
  2030. case 29: return 288;
  2031. case 37: return 480;
  2032. }
  2033. } else if (id == AV_CODEC_ID_ILBC) {
  2034. switch (ba) {
  2035. case 38: return 160;
  2036. case 50: return 240;
  2037. }
  2038. }
  2039. }
  2040. if (frame_bytes > 0) {
  2041. /* calc from frame_bytes only */
  2042. if (id == AV_CODEC_ID_TRUESPEECH)
  2043. return 240 * (frame_bytes / 32);
  2044. if (id == AV_CODEC_ID_NELLYMOSER)
  2045. return 256 * (frame_bytes / 64);
  2046. if (bps > 0) {
  2047. /* calc from frame_bytes and bits_per_coded_sample */
  2048. if (id == AV_CODEC_ID_ADPCM_G726)
  2049. return frame_bytes * 8 / bps;
  2050. }
  2051. if (ch > 0) {
  2052. /* calc from frame_bytes and channels */
  2053. switch (id) {
  2054. case AV_CODEC_ID_ADPCM_4XM:
  2055. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2056. return (frame_bytes - 4 * ch) * 2 / ch;
  2057. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2058. return (frame_bytes - 4) * 2 / ch;
  2059. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2060. return (frame_bytes - 8) * 2 / ch;
  2061. case AV_CODEC_ID_ADPCM_XA:
  2062. return (frame_bytes / 128) * 224 / ch;
  2063. case AV_CODEC_ID_INTERPLAY_DPCM:
  2064. return (frame_bytes - 6 - ch) / ch;
  2065. case AV_CODEC_ID_ROQ_DPCM:
  2066. return (frame_bytes - 8) / ch;
  2067. case AV_CODEC_ID_XAN_DPCM:
  2068. return (frame_bytes - 2 * ch) / ch;
  2069. case AV_CODEC_ID_MACE3:
  2070. return 3 * frame_bytes / ch;
  2071. case AV_CODEC_ID_MACE6:
  2072. return 6 * frame_bytes / ch;
  2073. case AV_CODEC_ID_PCM_LXF:
  2074. return 2 * (frame_bytes / (5 * ch));
  2075. }
  2076. if (tag) {
  2077. /* calc from frame_bytes, channels, and codec_tag */
  2078. if (id == AV_CODEC_ID_SOL_DPCM) {
  2079. if (tag == 3)
  2080. return frame_bytes / ch;
  2081. else
  2082. return frame_bytes * 2 / ch;
  2083. }
  2084. }
  2085. if (ba > 0) {
  2086. /* calc from frame_bytes, channels, and block_align */
  2087. int blocks = frame_bytes / ba;
  2088. switch (id) {
  2089. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2090. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  2091. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2092. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2093. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2094. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2095. case AV_CODEC_ID_ADPCM_MS:
  2096. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2097. }
  2098. }
  2099. if (bps > 0) {
  2100. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2101. switch (id) {
  2102. case AV_CODEC_ID_PCM_DVD:
  2103. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2104. case AV_CODEC_ID_PCM_BLURAY:
  2105. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2106. case AV_CODEC_ID_S302M:
  2107. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2108. }
  2109. }
  2110. }
  2111. }
  2112. return 0;
  2113. }
  2114. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2115. {
  2116. return get_audio_frame_duration(avctx->codec_id, avctx->sample_rate,
  2117. avctx->channels, avctx->block_align,
  2118. avctx->codec_tag, avctx->bits_per_coded_sample,
  2119. frame_bytes);
  2120. }
  2121. int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
  2122. {
  2123. return get_audio_frame_duration(par->codec_id, par->sample_rate,
  2124. par->channels, par->block_align,
  2125. par->codec_tag, par->bits_per_coded_sample,
  2126. frame_bytes);
  2127. }
  2128. #if !HAVE_THREADS
  2129. int ff_thread_init(AVCodecContext *s)
  2130. {
  2131. return -1;
  2132. }
  2133. #endif
  2134. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2135. {
  2136. unsigned int n = 0;
  2137. while (v >= 0xff) {
  2138. *s++ = 0xff;
  2139. v -= 0xff;
  2140. n++;
  2141. }
  2142. *s = v;
  2143. n++;
  2144. return n;
  2145. }
  2146. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2147. {
  2148. int i;
  2149. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2150. return i;
  2151. }
  2152. #if FF_API_MISSING_SAMPLE
  2153. FF_DISABLE_DEPRECATION_WARNINGS
  2154. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2155. {
  2156. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
  2157. "version to the newest one from Git. If the problem still "
  2158. "occurs, it means that your file has a feature which has not "
  2159. "been implemented.\n", feature);
  2160. if(want_sample)
  2161. av_log_ask_for_sample(avc, NULL);
  2162. }
  2163. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2164. {
  2165. va_list argument_list;
  2166. va_start(argument_list, msg);
  2167. if (msg)
  2168. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2169. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2170. "of this file to ftp://upload.libav.org/incoming/ "
  2171. "and contact the libav-devel mailing list.\n");
  2172. va_end(argument_list);
  2173. }
  2174. FF_ENABLE_DEPRECATION_WARNINGS
  2175. #endif /* FF_API_MISSING_SAMPLE */
  2176. static AVHWAccel *first_hwaccel = NULL;
  2177. void av_register_hwaccel(AVHWAccel *hwaccel)
  2178. {
  2179. AVHWAccel **p = &first_hwaccel;
  2180. while (*p)
  2181. p = &(*p)->next;
  2182. *p = hwaccel;
  2183. hwaccel->next = NULL;
  2184. }
  2185. AVHWAccel *av_hwaccel_next(const AVHWAccel *hwaccel)
  2186. {
  2187. return hwaccel ? hwaccel->next : first_hwaccel;
  2188. }
  2189. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2190. {
  2191. if (lockmgr_cb) {
  2192. // There is no good way to rollback a failure to destroy the
  2193. // mutex, so we ignore failures.
  2194. lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY);
  2195. lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY);
  2196. lockmgr_cb = NULL;
  2197. codec_mutex = NULL;
  2198. avformat_mutex = NULL;
  2199. }
  2200. if (cb) {
  2201. void *new_codec_mutex = NULL;
  2202. void *new_avformat_mutex = NULL;
  2203. int err;
  2204. if (err = cb(&new_codec_mutex, AV_LOCK_CREATE)) {
  2205. return err > 0 ? AVERROR_UNKNOWN : err;
  2206. }
  2207. if (err = cb(&new_avformat_mutex, AV_LOCK_CREATE)) {
  2208. // Ignore failures to destroy the newly created mutex.
  2209. cb(&new_codec_mutex, AV_LOCK_DESTROY);
  2210. return err > 0 ? AVERROR_UNKNOWN : err;
  2211. }
  2212. lockmgr_cb = cb;
  2213. codec_mutex = new_codec_mutex;
  2214. avformat_mutex = new_avformat_mutex;
  2215. }
  2216. return 0;
  2217. }
  2218. int avpriv_lock_avformat(void)
  2219. {
  2220. if (lockmgr_cb) {
  2221. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2222. return -1;
  2223. }
  2224. return 0;
  2225. }
  2226. int avpriv_unlock_avformat(void)
  2227. {
  2228. if (lockmgr_cb) {
  2229. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2230. return -1;
  2231. }
  2232. return 0;
  2233. }
  2234. unsigned int avpriv_toupper4(unsigned int x)
  2235. {
  2236. return av_toupper(x & 0xFF) +
  2237. (av_toupper((x >> 8) & 0xFF) << 8) +
  2238. (av_toupper((x >> 16) & 0xFF) << 16) +
  2239. (av_toupper((x >> 24) & 0xFF) << 24);
  2240. }
  2241. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  2242. {
  2243. int ret;
  2244. dst->owner = src->owner;
  2245. ret = av_frame_ref(dst->f, src->f);
  2246. if (ret < 0)
  2247. return ret;
  2248. if (src->progress &&
  2249. !(dst->progress = av_buffer_ref(src->progress))) {
  2250. ff_thread_release_buffer(dst->owner, dst);
  2251. return AVERROR(ENOMEM);
  2252. }
  2253. return 0;
  2254. }
  2255. #if !HAVE_THREADS
  2256. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  2257. {
  2258. f->owner = avctx;
  2259. return ff_get_buffer(avctx, f->f, flags);
  2260. }
  2261. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  2262. {
  2263. if (f->f)
  2264. av_frame_unref(f->f);
  2265. }
  2266. void ff_thread_finish_setup(AVCodecContext *avctx)
  2267. {
  2268. }
  2269. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  2270. {
  2271. }
  2272. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  2273. {
  2274. }
  2275. #endif
  2276. int avcodec_is_open(AVCodecContext *s)
  2277. {
  2278. return !!s->internal;
  2279. }
  2280. const uint8_t *avpriv_find_start_code(const uint8_t *restrict p,
  2281. const uint8_t *end,
  2282. uint32_t * restrict state)
  2283. {
  2284. int i;
  2285. assert(p <= end);
  2286. if (p >= end)
  2287. return end;
  2288. for (i = 0; i < 3; i++) {
  2289. uint32_t tmp = *state << 8;
  2290. *state = tmp + *(p++);
  2291. if (tmp == 0x100 || p == end)
  2292. return p;
  2293. }
  2294. while (p < end) {
  2295. if (p[-1] > 1 ) p += 3;
  2296. else if (p[-2] ) p += 2;
  2297. else if (p[-3]|(p[-1]-1)) p++;
  2298. else {
  2299. p++;
  2300. break;
  2301. }
  2302. }
  2303. p = FFMIN(p, end) - 4;
  2304. *state = AV_RB32(p);
  2305. return p + 4;
  2306. }
  2307. AVCPBProperties *av_cpb_properties_alloc(size_t *size)
  2308. {
  2309. AVCPBProperties *props = av_mallocz(sizeof(AVCPBProperties));
  2310. if (!props)
  2311. return NULL;
  2312. if (size)
  2313. *size = sizeof(*props);
  2314. props->vbv_delay = UINT64_MAX;
  2315. return props;
  2316. }
  2317. AVCPBProperties *ff_add_cpb_side_data(AVCodecContext *avctx)
  2318. {
  2319. AVPacketSideData *tmp;
  2320. AVCPBProperties *props;
  2321. size_t size;
  2322. props = av_cpb_properties_alloc(&size);
  2323. if (!props)
  2324. return NULL;
  2325. tmp = av_realloc_array(avctx->coded_side_data, avctx->nb_coded_side_data + 1, sizeof(*tmp));
  2326. if (!tmp) {
  2327. av_freep(&props);
  2328. return NULL;
  2329. }
  2330. avctx->coded_side_data = tmp;
  2331. avctx->nb_coded_side_data++;
  2332. avctx->coded_side_data[avctx->nb_coded_side_data - 1].type = AV_PKT_DATA_CPB_PROPERTIES;
  2333. avctx->coded_side_data[avctx->nb_coded_side_data - 1].data = (uint8_t*)props;
  2334. avctx->coded_side_data[avctx->nb_coded_side_data - 1].size = size;
  2335. return props;
  2336. }
  2337. static void codec_parameters_reset(AVCodecParameters *par)
  2338. {
  2339. av_freep(&par->extradata);
  2340. memset(par, 0, sizeof(*par));
  2341. par->codec_type = AVMEDIA_TYPE_UNKNOWN;
  2342. par->codec_id = AV_CODEC_ID_NONE;
  2343. par->format = -1;
  2344. par->field_order = AV_FIELD_UNKNOWN;
  2345. par->color_range = AVCOL_RANGE_UNSPECIFIED;
  2346. par->color_primaries = AVCOL_PRI_UNSPECIFIED;
  2347. par->color_trc = AVCOL_TRC_UNSPECIFIED;
  2348. par->color_space = AVCOL_SPC_UNSPECIFIED;
  2349. par->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
  2350. par->sample_aspect_ratio = (AVRational){ 0, 1 };
  2351. }
  2352. AVCodecParameters *avcodec_parameters_alloc(void)
  2353. {
  2354. AVCodecParameters *par = av_mallocz(sizeof(*par));
  2355. if (!par)
  2356. return NULL;
  2357. codec_parameters_reset(par);
  2358. return par;
  2359. }
  2360. void avcodec_parameters_free(AVCodecParameters **ppar)
  2361. {
  2362. AVCodecParameters *par = *ppar;
  2363. if (!par)
  2364. return;
  2365. codec_parameters_reset(par);
  2366. av_freep(ppar);
  2367. }
  2368. int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
  2369. {
  2370. codec_parameters_reset(dst);
  2371. memcpy(dst, src, sizeof(*dst));
  2372. dst->extradata = NULL;
  2373. dst->extradata_size = 0;
  2374. if (src->extradata) {
  2375. dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  2376. if (!dst->extradata)
  2377. return AVERROR(ENOMEM);
  2378. memcpy(dst->extradata, src->extradata, src->extradata_size);
  2379. dst->extradata_size = src->extradata_size;
  2380. }
  2381. return 0;
  2382. }
  2383. int avcodec_parameters_from_context(AVCodecParameters *par,
  2384. const AVCodecContext *codec)
  2385. {
  2386. codec_parameters_reset(par);
  2387. par->codec_type = codec->codec_type;
  2388. par->codec_id = codec->codec_id;
  2389. par->codec_tag = codec->codec_tag;
  2390. par->bit_rate = codec->bit_rate;
  2391. par->bits_per_coded_sample = codec->bits_per_coded_sample;
  2392. par->profile = codec->profile;
  2393. par->level = codec->level;
  2394. switch (par->codec_type) {
  2395. case AVMEDIA_TYPE_VIDEO:
  2396. par->format = codec->pix_fmt;
  2397. par->width = codec->width;
  2398. par->height = codec->height;
  2399. par->field_order = codec->field_order;
  2400. par->color_range = codec->color_range;
  2401. par->color_primaries = codec->color_primaries;
  2402. par->color_trc = codec->color_trc;
  2403. par->color_space = codec->colorspace;
  2404. par->chroma_location = codec->chroma_sample_location;
  2405. par->sample_aspect_ratio = codec->sample_aspect_ratio;
  2406. break;
  2407. case AVMEDIA_TYPE_AUDIO:
  2408. par->format = codec->sample_fmt;
  2409. par->channel_layout = codec->channel_layout;
  2410. par->channels = codec->channels;
  2411. par->sample_rate = codec->sample_rate;
  2412. par->block_align = codec->block_align;
  2413. par->initial_padding = codec->initial_padding;
  2414. break;
  2415. }
  2416. if (codec->extradata) {
  2417. par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  2418. if (!par->extradata)
  2419. return AVERROR(ENOMEM);
  2420. memcpy(par->extradata, codec->extradata, codec->extradata_size);
  2421. par->extradata_size = codec->extradata_size;
  2422. }
  2423. return 0;
  2424. }
  2425. int avcodec_parameters_to_context(AVCodecContext *codec,
  2426. const AVCodecParameters *par)
  2427. {
  2428. codec->codec_type = par->codec_type;
  2429. codec->codec_id = par->codec_id;
  2430. codec->codec_tag = par->codec_tag;
  2431. codec->bit_rate = par->bit_rate;
  2432. codec->bits_per_coded_sample = par->bits_per_coded_sample;
  2433. codec->profile = par->profile;
  2434. codec->level = par->level;
  2435. switch (par->codec_type) {
  2436. case AVMEDIA_TYPE_VIDEO:
  2437. codec->pix_fmt = par->format;
  2438. codec->width = par->width;
  2439. codec->height = par->height;
  2440. codec->field_order = par->field_order;
  2441. codec->color_range = par->color_range;
  2442. codec->color_primaries = par->color_primaries;
  2443. codec->color_trc = par->color_trc;
  2444. codec->colorspace = par->color_space;
  2445. codec->chroma_sample_location = par->chroma_location;
  2446. codec->sample_aspect_ratio = par->sample_aspect_ratio;
  2447. break;
  2448. case AVMEDIA_TYPE_AUDIO:
  2449. codec->sample_fmt = par->format;
  2450. codec->channel_layout = par->channel_layout;
  2451. codec->channels = par->channels;
  2452. codec->sample_rate = par->sample_rate;
  2453. codec->block_align = par->block_align;
  2454. codec->initial_padding = par->initial_padding;
  2455. break;
  2456. }
  2457. if (par->extradata) {
  2458. av_freep(&codec->extradata);
  2459. codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  2460. if (!codec->extradata)
  2461. return AVERROR(ENOMEM);
  2462. memcpy(codec->extradata, par->extradata, par->extradata_size);
  2463. codec->extradata_size = par->extradata_size;
  2464. }
  2465. return 0;
  2466. }