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.

2430 lines
73KB

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