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.

3096 lines
100KB

  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 FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/avassert.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/bprint.h"
  30. #include "libavutil/channel_layout.h"
  31. #include "libavutil/crc.h"
  32. #include "libavutil/frame.h"
  33. #include "libavutil/mathematics.h"
  34. #include "libavutil/pixdesc.h"
  35. #include "libavutil/imgutils.h"
  36. #include "libavutil/samplefmt.h"
  37. #include "libavutil/dict.h"
  38. #include "libavutil/avassert.h"
  39. #include "avcodec.h"
  40. #include "dsputil.h"
  41. #include "libavutil/opt.h"
  42. #include "thread.h"
  43. #include "frame_thread_encoder.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. #if CONFIG_ICONV
  52. # include <iconv.h>
  53. #endif
  54. volatile int ff_avcodec_locked;
  55. static int volatile entangled_thread_counter = 0;
  56. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  57. static void *codec_mutex;
  58. static void *avformat_mutex;
  59. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  60. {
  61. if (min_size < *size)
  62. return ptr;
  63. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  64. ptr = av_realloc(ptr, min_size);
  65. /* we could set this to the unmodified min_size but this is safer
  66. * if the user lost the ptr and uses NULL now
  67. */
  68. if (!ptr)
  69. min_size = 0;
  70. *size = min_size;
  71. return ptr;
  72. }
  73. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  74. {
  75. void **p = ptr;
  76. if (min_size < *size)
  77. return 0;
  78. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  79. av_free(*p);
  80. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  81. if (!*p)
  82. min_size = 0;
  83. *size = min_size;
  84. return 1;
  85. }
  86. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  87. {
  88. ff_fast_malloc(ptr, size, min_size, 0);
  89. }
  90. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  91. {
  92. uint8_t **p = ptr;
  93. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  94. av_freep(p);
  95. *size = 0;
  96. return;
  97. }
  98. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  99. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  100. }
  101. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  102. {
  103. uint8_t **p = ptr;
  104. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  105. av_freep(p);
  106. *size = 0;
  107. return;
  108. }
  109. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  110. memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  111. }
  112. /* encoder management */
  113. static AVCodec *first_avcodec = NULL;
  114. AVCodec *av_codec_next(const AVCodec *c)
  115. {
  116. if (c)
  117. return c->next;
  118. else
  119. return first_avcodec;
  120. }
  121. static void avcodec_init(void)
  122. {
  123. static int initialized = 0;
  124. if (initialized != 0)
  125. return;
  126. initialized = 1;
  127. if (CONFIG_DSPUTIL)
  128. ff_dsputil_static_init();
  129. }
  130. int av_codec_is_encoder(const AVCodec *codec)
  131. {
  132. return codec && (codec->encode_sub || codec->encode2);
  133. }
  134. int av_codec_is_decoder(const AVCodec *codec)
  135. {
  136. return codec && codec->decode;
  137. }
  138. void avcodec_register(AVCodec *codec)
  139. {
  140. AVCodec **p;
  141. avcodec_init();
  142. p = &first_avcodec;
  143. while (*p != NULL)
  144. p = &(*p)->next;
  145. *p = codec;
  146. codec->next = NULL;
  147. if (codec->init_static_data)
  148. codec->init_static_data(codec);
  149. }
  150. unsigned avcodec_get_edge_width(void)
  151. {
  152. return EDGE_WIDTH;
  153. }
  154. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  155. {
  156. s->coded_width = width;
  157. s->coded_height = height;
  158. s->width = -((-width ) >> s->lowres);
  159. s->height = -((-height) >> s->lowres);
  160. }
  161. #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
  162. # define STRIDE_ALIGN 16
  163. #else
  164. # define STRIDE_ALIGN 8
  165. #endif
  166. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  167. int linesize_align[AV_NUM_DATA_POINTERS])
  168. {
  169. int i;
  170. int w_align = 1;
  171. int h_align = 1;
  172. switch (s->pix_fmt) {
  173. case AV_PIX_FMT_YUV420P:
  174. case AV_PIX_FMT_YUYV422:
  175. case AV_PIX_FMT_UYVY422:
  176. case AV_PIX_FMT_YUV422P:
  177. case AV_PIX_FMT_YUV440P:
  178. case AV_PIX_FMT_YUV444P:
  179. case AV_PIX_FMT_GBRP:
  180. case AV_PIX_FMT_GRAY8:
  181. case AV_PIX_FMT_GRAY16BE:
  182. case AV_PIX_FMT_GRAY16LE:
  183. case AV_PIX_FMT_YUVJ420P:
  184. case AV_PIX_FMT_YUVJ422P:
  185. case AV_PIX_FMT_YUVJ440P:
  186. case AV_PIX_FMT_YUVJ444P:
  187. case AV_PIX_FMT_YUVA420P:
  188. case AV_PIX_FMT_YUVA422P:
  189. case AV_PIX_FMT_YUVA444P:
  190. case AV_PIX_FMT_YUV420P9LE:
  191. case AV_PIX_FMT_YUV420P9BE:
  192. case AV_PIX_FMT_YUV420P10LE:
  193. case AV_PIX_FMT_YUV420P10BE:
  194. case AV_PIX_FMT_YUV420P12LE:
  195. case AV_PIX_FMT_YUV420P12BE:
  196. case AV_PIX_FMT_YUV420P14LE:
  197. case AV_PIX_FMT_YUV420P14BE:
  198. case AV_PIX_FMT_YUV422P9LE:
  199. case AV_PIX_FMT_YUV422P9BE:
  200. case AV_PIX_FMT_YUV422P10LE:
  201. case AV_PIX_FMT_YUV422P10BE:
  202. case AV_PIX_FMT_YUV422P12LE:
  203. case AV_PIX_FMT_YUV422P12BE:
  204. case AV_PIX_FMT_YUV422P14LE:
  205. case AV_PIX_FMT_YUV422P14BE:
  206. case AV_PIX_FMT_YUV444P9LE:
  207. case AV_PIX_FMT_YUV444P9BE:
  208. case AV_PIX_FMT_YUV444P10LE:
  209. case AV_PIX_FMT_YUV444P10BE:
  210. case AV_PIX_FMT_YUV444P12LE:
  211. case AV_PIX_FMT_YUV444P12BE:
  212. case AV_PIX_FMT_YUV444P14LE:
  213. case AV_PIX_FMT_YUV444P14BE:
  214. case AV_PIX_FMT_GBRP9LE:
  215. case AV_PIX_FMT_GBRP9BE:
  216. case AV_PIX_FMT_GBRP10LE:
  217. case AV_PIX_FMT_GBRP10BE:
  218. case AV_PIX_FMT_GBRP12LE:
  219. case AV_PIX_FMT_GBRP12BE:
  220. case AV_PIX_FMT_GBRP14LE:
  221. case AV_PIX_FMT_GBRP14BE:
  222. w_align = 16; //FIXME assume 16 pixel per macroblock
  223. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  224. break;
  225. case AV_PIX_FMT_YUV411P:
  226. case AV_PIX_FMT_UYYVYY411:
  227. w_align = 32;
  228. h_align = 8;
  229. break;
  230. case AV_PIX_FMT_YUV410P:
  231. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  232. w_align = 64;
  233. h_align = 64;
  234. }
  235. break;
  236. case AV_PIX_FMT_RGB555:
  237. if (s->codec_id == AV_CODEC_ID_RPZA) {
  238. w_align = 4;
  239. h_align = 4;
  240. }
  241. break;
  242. case AV_PIX_FMT_PAL8:
  243. case AV_PIX_FMT_BGR8:
  244. case AV_PIX_FMT_RGB8:
  245. if (s->codec_id == AV_CODEC_ID_SMC ||
  246. s->codec_id == AV_CODEC_ID_CINEPAK) {
  247. w_align = 4;
  248. h_align = 4;
  249. }
  250. break;
  251. case AV_PIX_FMT_BGR24:
  252. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  253. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  254. w_align = 4;
  255. h_align = 4;
  256. }
  257. break;
  258. case AV_PIX_FMT_RGB24:
  259. if (s->codec_id == AV_CODEC_ID_CINEPAK) {
  260. w_align = 4;
  261. h_align = 4;
  262. }
  263. break;
  264. default:
  265. w_align = 1;
  266. h_align = 1;
  267. break;
  268. }
  269. if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
  270. w_align = FFMAX(w_align, 8);
  271. }
  272. *width = FFALIGN(*width, w_align);
  273. *height = FFALIGN(*height, h_align);
  274. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
  275. // some of the optimized chroma MC reads one line too much
  276. // which is also done in mpeg decoders with lowres > 0
  277. *height += 2;
  278. for (i = 0; i < 4; i++)
  279. linesize_align[i] = STRIDE_ALIGN;
  280. }
  281. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  282. {
  283. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  284. int chroma_shift = desc->log2_chroma_w;
  285. int linesize_align[AV_NUM_DATA_POINTERS];
  286. int align;
  287. avcodec_align_dimensions2(s, width, height, linesize_align);
  288. align = FFMAX(linesize_align[0], linesize_align[3]);
  289. linesize_align[1] <<= chroma_shift;
  290. linesize_align[2] <<= chroma_shift;
  291. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  292. *width = FFALIGN(*width, align);
  293. }
  294. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  295. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  296. int buf_size, int align)
  297. {
  298. int ch, planar, needed_size, ret = 0;
  299. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  300. frame->nb_samples, sample_fmt,
  301. align);
  302. if (buf_size < needed_size)
  303. return AVERROR(EINVAL);
  304. planar = av_sample_fmt_is_planar(sample_fmt);
  305. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  306. if (!(frame->extended_data = av_mallocz(nb_channels *
  307. sizeof(*frame->extended_data))))
  308. return AVERROR(ENOMEM);
  309. } else {
  310. frame->extended_data = frame->data;
  311. }
  312. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  313. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  314. sample_fmt, align)) < 0) {
  315. if (frame->extended_data != frame->data)
  316. av_freep(&frame->extended_data);
  317. return ret;
  318. }
  319. if (frame->extended_data != frame->data) {
  320. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  321. frame->data[ch] = frame->extended_data[ch];
  322. }
  323. return ret;
  324. }
  325. static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
  326. {
  327. FramePool *pool = avctx->internal->pool;
  328. int i, ret;
  329. switch (avctx->codec_type) {
  330. case AVMEDIA_TYPE_VIDEO: {
  331. AVPicture picture;
  332. int size[4] = { 0 };
  333. int w = frame->width;
  334. int h = frame->height;
  335. int tmpsize, unaligned;
  336. if (pool->format == frame->format &&
  337. pool->width == frame->width && pool->height == frame->height)
  338. return 0;
  339. avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
  340. if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
  341. w += EDGE_WIDTH * 2;
  342. h += EDGE_WIDTH * 2;
  343. }
  344. do {
  345. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  346. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  347. av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
  348. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  349. w += w & ~(w - 1);
  350. unaligned = 0;
  351. for (i = 0; i < 4; i++)
  352. unaligned |= picture.linesize[i] % pool->stride_align[i];
  353. } while (unaligned);
  354. tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
  355. NULL, picture.linesize);
  356. if (tmpsize < 0)
  357. return -1;
  358. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  359. size[i] = picture.data[i + 1] - picture.data[i];
  360. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  361. for (i = 0; i < 4; i++) {
  362. av_buffer_pool_uninit(&pool->pools[i]);
  363. pool->linesize[i] = picture.linesize[i];
  364. if (size[i]) {
  365. pool->pools[i] = av_buffer_pool_init(size[i] + 16, NULL);
  366. if (!pool->pools[i]) {
  367. ret = AVERROR(ENOMEM);
  368. goto fail;
  369. }
  370. }
  371. }
  372. pool->format = frame->format;
  373. pool->width = frame->width;
  374. pool->height = frame->height;
  375. break;
  376. }
  377. case AVMEDIA_TYPE_AUDIO: {
  378. int ch = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
  379. int planar = av_sample_fmt_is_planar(frame->format);
  380. int planes = planar ? ch : 1;
  381. if (pool->format == frame->format && pool->planes == planes &&
  382. pool->channels == ch && frame->nb_samples == pool->samples)
  383. return 0;
  384. av_buffer_pool_uninit(&pool->pools[0]);
  385. ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
  386. frame->nb_samples, frame->format, 0);
  387. if (ret < 0)
  388. goto fail;
  389. pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
  390. if (!pool->pools[0]) {
  391. ret = AVERROR(ENOMEM);
  392. goto fail;
  393. }
  394. pool->format = frame->format;
  395. pool->planes = planes;
  396. pool->channels = ch;
  397. pool->samples = frame->nb_samples;
  398. break;
  399. }
  400. default: av_assert0(0);
  401. }
  402. return 0;
  403. fail:
  404. for (i = 0; i < 4; i++)
  405. av_buffer_pool_uninit(&pool->pools[i]);
  406. pool->format = -1;
  407. pool->planes = pool->channels = pool->samples = 0;
  408. pool->width = pool->height = 0;
  409. return ret;
  410. }
  411. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  412. {
  413. FramePool *pool = avctx->internal->pool;
  414. int planes = pool->planes;
  415. int i;
  416. frame->linesize[0] = pool->linesize[0];
  417. if (planes > AV_NUM_DATA_POINTERS) {
  418. frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
  419. frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
  420. frame->extended_buf = av_mallocz(frame->nb_extended_buf *
  421. sizeof(*frame->extended_buf));
  422. if (!frame->extended_data || !frame->extended_buf) {
  423. av_freep(&frame->extended_data);
  424. av_freep(&frame->extended_buf);
  425. return AVERROR(ENOMEM);
  426. }
  427. } else {
  428. frame->extended_data = frame->data;
  429. av_assert0(frame->nb_extended_buf == 0);
  430. }
  431. for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
  432. frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
  433. if (!frame->buf[i])
  434. goto fail;
  435. frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
  436. }
  437. for (i = 0; i < frame->nb_extended_buf; i++) {
  438. frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
  439. if (!frame->extended_buf[i])
  440. goto fail;
  441. frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
  442. }
  443. if (avctx->debug & FF_DEBUG_BUFFERS)
  444. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
  445. return 0;
  446. fail:
  447. av_frame_unref(frame);
  448. return AVERROR(ENOMEM);
  449. }
  450. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  451. {
  452. FramePool *pool = s->internal->pool;
  453. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
  454. int pixel_size = desc->comp[0].step_minus1 + 1;
  455. int h_chroma_shift, v_chroma_shift;
  456. int i;
  457. if (pic->data[0] != NULL) {
  458. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  459. return -1;
  460. }
  461. memset(pic->data, 0, sizeof(pic->data));
  462. pic->extended_data = pic->data;
  463. av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  464. for (i = 0; i < 4 && pool->pools[i]; i++) {
  465. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  466. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  467. pic->linesize[i] = pool->linesize[i];
  468. pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
  469. if (!pic->buf[i])
  470. goto fail;
  471. // no edge if EDGE EMU or not planar YUV
  472. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !pool->pools[2])
  473. pic->data[i] = pic->buf[i]->data;
  474. else {
  475. pic->data[i] = pic->buf[i]->data +
  476. FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
  477. (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
  478. }
  479. }
  480. for (; i < AV_NUM_DATA_POINTERS; i++) {
  481. pic->data[i] = NULL;
  482. pic->linesize[i] = 0;
  483. }
  484. if (pic->data[1] && !pic->data[2])
  485. avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
  486. if (s->debug & FF_DEBUG_BUFFERS)
  487. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
  488. return 0;
  489. fail:
  490. av_frame_unref(pic);
  491. return AVERROR(ENOMEM);
  492. }
  493. void avpriv_color_frame(AVFrame *frame, const int c[4])
  494. {
  495. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  496. int p, y, x;
  497. av_assert0(desc->flags & PIX_FMT_PLANAR);
  498. for (p = 0; p<desc->nb_components; p++) {
  499. uint8_t *dst = frame->data[p];
  500. int is_chroma = p == 1 || p == 2;
  501. int bytes = -((-frame->width) >> (is_chroma ? desc->log2_chroma_w : 0));
  502. for (y = 0; y<-((-frame->height) >> (is_chroma ? desc->log2_chroma_h : 0)); y++){
  503. if (desc->comp[0].depth_minus1 >= 8) {
  504. for (x = 0; x<bytes; x++)
  505. ((uint16_t*)dst)[x] = c[p];
  506. }else
  507. memset(dst, c[p], bytes);
  508. dst += frame->linesize[p];
  509. }
  510. }
  511. }
  512. int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
  513. {
  514. int ret;
  515. if ((ret = update_frame_pool(avctx, frame)) < 0)
  516. return ret;
  517. #if FF_API_GET_BUFFER
  518. frame->type = FF_BUFFER_TYPE_INTERNAL;
  519. #endif
  520. switch (avctx->codec_type) {
  521. case AVMEDIA_TYPE_VIDEO:
  522. return video_get_buffer(avctx, frame);
  523. case AVMEDIA_TYPE_AUDIO:
  524. return audio_get_buffer(avctx, frame);
  525. default:
  526. return -1;
  527. }
  528. }
  529. int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
  530. {
  531. if (avctx->pkt) {
  532. frame->pkt_pts = avctx->pkt->pts;
  533. av_frame_set_pkt_pos (frame, avctx->pkt->pos);
  534. av_frame_set_pkt_duration(frame, avctx->pkt->duration);
  535. av_frame_set_pkt_size (frame, avctx->pkt->size);
  536. } else {
  537. frame->pkt_pts = AV_NOPTS_VALUE;
  538. av_frame_set_pkt_pos (frame, -1);
  539. av_frame_set_pkt_duration(frame, 0);
  540. av_frame_set_pkt_size (frame, -1);
  541. }
  542. frame->reordered_opaque = avctx->reordered_opaque;
  543. switch (avctx->codec->type) {
  544. case AVMEDIA_TYPE_VIDEO:
  545. if (!frame->width)
  546. frame->width = avctx->width;
  547. if (!frame->height)
  548. frame->height = avctx->height;
  549. if (frame->format < 0)
  550. frame->format = avctx->pix_fmt;
  551. if (!frame->sample_aspect_ratio.num)
  552. frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
  553. break;
  554. case AVMEDIA_TYPE_AUDIO:
  555. if (!frame->sample_rate)
  556. frame->sample_rate = avctx->sample_rate;
  557. if (frame->format < 0)
  558. frame->format = avctx->sample_fmt;
  559. if (!frame->channel_layout) {
  560. if (avctx->channel_layout) {
  561. if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
  562. avctx->channels) {
  563. av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
  564. "configuration.\n");
  565. return AVERROR(EINVAL);
  566. }
  567. frame->channel_layout = avctx->channel_layout;
  568. } else {
  569. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  570. av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
  571. avctx->channels);
  572. return AVERROR(ENOSYS);
  573. }
  574. frame->channel_layout = av_get_default_channel_layout(avctx->channels);
  575. }
  576. }
  577. av_frame_set_channels(frame, avctx->channels);
  578. break;
  579. }
  580. return 0;
  581. }
  582. #if FF_API_GET_BUFFER
  583. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  584. {
  585. return avcodec_default_get_buffer2(avctx, frame, 0);
  586. }
  587. typedef struct CompatReleaseBufPriv {
  588. AVCodecContext avctx;
  589. AVFrame frame;
  590. } CompatReleaseBufPriv;
  591. static void compat_free_buffer(void *opaque, uint8_t *data)
  592. {
  593. CompatReleaseBufPriv *priv = opaque;
  594. if (priv->avctx.release_buffer)
  595. priv->avctx.release_buffer(&priv->avctx, &priv->frame);
  596. av_freep(&priv);
  597. }
  598. static void compat_release_buffer(void *opaque, uint8_t *data)
  599. {
  600. AVBufferRef *buf = opaque;
  601. av_buffer_unref(&buf);
  602. }
  603. #endif
  604. static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
  605. {
  606. int ret;
  607. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  608. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
  609. av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  610. return AVERROR(EINVAL);
  611. }
  612. }
  613. if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
  614. return ret;
  615. #if FF_API_GET_BUFFER
  616. /*
  617. * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
  618. * We wrap each plane in its own AVBuffer. Each of those has a reference to
  619. * a dummy AVBuffer as its private data, unreffing it on free.
  620. * When all the planes are freed, the dummy buffer's free callback calls
  621. * release_buffer().
  622. */
  623. if (avctx->get_buffer) {
  624. CompatReleaseBufPriv *priv = NULL;
  625. AVBufferRef *dummy_buf = NULL;
  626. int planes, i, ret;
  627. if (flags & AV_GET_BUFFER_FLAG_REF)
  628. frame->reference = 1;
  629. ret = avctx->get_buffer(avctx, frame);
  630. if (ret < 0)
  631. return ret;
  632. /* return if the buffers are already set up
  633. * this would happen e.g. when a custom get_buffer() calls
  634. * avcodec_default_get_buffer
  635. */
  636. if (frame->buf[0])
  637. return 0;
  638. priv = av_mallocz(sizeof(*priv));
  639. if (!priv) {
  640. ret = AVERROR(ENOMEM);
  641. goto fail;
  642. }
  643. priv->avctx = *avctx;
  644. priv->frame = *frame;
  645. dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
  646. if (!dummy_buf) {
  647. ret = AVERROR(ENOMEM);
  648. goto fail;
  649. }
  650. #define WRAP_PLANE(ref_out, data, data_size) \
  651. do { \
  652. AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf); \
  653. if (!dummy_ref) { \
  654. ret = AVERROR(ENOMEM); \
  655. goto fail; \
  656. } \
  657. ref_out = av_buffer_create(data, data_size, compat_release_buffer, \
  658. dummy_ref, 0); \
  659. if (!ref_out) { \
  660. av_frame_unref(frame); \
  661. ret = AVERROR(ENOMEM); \
  662. goto fail; \
  663. } \
  664. } while (0)
  665. if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  666. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  667. planes = av_pix_fmt_count_planes(frame->format);
  668. if (!desc || planes <= 0) {
  669. ret = AVERROR(EINVAL);
  670. goto fail;
  671. }
  672. for (i = 0; i < planes; i++) {
  673. int v_shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  674. int plane_size = (frame->height >> v_shift) * frame->linesize[i];
  675. WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
  676. }
  677. } else {
  678. int planar = av_sample_fmt_is_planar(frame->format);
  679. planes = planar ? avctx->channels : 1;
  680. if (planes > FF_ARRAY_ELEMS(frame->buf)) {
  681. frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
  682. frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
  683. frame->nb_extended_buf);
  684. if (!frame->extended_buf) {
  685. ret = AVERROR(ENOMEM);
  686. goto fail;
  687. }
  688. }
  689. for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
  690. WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
  691. for (i = 0; i < frame->nb_extended_buf; i++)
  692. WRAP_PLANE(frame->extended_buf[i],
  693. frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
  694. frame->linesize[0]);
  695. }
  696. av_buffer_unref(&dummy_buf);
  697. return 0;
  698. fail:
  699. avctx->release_buffer(avctx, frame);
  700. av_freep(&priv);
  701. av_buffer_unref(&dummy_buf);
  702. return ret;
  703. }
  704. #endif
  705. return avctx->get_buffer2(avctx, frame, flags);
  706. }
  707. int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
  708. {
  709. int ret = get_buffer_internal(avctx, frame, flags);
  710. if (ret < 0)
  711. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  712. return ret;
  713. }
  714. static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
  715. {
  716. AVFrame tmp;
  717. int ret;
  718. av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
  719. if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
  720. av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  721. frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
  722. av_frame_unref(frame);
  723. }
  724. ff_init_buffer_info(avctx, frame);
  725. if (!frame->data[0])
  726. return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  727. if (av_frame_is_writable(frame))
  728. return 0;
  729. av_frame_move_ref(&tmp, frame);
  730. ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
  731. if (ret < 0) {
  732. av_frame_unref(&tmp);
  733. return ret;
  734. }
  735. av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
  736. frame->format, frame->width, frame->height);
  737. av_frame_unref(&tmp);
  738. return 0;
  739. }
  740. int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
  741. {
  742. int ret = reget_buffer_internal(avctx, frame);
  743. if (ret < 0)
  744. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  745. return ret;
  746. }
  747. #if FF_API_GET_BUFFER
  748. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  749. {
  750. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  751. av_frame_unref(pic);
  752. }
  753. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  754. {
  755. av_assert0(0);
  756. }
  757. #endif
  758. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  759. {
  760. int i;
  761. for (i = 0; i < count; i++) {
  762. int r = func(c, (char *)arg + i * size);
  763. if (ret)
  764. ret[i] = r;
  765. }
  766. return 0;
  767. }
  768. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  769. {
  770. int i;
  771. for (i = 0; i < count; i++) {
  772. int r = func(c, arg, i, 0);
  773. if (ret)
  774. ret[i] = r;
  775. }
  776. return 0;
  777. }
  778. static int is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
  779. {
  780. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  781. return desc->flags & PIX_FMT_HWACCEL;
  782. }
  783. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  784. {
  785. while (*fmt != AV_PIX_FMT_NONE && is_hwaccel_pix_fmt(*fmt))
  786. ++fmt;
  787. return fmt[0];
  788. }
  789. void avcodec_get_frame_defaults(AVFrame *frame)
  790. {
  791. #if LIBAVCODEC_VERSION_MAJOR >= 55
  792. // extended_data should explicitly be freed when needed, this code is unsafe currently
  793. // also this is not compatible to the <55 ABI/API
  794. if (frame->extended_data != frame->data && 0)
  795. av_freep(&frame->extended_data);
  796. #endif
  797. memset(frame, 0, sizeof(AVFrame));
  798. frame->pts =
  799. frame->pkt_dts =
  800. frame->pkt_pts = AV_NOPTS_VALUE;
  801. av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
  802. av_frame_set_pkt_duration (frame, 0);
  803. av_frame_set_pkt_pos (frame, -1);
  804. av_frame_set_pkt_size (frame, -1);
  805. frame->key_frame = 1;
  806. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  807. frame->format = -1; /* unknown */
  808. frame->extended_data = frame->data;
  809. }
  810. AVFrame *avcodec_alloc_frame(void)
  811. {
  812. AVFrame *frame = av_malloc(sizeof(AVFrame));
  813. if (frame == NULL)
  814. return NULL;
  815. frame->extended_data = NULL;
  816. avcodec_get_frame_defaults(frame);
  817. return frame;
  818. }
  819. void avcodec_free_frame(AVFrame **frame)
  820. {
  821. AVFrame *f;
  822. if (!frame || !*frame)
  823. return;
  824. f = *frame;
  825. if (f->extended_data != f->data)
  826. av_freep(&f->extended_data);
  827. av_freep(frame);
  828. }
  829. #define MAKE_ACCESSORS(str, name, type, field) \
  830. type av_##name##_get_##field(const str *s) { return s->field; } \
  831. void av_##name##_set_##field(str *s, type v) { s->field = v; }
  832. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  833. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  834. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  835. {
  836. memset(sub, 0, sizeof(*sub));
  837. sub->pts = AV_NOPTS_VALUE;
  838. }
  839. static int get_bit_rate(AVCodecContext *ctx)
  840. {
  841. int bit_rate;
  842. int bits_per_sample;
  843. switch (ctx->codec_type) {
  844. case AVMEDIA_TYPE_VIDEO:
  845. case AVMEDIA_TYPE_DATA:
  846. case AVMEDIA_TYPE_SUBTITLE:
  847. case AVMEDIA_TYPE_ATTACHMENT:
  848. bit_rate = ctx->bit_rate;
  849. break;
  850. case AVMEDIA_TYPE_AUDIO:
  851. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  852. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  853. break;
  854. default:
  855. bit_rate = 0;
  856. break;
  857. }
  858. return bit_rate;
  859. }
  860. #if FF_API_AVCODEC_OPEN
  861. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  862. {
  863. return avcodec_open2(avctx, codec, NULL);
  864. }
  865. #endif
  866. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  867. {
  868. int ret = 0;
  869. ff_unlock_avcodec();
  870. ret = avcodec_open2(avctx, codec, options);
  871. ff_lock_avcodec(avctx);
  872. return ret;
  873. }
  874. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  875. {
  876. int ret = 0;
  877. AVDictionary *tmp = NULL;
  878. if (avcodec_is_open(avctx))
  879. return 0;
  880. if ((!codec && !avctx->codec)) {
  881. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  882. return AVERROR(EINVAL);
  883. }
  884. if ((codec && avctx->codec && codec != avctx->codec)) {
  885. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  886. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  887. return AVERROR(EINVAL);
  888. }
  889. if (!codec)
  890. codec = avctx->codec;
  891. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  892. return AVERROR(EINVAL);
  893. if (options)
  894. av_dict_copy(&tmp, *options, 0);
  895. ret = ff_lock_avcodec(avctx);
  896. if (ret < 0)
  897. return ret;
  898. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  899. if (!avctx->internal) {
  900. ret = AVERROR(ENOMEM);
  901. goto end;
  902. }
  903. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  904. if (!avctx->internal->pool) {
  905. ret = AVERROR(ENOMEM);
  906. goto free_and_end;
  907. }
  908. if (codec->priv_data_size > 0) {
  909. if (!avctx->priv_data) {
  910. avctx->priv_data = av_mallocz(codec->priv_data_size);
  911. if (!avctx->priv_data) {
  912. ret = AVERROR(ENOMEM);
  913. goto end;
  914. }
  915. if (codec->priv_class) {
  916. *(const AVClass **)avctx->priv_data = codec->priv_class;
  917. av_opt_set_defaults(avctx->priv_data);
  918. }
  919. }
  920. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  921. goto free_and_end;
  922. } else {
  923. avctx->priv_data = NULL;
  924. }
  925. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  926. goto free_and_end;
  927. // only call avcodec_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
  928. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  929. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
  930. if (avctx->coded_width && avctx->coded_height)
  931. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  932. else if (avctx->width && avctx->height)
  933. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  934. }
  935. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  936. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  937. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  938. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  939. avcodec_set_dimensions(avctx, 0, 0);
  940. }
  941. /* if the decoder init function was already called previously,
  942. * free the already allocated subtitle_header before overwriting it */
  943. if (av_codec_is_decoder(codec))
  944. av_freep(&avctx->subtitle_header);
  945. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  946. ret = AVERROR(EINVAL);
  947. goto free_and_end;
  948. }
  949. avctx->codec = codec;
  950. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  951. avctx->codec_id == AV_CODEC_ID_NONE) {
  952. avctx->codec_type = codec->type;
  953. avctx->codec_id = codec->id;
  954. }
  955. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  956. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  957. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  958. ret = AVERROR(EINVAL);
  959. goto free_and_end;
  960. }
  961. avctx->frame_number = 0;
  962. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  963. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  964. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  965. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  966. AVCodec *codec2;
  967. av_log(NULL, AV_LOG_ERROR,
  968. "The %s '%s' is experimental but experimental codecs are not enabled, "
  969. "add '-strict %d' if you want to use it.\n",
  970. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  971. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  972. if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
  973. av_log(NULL, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  974. codec_string, codec2->name);
  975. ret = AVERROR_EXPERIMENTAL;
  976. goto free_and_end;
  977. }
  978. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  979. (!avctx->time_base.num || !avctx->time_base.den)) {
  980. avctx->time_base.num = 1;
  981. avctx->time_base.den = avctx->sample_rate;
  982. }
  983. if (!HAVE_THREADS)
  984. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  985. if (CONFIG_FRAME_THREAD_ENCODER) {
  986. ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  987. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  988. ff_lock_avcodec(avctx);
  989. if (ret < 0)
  990. goto free_and_end;
  991. }
  992. if (HAVE_THREADS && !avctx->thread_opaque
  993. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  994. ret = ff_thread_init(avctx);
  995. if (ret < 0) {
  996. goto free_and_end;
  997. }
  998. }
  999. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  1000. avctx->thread_count = 1;
  1001. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  1002. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  1003. avctx->codec->max_lowres);
  1004. ret = AVERROR(EINVAL);
  1005. goto free_and_end;
  1006. }
  1007. if (av_codec_is_encoder(avctx->codec)) {
  1008. int i;
  1009. if (avctx->codec->sample_fmts) {
  1010. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  1011. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  1012. break;
  1013. if (avctx->channels == 1 &&
  1014. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  1015. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  1016. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  1017. break;
  1018. }
  1019. }
  1020. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  1021. char buf[128];
  1022. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  1023. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  1024. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  1025. ret = AVERROR(EINVAL);
  1026. goto free_and_end;
  1027. }
  1028. }
  1029. if (avctx->codec->pix_fmts) {
  1030. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  1031. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  1032. break;
  1033. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  1034. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  1035. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  1036. char buf[128];
  1037. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  1038. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  1039. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  1040. ret = AVERROR(EINVAL);
  1041. goto free_and_end;
  1042. }
  1043. }
  1044. if (avctx->codec->supported_samplerates) {
  1045. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  1046. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  1047. break;
  1048. if (avctx->codec->supported_samplerates[i] == 0) {
  1049. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  1050. avctx->sample_rate);
  1051. ret = AVERROR(EINVAL);
  1052. goto free_and_end;
  1053. }
  1054. }
  1055. if (avctx->codec->channel_layouts) {
  1056. if (!avctx->channel_layout) {
  1057. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  1058. } else {
  1059. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  1060. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  1061. break;
  1062. if (avctx->codec->channel_layouts[i] == 0) {
  1063. char buf[512];
  1064. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1065. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  1066. ret = AVERROR(EINVAL);
  1067. goto free_and_end;
  1068. }
  1069. }
  1070. }
  1071. if (avctx->channel_layout && avctx->channels) {
  1072. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1073. if (channels != avctx->channels) {
  1074. char buf[512];
  1075. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1076. av_log(avctx, AV_LOG_ERROR,
  1077. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  1078. buf, channels, avctx->channels);
  1079. ret = AVERROR(EINVAL);
  1080. goto free_and_end;
  1081. }
  1082. } else if (avctx->channel_layout) {
  1083. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1084. }
  1085. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
  1086. avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
  1087. ) {
  1088. if (avctx->width <= 0 || avctx->height <= 0) {
  1089. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  1090. ret = AVERROR(EINVAL);
  1091. goto free_and_end;
  1092. }
  1093. }
  1094. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  1095. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  1096. av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
  1097. }
  1098. if (!avctx->rc_initial_buffer_occupancy)
  1099. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  1100. }
  1101. avctx->pts_correction_num_faulty_pts =
  1102. avctx->pts_correction_num_faulty_dts = 0;
  1103. avctx->pts_correction_last_pts =
  1104. avctx->pts_correction_last_dts = INT64_MIN;
  1105. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  1106. || avctx->internal->frame_thread_encoder)) {
  1107. ret = avctx->codec->init(avctx);
  1108. if (ret < 0) {
  1109. goto free_and_end;
  1110. }
  1111. }
  1112. ret=0;
  1113. if (av_codec_is_decoder(avctx->codec)) {
  1114. if (!avctx->bit_rate)
  1115. avctx->bit_rate = get_bit_rate(avctx);
  1116. /* validate channel layout from the decoder */
  1117. if (avctx->channel_layout) {
  1118. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1119. if (!avctx->channels)
  1120. avctx->channels = channels;
  1121. else if (channels != avctx->channels) {
  1122. char buf[512];
  1123. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1124. av_log(avctx, AV_LOG_WARNING,
  1125. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  1126. "ignoring specified channel layout\n",
  1127. buf, channels, avctx->channels);
  1128. avctx->channel_layout = 0;
  1129. }
  1130. }
  1131. if (avctx->channels && avctx->channels < 0 ||
  1132. avctx->channels > FF_SANE_NB_CHANNELS) {
  1133. ret = AVERROR(EINVAL);
  1134. goto free_and_end;
  1135. }
  1136. if (avctx->sub_charenc) {
  1137. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  1138. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  1139. "supported with subtitles codecs\n");
  1140. ret = AVERROR(EINVAL);
  1141. goto free_and_end;
  1142. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  1143. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  1144. "subtitles character encoding will be ignored\n",
  1145. avctx->codec_descriptor->name);
  1146. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  1147. } else {
  1148. /* input character encoding is set for a text based subtitle
  1149. * codec at this point */
  1150. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  1151. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  1152. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  1153. #if CONFIG_ICONV
  1154. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  1155. if (cd == (iconv_t)-1) {
  1156. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  1157. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  1158. ret = AVERROR(errno);
  1159. goto free_and_end;
  1160. }
  1161. iconv_close(cd);
  1162. #else
  1163. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  1164. "conversion needs a libavcodec built with iconv support "
  1165. "for this codec\n");
  1166. ret = AVERROR(ENOSYS);
  1167. goto free_and_end;
  1168. #endif
  1169. }
  1170. }
  1171. }
  1172. }
  1173. end:
  1174. ff_unlock_avcodec();
  1175. if (options) {
  1176. av_dict_free(options);
  1177. *options = tmp;
  1178. }
  1179. return ret;
  1180. free_and_end:
  1181. av_dict_free(&tmp);
  1182. av_freep(&avctx->priv_data);
  1183. if (avctx->internal)
  1184. av_freep(&avctx->internal->pool);
  1185. av_freep(&avctx->internal);
  1186. avctx->codec = NULL;
  1187. goto end;
  1188. }
  1189. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  1190. {
  1191. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  1192. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  1193. return AVERROR(EINVAL);
  1194. }
  1195. if (avctx) {
  1196. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  1197. if (!avpkt->data || avpkt->size < size) {
  1198. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  1199. avpkt->data = avctx->internal->byte_buffer;
  1200. avpkt->size = avctx->internal->byte_buffer_size;
  1201. avpkt->destruct = NULL;
  1202. }
  1203. }
  1204. if (avpkt->data) {
  1205. AVBufferRef *buf = avpkt->buf;
  1206. #if FF_API_DESTRUCT_PACKET
  1207. void *destruct = avpkt->destruct;
  1208. #endif
  1209. if (avpkt->size < size) {
  1210. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  1211. return AVERROR(EINVAL);
  1212. }
  1213. av_init_packet(avpkt);
  1214. #if FF_API_DESTRUCT_PACKET
  1215. avpkt->destruct = destruct;
  1216. #endif
  1217. avpkt->buf = buf;
  1218. avpkt->size = size;
  1219. return 0;
  1220. } else {
  1221. int ret = av_new_packet(avpkt, size);
  1222. if (ret < 0)
  1223. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  1224. return ret;
  1225. }
  1226. }
  1227. int ff_alloc_packet(AVPacket *avpkt, int size)
  1228. {
  1229. return ff_alloc_packet2(NULL, avpkt, size);
  1230. }
  1231. /**
  1232. * Pad last frame with silence.
  1233. */
  1234. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1235. {
  1236. AVFrame *frame = NULL;
  1237. uint8_t *buf = NULL;
  1238. int ret;
  1239. if (!(frame = avcodec_alloc_frame()))
  1240. return AVERROR(ENOMEM);
  1241. *frame = *src;
  1242. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  1243. s->frame_size, s->sample_fmt, 0)) < 0)
  1244. goto fail;
  1245. if (!(buf = av_malloc(ret))) {
  1246. ret = AVERROR(ENOMEM);
  1247. goto fail;
  1248. }
  1249. frame->nb_samples = s->frame_size;
  1250. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  1251. buf, ret, 0)) < 0)
  1252. goto fail;
  1253. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1254. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1255. goto fail;
  1256. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1257. frame->nb_samples - src->nb_samples,
  1258. s->channels, s->sample_fmt)) < 0)
  1259. goto fail;
  1260. *dst = frame;
  1261. return 0;
  1262. fail:
  1263. if (frame->extended_data != frame->data)
  1264. av_freep(&frame->extended_data);
  1265. av_freep(&buf);
  1266. av_freep(&frame);
  1267. return ret;
  1268. }
  1269. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1270. AVPacket *avpkt,
  1271. const AVFrame *frame,
  1272. int *got_packet_ptr)
  1273. {
  1274. AVFrame tmp;
  1275. AVFrame *padded_frame = NULL;
  1276. int ret;
  1277. AVPacket user_pkt = *avpkt;
  1278. int needs_realloc = !user_pkt.data;
  1279. *got_packet_ptr = 0;
  1280. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1281. av_free_packet(avpkt);
  1282. av_init_packet(avpkt);
  1283. return 0;
  1284. }
  1285. /* ensure that extended_data is properly set */
  1286. if (frame && !frame->extended_data) {
  1287. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1288. avctx->channels > AV_NUM_DATA_POINTERS) {
  1289. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1290. "with more than %d channels, but extended_data is not set.\n",
  1291. AV_NUM_DATA_POINTERS);
  1292. return AVERROR(EINVAL);
  1293. }
  1294. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1295. tmp = *frame;
  1296. tmp.extended_data = tmp.data;
  1297. frame = &tmp;
  1298. }
  1299. /* check for valid frame size */
  1300. if (frame) {
  1301. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1302. if (frame->nb_samples > avctx->frame_size) {
  1303. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1304. return AVERROR(EINVAL);
  1305. }
  1306. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1307. if (frame->nb_samples < avctx->frame_size &&
  1308. !avctx->internal->last_audio_frame) {
  1309. ret = pad_last_frame(avctx, &padded_frame, frame);
  1310. if (ret < 0)
  1311. return ret;
  1312. frame = padded_frame;
  1313. avctx->internal->last_audio_frame = 1;
  1314. }
  1315. if (frame->nb_samples != avctx->frame_size) {
  1316. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1317. ret = AVERROR(EINVAL);
  1318. goto end;
  1319. }
  1320. }
  1321. }
  1322. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1323. if (!ret) {
  1324. if (*got_packet_ptr) {
  1325. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1326. if (avpkt->pts == AV_NOPTS_VALUE)
  1327. avpkt->pts = frame->pts;
  1328. if (!avpkt->duration)
  1329. avpkt->duration = ff_samples_to_time_base(avctx,
  1330. frame->nb_samples);
  1331. }
  1332. avpkt->dts = avpkt->pts;
  1333. } else {
  1334. avpkt->size = 0;
  1335. }
  1336. }
  1337. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1338. needs_realloc = 0;
  1339. if (user_pkt.data) {
  1340. if (user_pkt.size >= avpkt->size) {
  1341. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1342. } else {
  1343. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1344. avpkt->size = user_pkt.size;
  1345. ret = -1;
  1346. }
  1347. avpkt->buf = user_pkt.buf;
  1348. avpkt->data = user_pkt.data;
  1349. avpkt->destruct = user_pkt.destruct;
  1350. } else {
  1351. if (av_dup_packet(avpkt) < 0) {
  1352. ret = AVERROR(ENOMEM);
  1353. }
  1354. }
  1355. }
  1356. if (!ret) {
  1357. if (needs_realloc && avpkt->data) {
  1358. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1359. if (ret >= 0)
  1360. avpkt->data = avpkt->buf->data;
  1361. }
  1362. avctx->frame_number++;
  1363. }
  1364. if (ret < 0 || !*got_packet_ptr) {
  1365. av_free_packet(avpkt);
  1366. av_init_packet(avpkt);
  1367. goto end;
  1368. }
  1369. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1370. * this needs to be moved to the encoders, but for now we can do it
  1371. * here to simplify things */
  1372. avpkt->flags |= AV_PKT_FLAG_KEY;
  1373. end:
  1374. if (padded_frame) {
  1375. av_freep(&padded_frame->data[0]);
  1376. if (padded_frame->extended_data != padded_frame->data)
  1377. av_freep(&padded_frame->extended_data);
  1378. av_freep(&padded_frame);
  1379. }
  1380. return ret;
  1381. }
  1382. #if FF_API_OLD_ENCODE_AUDIO
  1383. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1384. uint8_t *buf, int buf_size,
  1385. const short *samples)
  1386. {
  1387. AVPacket pkt;
  1388. AVFrame frame0 = { { 0 } };
  1389. AVFrame *frame;
  1390. int ret, samples_size, got_packet;
  1391. av_init_packet(&pkt);
  1392. pkt.data = buf;
  1393. pkt.size = buf_size;
  1394. if (samples) {
  1395. frame = &frame0;
  1396. avcodec_get_frame_defaults(frame);
  1397. if (avctx->frame_size) {
  1398. frame->nb_samples = avctx->frame_size;
  1399. } else {
  1400. /* if frame_size is not set, the number of samples must be
  1401. * calculated from the buffer size */
  1402. int64_t nb_samples;
  1403. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1404. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1405. "support this codec\n");
  1406. return AVERROR(EINVAL);
  1407. }
  1408. nb_samples = (int64_t)buf_size * 8 /
  1409. (av_get_bits_per_sample(avctx->codec_id) *
  1410. avctx->channels);
  1411. if (nb_samples >= INT_MAX)
  1412. return AVERROR(EINVAL);
  1413. frame->nb_samples = nb_samples;
  1414. }
  1415. /* it is assumed that the samples buffer is large enough based on the
  1416. * relevant parameters */
  1417. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1418. frame->nb_samples,
  1419. avctx->sample_fmt, 1);
  1420. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1421. avctx->sample_fmt,
  1422. (const uint8_t *)samples,
  1423. samples_size, 1)) < 0)
  1424. return ret;
  1425. /* fabricate frame pts from sample count.
  1426. * this is needed because the avcodec_encode_audio() API does not have
  1427. * a way for the user to provide pts */
  1428. if (avctx->sample_rate && avctx->time_base.num)
  1429. frame->pts = ff_samples_to_time_base(avctx,
  1430. avctx->internal->sample_count);
  1431. else
  1432. frame->pts = AV_NOPTS_VALUE;
  1433. avctx->internal->sample_count += frame->nb_samples;
  1434. } else {
  1435. frame = NULL;
  1436. }
  1437. got_packet = 0;
  1438. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1439. if (!ret && got_packet && avctx->coded_frame) {
  1440. avctx->coded_frame->pts = pkt.pts;
  1441. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1442. }
  1443. /* free any side data since we cannot return it */
  1444. ff_packet_free_side_data(&pkt);
  1445. if (frame && frame->extended_data != frame->data)
  1446. av_freep(&frame->extended_data);
  1447. return ret ? ret : pkt.size;
  1448. }
  1449. #endif
  1450. #if FF_API_OLD_ENCODE_VIDEO
  1451. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1452. const AVFrame *pict)
  1453. {
  1454. AVPacket pkt;
  1455. int ret, got_packet = 0;
  1456. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1457. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1458. return -1;
  1459. }
  1460. av_init_packet(&pkt);
  1461. pkt.data = buf;
  1462. pkt.size = buf_size;
  1463. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1464. if (!ret && got_packet && avctx->coded_frame) {
  1465. avctx->coded_frame->pts = pkt.pts;
  1466. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1467. }
  1468. /* free any side data since we cannot return it */
  1469. if (pkt.side_data_elems > 0) {
  1470. int i;
  1471. for (i = 0; i < pkt.side_data_elems; i++)
  1472. av_free(pkt.side_data[i].data);
  1473. av_freep(&pkt.side_data);
  1474. pkt.side_data_elems = 0;
  1475. }
  1476. return ret ? ret : pkt.size;
  1477. }
  1478. #endif
  1479. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1480. AVPacket *avpkt,
  1481. const AVFrame *frame,
  1482. int *got_packet_ptr)
  1483. {
  1484. int ret;
  1485. AVPacket user_pkt = *avpkt;
  1486. int needs_realloc = !user_pkt.data;
  1487. *got_packet_ptr = 0;
  1488. if(CONFIG_FRAME_THREAD_ENCODER &&
  1489. avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1490. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1491. if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
  1492. avctx->stats_out[0] = '\0';
  1493. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1494. av_free_packet(avpkt);
  1495. av_init_packet(avpkt);
  1496. avpkt->size = 0;
  1497. return 0;
  1498. }
  1499. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1500. return AVERROR(EINVAL);
  1501. av_assert0(avctx->codec->encode2);
  1502. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1503. av_assert0(ret <= 0);
  1504. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1505. needs_realloc = 0;
  1506. if (user_pkt.data) {
  1507. if (user_pkt.size >= avpkt->size) {
  1508. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1509. } else {
  1510. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1511. avpkt->size = user_pkt.size;
  1512. ret = -1;
  1513. }
  1514. avpkt->buf = user_pkt.buf;
  1515. avpkt->data = user_pkt.data;
  1516. avpkt->destruct = user_pkt.destruct;
  1517. } else {
  1518. if (av_dup_packet(avpkt) < 0) {
  1519. ret = AVERROR(ENOMEM);
  1520. }
  1521. }
  1522. }
  1523. if (!ret) {
  1524. if (!*got_packet_ptr)
  1525. avpkt->size = 0;
  1526. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1527. avpkt->pts = avpkt->dts = frame->pts;
  1528. if (needs_realloc && avpkt->data) {
  1529. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1530. if (ret >= 0)
  1531. avpkt->data = avpkt->buf->data;
  1532. }
  1533. avctx->frame_number++;
  1534. }
  1535. if (ret < 0 || !*got_packet_ptr)
  1536. av_free_packet(avpkt);
  1537. emms_c();
  1538. return ret;
  1539. }
  1540. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1541. const AVSubtitle *sub)
  1542. {
  1543. int ret;
  1544. if (sub->start_display_time) {
  1545. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1546. return -1;
  1547. }
  1548. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1549. avctx->frame_number++;
  1550. return ret;
  1551. }
  1552. /**
  1553. * Attempt to guess proper monotonic timestamps for decoded video frames
  1554. * which might have incorrect times. Input timestamps may wrap around, in
  1555. * which case the output will as well.
  1556. *
  1557. * @param pts the pts field of the decoded AVPacket, as passed through
  1558. * AVFrame.pkt_pts
  1559. * @param dts the dts field of the decoded AVPacket
  1560. * @return one of the input values, may be AV_NOPTS_VALUE
  1561. */
  1562. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1563. int64_t reordered_pts, int64_t dts)
  1564. {
  1565. int64_t pts = AV_NOPTS_VALUE;
  1566. if (dts != AV_NOPTS_VALUE) {
  1567. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1568. ctx->pts_correction_last_dts = dts;
  1569. }
  1570. if (reordered_pts != AV_NOPTS_VALUE) {
  1571. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1572. ctx->pts_correction_last_pts = reordered_pts;
  1573. }
  1574. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1575. && reordered_pts != AV_NOPTS_VALUE)
  1576. pts = reordered_pts;
  1577. else
  1578. pts = dts;
  1579. return pts;
  1580. }
  1581. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1582. {
  1583. int size = 0;
  1584. const uint8_t *data;
  1585. uint32_t flags;
  1586. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1587. return;
  1588. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1589. if (!data || size < 4)
  1590. return;
  1591. flags = bytestream_get_le32(&data);
  1592. size -= 4;
  1593. if (size < 4) /* Required for any of the changes */
  1594. return;
  1595. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1596. avctx->channels = bytestream_get_le32(&data);
  1597. size -= 4;
  1598. }
  1599. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1600. if (size < 8)
  1601. return;
  1602. avctx->channel_layout = bytestream_get_le64(&data);
  1603. size -= 8;
  1604. }
  1605. if (size < 4)
  1606. return;
  1607. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1608. avctx->sample_rate = bytestream_get_le32(&data);
  1609. size -= 4;
  1610. }
  1611. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1612. if (size < 8)
  1613. return;
  1614. avctx->width = bytestream_get_le32(&data);
  1615. avctx->height = bytestream_get_le32(&data);
  1616. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1617. size -= 8;
  1618. }
  1619. }
  1620. static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
  1621. {
  1622. int size, ret = 0;
  1623. const uint8_t *side_metadata;
  1624. const uint8_t *end;
  1625. side_metadata = av_packet_get_side_data(avctx->pkt,
  1626. AV_PKT_DATA_STRINGS_METADATA, &size);
  1627. if (!side_metadata)
  1628. goto end;
  1629. end = side_metadata + size;
  1630. while (side_metadata < end) {
  1631. const uint8_t *key = side_metadata;
  1632. const uint8_t *val = side_metadata + strlen(key) + 1;
  1633. int ret = av_dict_set(avpriv_frame_get_metadatap(frame), key, val, 0);
  1634. if (ret < 0)
  1635. break;
  1636. side_metadata = val + strlen(val) + 1;
  1637. }
  1638. end:
  1639. return ret;
  1640. }
  1641. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1642. int *got_picture_ptr,
  1643. const AVPacket *avpkt)
  1644. {
  1645. AVCodecInternal *avci = avctx->internal;
  1646. int ret;
  1647. // copy to ensure we do not change avpkt
  1648. AVPacket tmp = *avpkt;
  1649. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1650. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1651. return AVERROR(EINVAL);
  1652. }
  1653. *got_picture_ptr = 0;
  1654. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1655. return AVERROR(EINVAL);
  1656. avcodec_get_frame_defaults(picture);
  1657. if (!avctx->refcounted_frames)
  1658. av_frame_unref(&avci->to_free);
  1659. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1660. int did_split = av_packet_split_side_data(&tmp);
  1661. apply_param_change(avctx, &tmp);
  1662. avctx->pkt = &tmp;
  1663. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1664. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1665. &tmp);
  1666. else {
  1667. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1668. &tmp);
  1669. picture->pkt_dts = avpkt->dts;
  1670. if(!avctx->has_b_frames){
  1671. av_frame_set_pkt_pos(picture, avpkt->pos);
  1672. }
  1673. //FIXME these should be under if(!avctx->has_b_frames)
  1674. /* get_buffer is supposed to set frame parameters */
  1675. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  1676. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1677. if (!picture->width) picture->width = avctx->width;
  1678. if (!picture->height) picture->height = avctx->height;
  1679. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1680. }
  1681. }
  1682. add_metadata_from_side_data(avctx, picture);
  1683. emms_c(); //needed to avoid an emms_c() call before every return;
  1684. avctx->pkt = NULL;
  1685. if (did_split) {
  1686. ff_packet_free_side_data(&tmp);
  1687. if(ret == tmp.size)
  1688. ret = avpkt->size;
  1689. }
  1690. if (ret < 0 && picture->data[0])
  1691. av_frame_unref(picture);
  1692. if (*got_picture_ptr) {
  1693. if (!avctx->refcounted_frames) {
  1694. avci->to_free = *picture;
  1695. avci->to_free.extended_data = avci->to_free.data;
  1696. }
  1697. avctx->frame_number++;
  1698. av_frame_set_best_effort_timestamp(picture,
  1699. guess_correct_pts(avctx,
  1700. picture->pkt_pts,
  1701. picture->pkt_dts));
  1702. }
  1703. } else
  1704. ret = 0;
  1705. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1706. * make sure it's set correctly */
  1707. picture->extended_data = picture->data;
  1708. return ret;
  1709. }
  1710. #if FF_API_OLD_DECODE_AUDIO
  1711. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1712. int *frame_size_ptr,
  1713. AVPacket *avpkt)
  1714. {
  1715. AVFrame frame = { { 0 } };
  1716. int ret, got_frame = 0;
  1717. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1718. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1719. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1720. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1721. "avcodec_decode_audio4()\n");
  1722. avctx->get_buffer = avcodec_default_get_buffer;
  1723. avctx->release_buffer = avcodec_default_release_buffer;
  1724. }
  1725. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1726. if (ret >= 0 && got_frame) {
  1727. int ch, plane_size;
  1728. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1729. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1730. frame.nb_samples,
  1731. avctx->sample_fmt, 1);
  1732. if (*frame_size_ptr < data_size) {
  1733. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1734. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1735. return AVERROR(EINVAL);
  1736. }
  1737. memcpy(samples, frame.extended_data[0], plane_size);
  1738. if (planar && avctx->channels > 1) {
  1739. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1740. for (ch = 1; ch < avctx->channels; ch++) {
  1741. memcpy(out, frame.extended_data[ch], plane_size);
  1742. out += plane_size;
  1743. }
  1744. }
  1745. *frame_size_ptr = data_size;
  1746. } else {
  1747. *frame_size_ptr = 0;
  1748. }
  1749. return ret;
  1750. }
  1751. #endif
  1752. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1753. AVFrame *frame,
  1754. int *got_frame_ptr,
  1755. const AVPacket *avpkt)
  1756. {
  1757. AVCodecInternal *avci = avctx->internal;
  1758. int planar, channels;
  1759. int ret = 0;
  1760. *got_frame_ptr = 0;
  1761. if (!avpkt->data && avpkt->size) {
  1762. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1763. return AVERROR(EINVAL);
  1764. }
  1765. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1766. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1767. return AVERROR(EINVAL);
  1768. }
  1769. avcodec_get_frame_defaults(frame);
  1770. if (!avctx->refcounted_frames)
  1771. av_frame_unref(&avci->to_free);
  1772. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1773. uint8_t *side;
  1774. int side_size;
  1775. // copy to ensure we do not change avpkt
  1776. AVPacket tmp = *avpkt;
  1777. int did_split = av_packet_split_side_data(&tmp);
  1778. apply_param_change(avctx, &tmp);
  1779. avctx->pkt = &tmp;
  1780. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1781. if (ret >= 0 && *got_frame_ptr) {
  1782. avctx->frame_number++;
  1783. frame->pkt_dts = avpkt->dts;
  1784. av_frame_set_best_effort_timestamp(frame,
  1785. guess_correct_pts(avctx,
  1786. frame->pkt_pts,
  1787. frame->pkt_dts));
  1788. if (frame->format == AV_SAMPLE_FMT_NONE)
  1789. frame->format = avctx->sample_fmt;
  1790. if (!frame->channel_layout)
  1791. frame->channel_layout = avctx->channel_layout;
  1792. if (!av_frame_get_channels(frame))
  1793. av_frame_set_channels(frame, avctx->channels);
  1794. if (!frame->sample_rate)
  1795. frame->sample_rate = avctx->sample_rate;
  1796. if (!avctx->refcounted_frames) {
  1797. avci->to_free = *frame;
  1798. avci->to_free.extended_data = avci->to_free.data;
  1799. }
  1800. }
  1801. add_metadata_from_side_data(avctx, frame);
  1802. side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1803. if(side && side_size>=10) {
  1804. avctx->internal->skip_samples = AV_RL32(side);
  1805. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  1806. avctx->internal->skip_samples);
  1807. }
  1808. if (avctx->internal->skip_samples && *got_frame_ptr) {
  1809. if(frame->nb_samples <= avctx->internal->skip_samples){
  1810. *got_frame_ptr = 0;
  1811. avctx->internal->skip_samples -= frame->nb_samples;
  1812. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1813. avctx->internal->skip_samples);
  1814. } else {
  1815. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1816. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1817. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1818. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1819. (AVRational){1, avctx->sample_rate},
  1820. avctx->pkt_timebase);
  1821. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1822. frame->pkt_pts += diff_ts;
  1823. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1824. frame->pkt_dts += diff_ts;
  1825. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  1826. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  1827. } else {
  1828. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  1829. }
  1830. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  1831. avctx->internal->skip_samples, frame->nb_samples);
  1832. frame->nb_samples -= avctx->internal->skip_samples;
  1833. avctx->internal->skip_samples = 0;
  1834. }
  1835. }
  1836. avctx->pkt = NULL;
  1837. if (did_split) {
  1838. ff_packet_free_side_data(&tmp);
  1839. if(ret == tmp.size)
  1840. ret = avpkt->size;
  1841. }
  1842. if (ret < 0 && frame->data[0])
  1843. av_frame_unref(frame);
  1844. }
  1845. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1846. * make sure it's set correctly; assume decoders that actually use
  1847. * extended_data are doing it correctly */
  1848. if (*got_frame_ptr) {
  1849. planar = av_sample_fmt_is_planar(frame->format);
  1850. channels = av_frame_get_channels(frame);
  1851. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1852. frame->extended_data = frame->data;
  1853. } else {
  1854. frame->extended_data = NULL;
  1855. }
  1856. return ret;
  1857. }
  1858. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  1859. static int recode_subtitle(AVCodecContext *avctx,
  1860. AVPacket *outpkt, const AVPacket *inpkt)
  1861. {
  1862. #if CONFIG_ICONV
  1863. iconv_t cd = (iconv_t)-1;
  1864. int ret = 0;
  1865. char *inb, *outb;
  1866. size_t inl, outl;
  1867. AVPacket tmp;
  1868. #endif
  1869. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER)
  1870. return 0;
  1871. #if CONFIG_ICONV
  1872. cd = iconv_open("UTF-8", avctx->sub_charenc);
  1873. av_assert0(cd != (iconv_t)-1);
  1874. inb = inpkt->data;
  1875. inl = inpkt->size;
  1876. if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
  1877. av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  1878. ret = AVERROR(ENOMEM);
  1879. goto end;
  1880. }
  1881. ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
  1882. if (ret < 0)
  1883. goto end;
  1884. outpkt->buf = tmp.buf;
  1885. outpkt->data = tmp.data;
  1886. outpkt->size = tmp.size;
  1887. outb = outpkt->data;
  1888. outl = outpkt->size;
  1889. if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  1890. iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  1891. outl >= outpkt->size || inl != 0) {
  1892. av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  1893. "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  1894. av_free_packet(&tmp);
  1895. ret = AVERROR(errno);
  1896. goto end;
  1897. }
  1898. outpkt->size -= outl;
  1899. outpkt->data[outpkt->size - 1] = '\0';
  1900. end:
  1901. if (cd != (iconv_t)-1)
  1902. iconv_close(cd);
  1903. return ret;
  1904. #else
  1905. av_assert0(!"requesting subtitles recoding without iconv");
  1906. #endif
  1907. }
  1908. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1909. int *got_sub_ptr,
  1910. AVPacket *avpkt)
  1911. {
  1912. int ret = 0;
  1913. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1914. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1915. return AVERROR(EINVAL);
  1916. }
  1917. *got_sub_ptr = 0;
  1918. avcodec_get_subtitle_defaults(sub);
  1919. if (avpkt->size) {
  1920. AVPacket pkt_recoded;
  1921. AVPacket tmp = *avpkt;
  1922. int did_split = av_packet_split_side_data(&tmp);
  1923. //apply_param_change(avctx, &tmp);
  1924. pkt_recoded = tmp;
  1925. ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
  1926. if (ret < 0) {
  1927. *got_sub_ptr = 0;
  1928. } else {
  1929. avctx->pkt = &pkt_recoded;
  1930. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  1931. sub->pts = av_rescale_q(avpkt->pts,
  1932. avctx->pkt_timebase, AV_TIME_BASE_Q);
  1933. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
  1934. av_assert1((ret >= 0) >= !!*got_sub_ptr &&
  1935. !!*got_sub_ptr >= !!sub->num_rects);
  1936. if (tmp.data != pkt_recoded.data) { // did we recode?
  1937. /* prevent from destroying side data from original packet */
  1938. pkt_recoded.side_data = NULL;
  1939. pkt_recoded.side_data_elems = 0;
  1940. av_free_packet(&pkt_recoded);
  1941. }
  1942. sub->format = !(avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB);
  1943. avctx->pkt = NULL;
  1944. }
  1945. if (did_split) {
  1946. ff_packet_free_side_data(&tmp);
  1947. if(ret == tmp.size)
  1948. ret = avpkt->size;
  1949. }
  1950. if (*got_sub_ptr)
  1951. avctx->frame_number++;
  1952. }
  1953. return ret;
  1954. }
  1955. void avsubtitle_free(AVSubtitle *sub)
  1956. {
  1957. int i;
  1958. for (i = 0; i < sub->num_rects; i++) {
  1959. av_freep(&sub->rects[i]->pict.data[0]);
  1960. av_freep(&sub->rects[i]->pict.data[1]);
  1961. av_freep(&sub->rects[i]->pict.data[2]);
  1962. av_freep(&sub->rects[i]->pict.data[3]);
  1963. av_freep(&sub->rects[i]->text);
  1964. av_freep(&sub->rects[i]->ass);
  1965. av_freep(&sub->rects[i]);
  1966. }
  1967. av_freep(&sub->rects);
  1968. memset(sub, 0, sizeof(AVSubtitle));
  1969. }
  1970. av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
  1971. {
  1972. int ret = 0;
  1973. ff_unlock_avcodec();
  1974. ret = avcodec_close(avctx);
  1975. ff_lock_avcodec(NULL);
  1976. return ret;
  1977. }
  1978. av_cold int avcodec_close(AVCodecContext *avctx)
  1979. {
  1980. int ret = ff_lock_avcodec(avctx);
  1981. if (ret < 0)
  1982. return ret;
  1983. if (avcodec_is_open(avctx)) {
  1984. FramePool *pool = avctx->internal->pool;
  1985. int i;
  1986. if (CONFIG_FRAME_THREAD_ENCODER &&
  1987. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  1988. ff_unlock_avcodec();
  1989. ff_frame_thread_encoder_free(avctx);
  1990. ff_lock_avcodec(avctx);
  1991. }
  1992. if (HAVE_THREADS && avctx->thread_opaque)
  1993. ff_thread_free(avctx);
  1994. if (avctx->codec && avctx->codec->close)
  1995. avctx->codec->close(avctx);
  1996. avctx->coded_frame = NULL;
  1997. avctx->internal->byte_buffer_size = 0;
  1998. av_freep(&avctx->internal->byte_buffer);
  1999. if (!avctx->refcounted_frames)
  2000. av_frame_unref(&avctx->internal->to_free);
  2001. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  2002. av_buffer_pool_uninit(&pool->pools[i]);
  2003. av_freep(&avctx->internal->pool);
  2004. av_freep(&avctx->internal);
  2005. }
  2006. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  2007. av_opt_free(avctx->priv_data);
  2008. av_opt_free(avctx);
  2009. av_freep(&avctx->priv_data);
  2010. if (av_codec_is_encoder(avctx->codec))
  2011. av_freep(&avctx->extradata);
  2012. avctx->codec = NULL;
  2013. avctx->active_thread_type = 0;
  2014. ff_unlock_avcodec();
  2015. return 0;
  2016. }
  2017. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  2018. {
  2019. switch(id){
  2020. //This is for future deprecatec codec ids, its empty since
  2021. //last major bump but will fill up again over time, please don't remove it
  2022. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  2023. case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
  2024. case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
  2025. default : return id;
  2026. }
  2027. }
  2028. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  2029. {
  2030. AVCodec *p, *experimental = NULL;
  2031. p = first_avcodec;
  2032. id= remap_deprecated_codec_id(id);
  2033. while (p) {
  2034. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  2035. p->id == id) {
  2036. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  2037. experimental = p;
  2038. } else
  2039. return p;
  2040. }
  2041. p = p->next;
  2042. }
  2043. return experimental;
  2044. }
  2045. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  2046. {
  2047. return find_encdec(id, 1);
  2048. }
  2049. AVCodec *avcodec_find_encoder_by_name(const char *name)
  2050. {
  2051. AVCodec *p;
  2052. if (!name)
  2053. return NULL;
  2054. p = first_avcodec;
  2055. while (p) {
  2056. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  2057. return p;
  2058. p = p->next;
  2059. }
  2060. return NULL;
  2061. }
  2062. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  2063. {
  2064. return find_encdec(id, 0);
  2065. }
  2066. AVCodec *avcodec_find_decoder_by_name(const char *name)
  2067. {
  2068. AVCodec *p;
  2069. if (!name)
  2070. return NULL;
  2071. p = first_avcodec;
  2072. while (p) {
  2073. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  2074. return p;
  2075. p = p->next;
  2076. }
  2077. return NULL;
  2078. }
  2079. const char *avcodec_get_name(enum AVCodecID id)
  2080. {
  2081. const AVCodecDescriptor *cd;
  2082. AVCodec *codec;
  2083. if (id == AV_CODEC_ID_NONE)
  2084. return "none";
  2085. cd = avcodec_descriptor_get(id);
  2086. if (cd)
  2087. return cd->name;
  2088. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  2089. codec = avcodec_find_decoder(id);
  2090. if (codec)
  2091. return codec->name;
  2092. codec = avcodec_find_encoder(id);
  2093. if (codec)
  2094. return codec->name;
  2095. return "unknown_codec";
  2096. }
  2097. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  2098. {
  2099. int i, len, ret = 0;
  2100. #define TAG_PRINT(x) \
  2101. (((x) >= '0' && (x) <= '9') || \
  2102. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  2103. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  2104. for (i = 0; i < 4; i++) {
  2105. len = snprintf(buf, buf_size,
  2106. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  2107. buf += len;
  2108. buf_size = buf_size > len ? buf_size - len : 0;
  2109. ret += len;
  2110. codec_tag >>= 8;
  2111. }
  2112. return ret;
  2113. }
  2114. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  2115. {
  2116. const char *codec_type;
  2117. const char *codec_name;
  2118. const char *profile = NULL;
  2119. const AVCodec *p;
  2120. int bitrate;
  2121. AVRational display_aspect_ratio;
  2122. if (!buf || buf_size <= 0)
  2123. return;
  2124. codec_type = av_get_media_type_string(enc->codec_type);
  2125. codec_name = avcodec_get_name(enc->codec_id);
  2126. if (enc->profile != FF_PROFILE_UNKNOWN) {
  2127. if (enc->codec)
  2128. p = enc->codec;
  2129. else
  2130. p = encode ? avcodec_find_encoder(enc->codec_id) :
  2131. avcodec_find_decoder(enc->codec_id);
  2132. if (p)
  2133. profile = av_get_profile_name(p, enc->profile);
  2134. }
  2135. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  2136. codec_name, enc->mb_decision ? " (hq)" : "");
  2137. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  2138. if (profile)
  2139. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  2140. if (enc->codec_tag) {
  2141. char tag_buf[32];
  2142. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  2143. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2144. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  2145. }
  2146. switch (enc->codec_type) {
  2147. case AVMEDIA_TYPE_VIDEO:
  2148. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  2149. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2150. ", %s",
  2151. av_get_pix_fmt_name(enc->pix_fmt));
  2152. if (enc->bits_per_raw_sample &&
  2153. enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
  2154. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2155. " (%d bpc)", enc->bits_per_raw_sample);
  2156. }
  2157. if (enc->width) {
  2158. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2159. ", %dx%d",
  2160. enc->width, enc->height);
  2161. if (enc->sample_aspect_ratio.num) {
  2162. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  2163. enc->width * enc->sample_aspect_ratio.num,
  2164. enc->height * enc->sample_aspect_ratio.den,
  2165. 1024 * 1024);
  2166. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2167. " [SAR %d:%d DAR %d:%d]",
  2168. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  2169. display_aspect_ratio.num, display_aspect_ratio.den);
  2170. }
  2171. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2172. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2173. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2174. ", %d/%d",
  2175. enc->time_base.num / g, enc->time_base.den / g);
  2176. }
  2177. }
  2178. if (encode) {
  2179. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2180. ", q=%d-%d", enc->qmin, enc->qmax);
  2181. }
  2182. break;
  2183. case AVMEDIA_TYPE_AUDIO:
  2184. if (enc->sample_rate) {
  2185. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2186. ", %d Hz", enc->sample_rate);
  2187. }
  2188. av_strlcat(buf, ", ", buf_size);
  2189. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  2190. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  2191. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2192. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  2193. }
  2194. break;
  2195. case AVMEDIA_TYPE_DATA:
  2196. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2197. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2198. if (g)
  2199. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2200. ", %d/%d",
  2201. enc->time_base.num / g, enc->time_base.den / g);
  2202. }
  2203. break;
  2204. default:
  2205. return;
  2206. }
  2207. if (encode) {
  2208. if (enc->flags & CODEC_FLAG_PASS1)
  2209. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2210. ", pass 1");
  2211. if (enc->flags & CODEC_FLAG_PASS2)
  2212. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2213. ", pass 2");
  2214. }
  2215. bitrate = get_bit_rate(enc);
  2216. if (bitrate != 0) {
  2217. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2218. ", %d kb/s", bitrate / 1000);
  2219. }
  2220. }
  2221. const char *av_get_profile_name(const AVCodec *codec, int profile)
  2222. {
  2223. const AVProfile *p;
  2224. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  2225. return NULL;
  2226. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  2227. if (p->profile == profile)
  2228. return p->name;
  2229. return NULL;
  2230. }
  2231. unsigned avcodec_version(void)
  2232. {
  2233. // av_assert0(AV_CODEC_ID_V410==164);
  2234. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  2235. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  2236. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  2237. av_assert0(AV_CODEC_ID_SRT==94216);
  2238. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  2239. av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
  2240. av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
  2241. av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
  2242. av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
  2243. av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
  2244. return LIBAVCODEC_VERSION_INT;
  2245. }
  2246. const char *avcodec_configuration(void)
  2247. {
  2248. return FFMPEG_CONFIGURATION;
  2249. }
  2250. const char *avcodec_license(void)
  2251. {
  2252. #define LICENSE_PREFIX "libavcodec license: "
  2253. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  2254. }
  2255. void avcodec_flush_buffers(AVCodecContext *avctx)
  2256. {
  2257. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2258. ff_thread_flush(avctx);
  2259. else if (avctx->codec->flush)
  2260. avctx->codec->flush(avctx);
  2261. avctx->pts_correction_last_pts =
  2262. avctx->pts_correction_last_dts = INT64_MIN;
  2263. }
  2264. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  2265. {
  2266. switch (codec_id) {
  2267. case AV_CODEC_ID_8SVX_EXP:
  2268. case AV_CODEC_ID_8SVX_FIB:
  2269. case AV_CODEC_ID_ADPCM_CT:
  2270. case AV_CODEC_ID_ADPCM_IMA_APC:
  2271. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  2272. case AV_CODEC_ID_ADPCM_IMA_OKI:
  2273. case AV_CODEC_ID_ADPCM_IMA_WS:
  2274. case AV_CODEC_ID_ADPCM_G722:
  2275. case AV_CODEC_ID_ADPCM_YAMAHA:
  2276. return 4;
  2277. case AV_CODEC_ID_PCM_ALAW:
  2278. case AV_CODEC_ID_PCM_MULAW:
  2279. case AV_CODEC_ID_PCM_S8:
  2280. case AV_CODEC_ID_PCM_S8_PLANAR:
  2281. case AV_CODEC_ID_PCM_U8:
  2282. case AV_CODEC_ID_PCM_ZORK:
  2283. return 8;
  2284. case AV_CODEC_ID_PCM_S16BE:
  2285. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  2286. case AV_CODEC_ID_PCM_S16LE:
  2287. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  2288. case AV_CODEC_ID_PCM_U16BE:
  2289. case AV_CODEC_ID_PCM_U16LE:
  2290. return 16;
  2291. case AV_CODEC_ID_PCM_S24DAUD:
  2292. case AV_CODEC_ID_PCM_S24BE:
  2293. case AV_CODEC_ID_PCM_S24LE:
  2294. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  2295. case AV_CODEC_ID_PCM_U24BE:
  2296. case AV_CODEC_ID_PCM_U24LE:
  2297. return 24;
  2298. case AV_CODEC_ID_PCM_S32BE:
  2299. case AV_CODEC_ID_PCM_S32LE:
  2300. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  2301. case AV_CODEC_ID_PCM_U32BE:
  2302. case AV_CODEC_ID_PCM_U32LE:
  2303. case AV_CODEC_ID_PCM_F32BE:
  2304. case AV_CODEC_ID_PCM_F32LE:
  2305. return 32;
  2306. case AV_CODEC_ID_PCM_F64BE:
  2307. case AV_CODEC_ID_PCM_F64LE:
  2308. return 64;
  2309. default:
  2310. return 0;
  2311. }
  2312. }
  2313. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2314. {
  2315. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2316. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2317. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2318. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2319. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2320. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2321. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2322. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2323. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2324. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2325. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2326. };
  2327. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2328. return AV_CODEC_ID_NONE;
  2329. if (be < 0 || be > 1)
  2330. be = AV_NE(1, 0);
  2331. return map[fmt][be];
  2332. }
  2333. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2334. {
  2335. switch (codec_id) {
  2336. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2337. return 2;
  2338. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2339. return 3;
  2340. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2341. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2342. case AV_CODEC_ID_ADPCM_IMA_QT:
  2343. case AV_CODEC_ID_ADPCM_SWF:
  2344. case AV_CODEC_ID_ADPCM_MS:
  2345. return 4;
  2346. default:
  2347. return av_get_exact_bits_per_sample(codec_id);
  2348. }
  2349. }
  2350. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2351. {
  2352. int id, sr, ch, ba, tag, bps;
  2353. id = avctx->codec_id;
  2354. sr = avctx->sample_rate;
  2355. ch = avctx->channels;
  2356. ba = avctx->block_align;
  2357. tag = avctx->codec_tag;
  2358. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2359. /* codecs with an exact constant bits per sample */
  2360. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2361. return (frame_bytes * 8LL) / (bps * ch);
  2362. bps = avctx->bits_per_coded_sample;
  2363. /* codecs with a fixed packet duration */
  2364. switch (id) {
  2365. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2366. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2367. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2368. case AV_CODEC_ID_AMR_NB:
  2369. case AV_CODEC_ID_EVRC:
  2370. case AV_CODEC_ID_GSM:
  2371. case AV_CODEC_ID_QCELP:
  2372. case AV_CODEC_ID_RA_288: return 160;
  2373. case AV_CODEC_ID_AMR_WB:
  2374. case AV_CODEC_ID_GSM_MS: return 320;
  2375. case AV_CODEC_ID_MP1: return 384;
  2376. case AV_CODEC_ID_ATRAC1: return 512;
  2377. case AV_CODEC_ID_ATRAC3: return 1024;
  2378. case AV_CODEC_ID_MP2:
  2379. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2380. case AV_CODEC_ID_AC3: return 1536;
  2381. }
  2382. if (sr > 0) {
  2383. /* calc from sample rate */
  2384. if (id == AV_CODEC_ID_TTA)
  2385. return 256 * sr / 245;
  2386. if (ch > 0) {
  2387. /* calc from sample rate and channels */
  2388. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2389. return (480 << (sr / 22050)) / ch;
  2390. }
  2391. }
  2392. if (ba > 0) {
  2393. /* calc from block_align */
  2394. if (id == AV_CODEC_ID_SIPR) {
  2395. switch (ba) {
  2396. case 20: return 160;
  2397. case 19: return 144;
  2398. case 29: return 288;
  2399. case 37: return 480;
  2400. }
  2401. } else if (id == AV_CODEC_ID_ILBC) {
  2402. switch (ba) {
  2403. case 38: return 160;
  2404. case 50: return 240;
  2405. }
  2406. }
  2407. }
  2408. if (frame_bytes > 0) {
  2409. /* calc from frame_bytes only */
  2410. if (id == AV_CODEC_ID_TRUESPEECH)
  2411. return 240 * (frame_bytes / 32);
  2412. if (id == AV_CODEC_ID_NELLYMOSER)
  2413. return 256 * (frame_bytes / 64);
  2414. if (id == AV_CODEC_ID_RA_144)
  2415. return 160 * (frame_bytes / 20);
  2416. if (id == AV_CODEC_ID_G723_1)
  2417. return 240 * (frame_bytes / 24);
  2418. if (bps > 0) {
  2419. /* calc from frame_bytes and bits_per_coded_sample */
  2420. if (id == AV_CODEC_ID_ADPCM_G726)
  2421. return frame_bytes * 8 / bps;
  2422. }
  2423. if (ch > 0) {
  2424. /* calc from frame_bytes and channels */
  2425. switch (id) {
  2426. case AV_CODEC_ID_ADPCM_AFC:
  2427. return frame_bytes / (9 * ch) * 16;
  2428. case AV_CODEC_ID_ADPCM_4XM:
  2429. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2430. return (frame_bytes - 4 * ch) * 2 / ch;
  2431. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2432. return (frame_bytes - 4) * 2 / ch;
  2433. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2434. return (frame_bytes - 8) * 2 / ch;
  2435. case AV_CODEC_ID_ADPCM_XA:
  2436. return (frame_bytes / 128) * 224 / ch;
  2437. case AV_CODEC_ID_INTERPLAY_DPCM:
  2438. return (frame_bytes - 6 - ch) / ch;
  2439. case AV_CODEC_ID_ROQ_DPCM:
  2440. return (frame_bytes - 8) / ch;
  2441. case AV_CODEC_ID_XAN_DPCM:
  2442. return (frame_bytes - 2 * ch) / ch;
  2443. case AV_CODEC_ID_MACE3:
  2444. return 3 * frame_bytes / ch;
  2445. case AV_CODEC_ID_MACE6:
  2446. return 6 * frame_bytes / ch;
  2447. case AV_CODEC_ID_PCM_LXF:
  2448. return 2 * (frame_bytes / (5 * ch));
  2449. case AV_CODEC_ID_IAC:
  2450. case AV_CODEC_ID_IMC:
  2451. return 4 * frame_bytes / ch;
  2452. }
  2453. if (tag) {
  2454. /* calc from frame_bytes, channels, and codec_tag */
  2455. if (id == AV_CODEC_ID_SOL_DPCM) {
  2456. if (tag == 3)
  2457. return frame_bytes / ch;
  2458. else
  2459. return frame_bytes * 2 / ch;
  2460. }
  2461. }
  2462. if (ba > 0) {
  2463. /* calc from frame_bytes, channels, and block_align */
  2464. int blocks = frame_bytes / ba;
  2465. switch (avctx->codec_id) {
  2466. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2467. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  2468. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2469. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2470. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2471. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2472. case AV_CODEC_ID_ADPCM_MS:
  2473. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2474. }
  2475. }
  2476. if (bps > 0) {
  2477. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2478. switch (avctx->codec_id) {
  2479. case AV_CODEC_ID_PCM_DVD:
  2480. if(bps<4)
  2481. return 0;
  2482. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2483. case AV_CODEC_ID_PCM_BLURAY:
  2484. if(bps<4)
  2485. return 0;
  2486. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2487. case AV_CODEC_ID_S302M:
  2488. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2489. }
  2490. }
  2491. }
  2492. }
  2493. return 0;
  2494. }
  2495. #if !HAVE_THREADS
  2496. int ff_thread_init(AVCodecContext *s)
  2497. {
  2498. return -1;
  2499. }
  2500. #endif
  2501. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2502. {
  2503. unsigned int n = 0;
  2504. while (v >= 0xff) {
  2505. *s++ = 0xff;
  2506. v -= 0xff;
  2507. n++;
  2508. }
  2509. *s = v;
  2510. n++;
  2511. return n;
  2512. }
  2513. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2514. {
  2515. int i;
  2516. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2517. return i;
  2518. }
  2519. #if FF_API_MISSING_SAMPLE
  2520. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2521. {
  2522. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2523. "version to the newest one from Git. If the problem still "
  2524. "occurs, it means that your file has a feature which has not "
  2525. "been implemented.\n", feature);
  2526. if(want_sample)
  2527. av_log_ask_for_sample(avc, NULL);
  2528. }
  2529. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2530. {
  2531. va_list argument_list;
  2532. va_start(argument_list, msg);
  2533. if (msg)
  2534. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2535. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2536. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  2537. "and contact the ffmpeg-devel mailing list.\n");
  2538. va_end(argument_list);
  2539. }
  2540. #endif /* FF_API_MISSING_SAMPLE */
  2541. static AVHWAccel *first_hwaccel = NULL;
  2542. void av_register_hwaccel(AVHWAccel *hwaccel)
  2543. {
  2544. AVHWAccel **p = &first_hwaccel;
  2545. while (*p)
  2546. p = &(*p)->next;
  2547. *p = hwaccel;
  2548. hwaccel->next = NULL;
  2549. }
  2550. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2551. {
  2552. return hwaccel ? hwaccel->next : first_hwaccel;
  2553. }
  2554. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  2555. {
  2556. AVHWAccel *hwaccel = NULL;
  2557. while ((hwaccel = av_hwaccel_next(hwaccel)))
  2558. if (hwaccel->id == codec_id
  2559. && hwaccel->pix_fmt == pix_fmt)
  2560. return hwaccel;
  2561. return NULL;
  2562. }
  2563. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2564. {
  2565. if (ff_lockmgr_cb) {
  2566. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2567. return -1;
  2568. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2569. return -1;
  2570. }
  2571. ff_lockmgr_cb = cb;
  2572. if (ff_lockmgr_cb) {
  2573. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2574. return -1;
  2575. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2576. return -1;
  2577. }
  2578. return 0;
  2579. }
  2580. int ff_lock_avcodec(AVCodecContext *log_ctx)
  2581. {
  2582. if (ff_lockmgr_cb) {
  2583. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  2584. return -1;
  2585. }
  2586. entangled_thread_counter++;
  2587. if (entangled_thread_counter != 1) {
  2588. av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
  2589. ff_avcodec_locked = 1;
  2590. ff_unlock_avcodec();
  2591. return AVERROR(EINVAL);
  2592. }
  2593. av_assert0(!ff_avcodec_locked);
  2594. ff_avcodec_locked = 1;
  2595. return 0;
  2596. }
  2597. int ff_unlock_avcodec(void)
  2598. {
  2599. av_assert0(ff_avcodec_locked);
  2600. ff_avcodec_locked = 0;
  2601. entangled_thread_counter--;
  2602. if (ff_lockmgr_cb) {
  2603. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  2604. return -1;
  2605. }
  2606. return 0;
  2607. }
  2608. int avpriv_lock_avformat(void)
  2609. {
  2610. if (ff_lockmgr_cb) {
  2611. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2612. return -1;
  2613. }
  2614. return 0;
  2615. }
  2616. int avpriv_unlock_avformat(void)
  2617. {
  2618. if (ff_lockmgr_cb) {
  2619. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2620. return -1;
  2621. }
  2622. return 0;
  2623. }
  2624. unsigned int avpriv_toupper4(unsigned int x)
  2625. {
  2626. return av_toupper(x & 0xFF) +
  2627. (av_toupper((x >> 8) & 0xFF) << 8) +
  2628. (av_toupper((x >> 16) & 0xFF) << 16) +
  2629. (av_toupper((x >> 24) & 0xFF) << 24);
  2630. }
  2631. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  2632. {
  2633. int ret;
  2634. dst->owner = src->owner;
  2635. ret = av_frame_ref(dst->f, src->f);
  2636. if (ret < 0)
  2637. return ret;
  2638. if (src->progress &&
  2639. !(dst->progress = av_buffer_ref(src->progress))) {
  2640. ff_thread_release_buffer(dst->owner, dst);
  2641. return AVERROR(ENOMEM);
  2642. }
  2643. return 0;
  2644. }
  2645. #if !HAVE_THREADS
  2646. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  2647. {
  2648. f->owner = avctx;
  2649. return ff_get_buffer(avctx, f->f, flags);
  2650. }
  2651. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  2652. {
  2653. av_frame_unref(f->f);
  2654. }
  2655. void ff_thread_finish_setup(AVCodecContext *avctx)
  2656. {
  2657. }
  2658. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  2659. {
  2660. }
  2661. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  2662. {
  2663. }
  2664. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2665. {
  2666. return 1;
  2667. }
  2668. #endif
  2669. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  2670. {
  2671. AVCodec *c= avcodec_find_decoder(codec_id);
  2672. if(!c)
  2673. c= avcodec_find_encoder(codec_id);
  2674. if(c)
  2675. return c->type;
  2676. if (codec_id <= AV_CODEC_ID_NONE)
  2677. return AVMEDIA_TYPE_UNKNOWN;
  2678. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  2679. return AVMEDIA_TYPE_VIDEO;
  2680. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  2681. return AVMEDIA_TYPE_AUDIO;
  2682. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  2683. return AVMEDIA_TYPE_SUBTITLE;
  2684. return AVMEDIA_TYPE_UNKNOWN;
  2685. }
  2686. int avcodec_is_open(AVCodecContext *s)
  2687. {
  2688. return !!s->internal;
  2689. }
  2690. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  2691. {
  2692. int ret;
  2693. char *str;
  2694. ret = av_bprint_finalize(buf, &str);
  2695. if (ret < 0)
  2696. return ret;
  2697. avctx->extradata = str;
  2698. /* Note: the string is NUL terminated (so extradata can be read as a
  2699. * string), but the ending character is not accounted in the size (in
  2700. * binary formats you are likely not supposed to mux that character). When
  2701. * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
  2702. * zeros. */
  2703. avctx->extradata_size = buf->len;
  2704. return 0;
  2705. }