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.

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