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.

2906 lines
88KB

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