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.

2408 lines
72KB

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