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.

2916 lines
89KB

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