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.

3336 lines
109KB

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