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.

3335 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. int av_codec_get_max_lowres(const AVCodec *codec)
  904. {
  905. return codec->max_lowres;
  906. }
  907. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  908. {
  909. memset(sub, 0, sizeof(*sub));
  910. sub->pts = AV_NOPTS_VALUE;
  911. }
  912. static int get_bit_rate(AVCodecContext *ctx)
  913. {
  914. int bit_rate;
  915. int bits_per_sample;
  916. switch (ctx->codec_type) {
  917. case AVMEDIA_TYPE_VIDEO:
  918. case AVMEDIA_TYPE_DATA:
  919. case AVMEDIA_TYPE_SUBTITLE:
  920. case AVMEDIA_TYPE_ATTACHMENT:
  921. bit_rate = ctx->bit_rate;
  922. break;
  923. case AVMEDIA_TYPE_AUDIO:
  924. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  925. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  926. break;
  927. default:
  928. bit_rate = 0;
  929. break;
  930. }
  931. return bit_rate;
  932. }
  933. #if FF_API_AVCODEC_OPEN
  934. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  935. {
  936. return avcodec_open2(avctx, codec, NULL);
  937. }
  938. #endif
  939. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  940. {
  941. int ret = 0;
  942. ff_unlock_avcodec();
  943. ret = avcodec_open2(avctx, codec, options);
  944. ff_lock_avcodec(avctx);
  945. return ret;
  946. }
  947. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  948. {
  949. int ret = 0;
  950. AVDictionary *tmp = NULL;
  951. if (avcodec_is_open(avctx))
  952. return 0;
  953. if ((!codec && !avctx->codec)) {
  954. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  955. return AVERROR(EINVAL);
  956. }
  957. if ((codec && avctx->codec && codec != avctx->codec)) {
  958. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  959. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  960. return AVERROR(EINVAL);
  961. }
  962. if (!codec)
  963. codec = avctx->codec;
  964. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  965. return AVERROR(EINVAL);
  966. if (options)
  967. av_dict_copy(&tmp, *options, 0);
  968. ret = ff_lock_avcodec(avctx);
  969. if (ret < 0)
  970. return ret;
  971. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  972. if (!avctx->internal) {
  973. ret = AVERROR(ENOMEM);
  974. goto end;
  975. }
  976. avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
  977. if (!avctx->internal->pool) {
  978. ret = AVERROR(ENOMEM);
  979. goto free_and_end;
  980. }
  981. if (codec->priv_data_size > 0) {
  982. if (!avctx->priv_data) {
  983. avctx->priv_data = av_mallocz(codec->priv_data_size);
  984. if (!avctx->priv_data) {
  985. ret = AVERROR(ENOMEM);
  986. goto end;
  987. }
  988. if (codec->priv_class) {
  989. *(const AVClass **)avctx->priv_data = codec->priv_class;
  990. av_opt_set_defaults(avctx->priv_data);
  991. }
  992. }
  993. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  994. goto free_and_end;
  995. } else {
  996. avctx->priv_data = NULL;
  997. }
  998. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  999. goto free_and_end;
  1000. // only call avcodec_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
  1001. if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
  1002. (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
  1003. if (avctx->coded_width && avctx->coded_height)
  1004. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  1005. else if (avctx->width && avctx->height)
  1006. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1007. }
  1008. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  1009. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  1010. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  1011. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  1012. avcodec_set_dimensions(avctx, 0, 0);
  1013. }
  1014. /* if the decoder init function was already called previously,
  1015. * free the already allocated subtitle_header before overwriting it */
  1016. if (av_codec_is_decoder(codec))
  1017. av_freep(&avctx->subtitle_header);
  1018. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  1019. ret = AVERROR(EINVAL);
  1020. goto free_and_end;
  1021. }
  1022. avctx->codec = codec;
  1023. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  1024. avctx->codec_id == AV_CODEC_ID_NONE) {
  1025. avctx->codec_type = codec->type;
  1026. avctx->codec_id = codec->id;
  1027. }
  1028. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  1029. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  1030. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  1031. ret = AVERROR(EINVAL);
  1032. goto free_and_end;
  1033. }
  1034. avctx->frame_number = 0;
  1035. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  1036. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  1037. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  1038. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  1039. AVCodec *codec2;
  1040. av_log(avctx, AV_LOG_ERROR,
  1041. "The %s '%s' is experimental but experimental codecs are not enabled, "
  1042. "add '-strict %d' if you want to use it.\n",
  1043. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  1044. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  1045. if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
  1046. av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  1047. codec_string, codec2->name);
  1048. ret = AVERROR_EXPERIMENTAL;
  1049. goto free_and_end;
  1050. }
  1051. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  1052. (!avctx->time_base.num || !avctx->time_base.den)) {
  1053. avctx->time_base.num = 1;
  1054. avctx->time_base.den = avctx->sample_rate;
  1055. }
  1056. if (!HAVE_THREADS)
  1057. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  1058. if (CONFIG_FRAME_THREAD_ENCODER) {
  1059. ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  1060. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  1061. ff_lock_avcodec(avctx);
  1062. if (ret < 0)
  1063. goto free_and_end;
  1064. }
  1065. if (HAVE_THREADS
  1066. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  1067. ret = ff_thread_init(avctx);
  1068. if (ret < 0) {
  1069. goto free_and_end;
  1070. }
  1071. }
  1072. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  1073. avctx->thread_count = 1;
  1074. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  1075. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  1076. avctx->codec->max_lowres);
  1077. ret = AVERROR(EINVAL);
  1078. goto free_and_end;
  1079. }
  1080. if (av_codec_is_encoder(avctx->codec)) {
  1081. int i;
  1082. if (avctx->codec->sample_fmts) {
  1083. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  1084. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  1085. break;
  1086. if (avctx->channels == 1 &&
  1087. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  1088. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  1089. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  1090. break;
  1091. }
  1092. }
  1093. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  1094. char buf[128];
  1095. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  1096. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  1097. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  1098. ret = AVERROR(EINVAL);
  1099. goto free_and_end;
  1100. }
  1101. }
  1102. if (avctx->codec->pix_fmts) {
  1103. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  1104. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  1105. break;
  1106. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  1107. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  1108. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  1109. char buf[128];
  1110. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  1111. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  1112. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  1113. ret = AVERROR(EINVAL);
  1114. goto free_and_end;
  1115. }
  1116. }
  1117. if (avctx->codec->supported_samplerates) {
  1118. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  1119. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  1120. break;
  1121. if (avctx->codec->supported_samplerates[i] == 0) {
  1122. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  1123. avctx->sample_rate);
  1124. ret = AVERROR(EINVAL);
  1125. goto free_and_end;
  1126. }
  1127. }
  1128. if (avctx->codec->channel_layouts) {
  1129. if (!avctx->channel_layout) {
  1130. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  1131. } else {
  1132. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  1133. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  1134. break;
  1135. if (avctx->codec->channel_layouts[i] == 0) {
  1136. char buf[512];
  1137. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1138. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  1139. ret = AVERROR(EINVAL);
  1140. goto free_and_end;
  1141. }
  1142. }
  1143. }
  1144. if (avctx->channel_layout && avctx->channels) {
  1145. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1146. if (channels != avctx->channels) {
  1147. char buf[512];
  1148. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1149. av_log(avctx, AV_LOG_ERROR,
  1150. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  1151. buf, channels, avctx->channels);
  1152. ret = AVERROR(EINVAL);
  1153. goto free_and_end;
  1154. }
  1155. } else if (avctx->channel_layout) {
  1156. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1157. }
  1158. if(avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
  1159. avctx->codec_id != AV_CODEC_ID_PNG // For mplayer
  1160. ) {
  1161. if (avctx->width <= 0 || avctx->height <= 0) {
  1162. av_log(avctx, AV_LOG_ERROR, "dimensions not set\n");
  1163. ret = AVERROR(EINVAL);
  1164. goto free_and_end;
  1165. }
  1166. }
  1167. if ( (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
  1168. && avctx->bit_rate>0 && avctx->bit_rate<1000) {
  1169. av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
  1170. }
  1171. if (!avctx->rc_initial_buffer_occupancy)
  1172. avctx->rc_initial_buffer_occupancy = avctx->rc_buffer_size * 3 / 4;
  1173. }
  1174. avctx->pts_correction_num_faulty_pts =
  1175. avctx->pts_correction_num_faulty_dts = 0;
  1176. avctx->pts_correction_last_pts =
  1177. avctx->pts_correction_last_dts = INT64_MIN;
  1178. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  1179. || avctx->internal->frame_thread_encoder)) {
  1180. ret = avctx->codec->init(avctx);
  1181. if (ret < 0) {
  1182. goto free_and_end;
  1183. }
  1184. }
  1185. ret=0;
  1186. if (av_codec_is_decoder(avctx->codec)) {
  1187. if (!avctx->bit_rate)
  1188. avctx->bit_rate = get_bit_rate(avctx);
  1189. /* validate channel layout from the decoder */
  1190. if (avctx->channel_layout) {
  1191. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  1192. if (!avctx->channels)
  1193. avctx->channels = channels;
  1194. else if (channels != avctx->channels) {
  1195. char buf[512];
  1196. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  1197. av_log(avctx, AV_LOG_WARNING,
  1198. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  1199. "ignoring specified channel layout\n",
  1200. buf, channels, avctx->channels);
  1201. avctx->channel_layout = 0;
  1202. }
  1203. }
  1204. if (avctx->channels && avctx->channels < 0 ||
  1205. avctx->channels > FF_SANE_NB_CHANNELS) {
  1206. ret = AVERROR(EINVAL);
  1207. goto free_and_end;
  1208. }
  1209. if (avctx->sub_charenc) {
  1210. if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
  1211. av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
  1212. "supported with subtitles codecs\n");
  1213. ret = AVERROR(EINVAL);
  1214. goto free_and_end;
  1215. } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
  1216. av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
  1217. "subtitles character encoding will be ignored\n",
  1218. avctx->codec_descriptor->name);
  1219. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
  1220. } else {
  1221. /* input character encoding is set for a text based subtitle
  1222. * codec at this point */
  1223. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
  1224. avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
  1225. if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
  1226. #if CONFIG_ICONV
  1227. iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
  1228. if (cd == (iconv_t)-1) {
  1229. av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
  1230. "with input character encoding \"%s\"\n", avctx->sub_charenc);
  1231. ret = AVERROR(errno);
  1232. goto free_and_end;
  1233. }
  1234. iconv_close(cd);
  1235. #else
  1236. av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
  1237. "conversion needs a libavcodec built with iconv support "
  1238. "for this codec\n");
  1239. ret = AVERROR(ENOSYS);
  1240. goto free_and_end;
  1241. #endif
  1242. }
  1243. }
  1244. }
  1245. }
  1246. end:
  1247. ff_unlock_avcodec();
  1248. if (options) {
  1249. av_dict_free(options);
  1250. *options = tmp;
  1251. }
  1252. return ret;
  1253. free_and_end:
  1254. av_dict_free(&tmp);
  1255. av_freep(&avctx->priv_data);
  1256. if (avctx->internal)
  1257. av_freep(&avctx->internal->pool);
  1258. av_freep(&avctx->internal);
  1259. avctx->codec = NULL;
  1260. goto end;
  1261. }
  1262. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
  1263. {
  1264. if (avpkt->size < 0) {
  1265. av_log(avctx, AV_LOG_ERROR, "Invalid negative user packet size %d\n", avpkt->size);
  1266. return AVERROR(EINVAL);
  1267. }
  1268. if (size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  1269. av_log(avctx, AV_LOG_ERROR, "Invalid minimum required packet size %"PRId64" (max allowed is %d)\n",
  1270. size, INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
  1271. return AVERROR(EINVAL);
  1272. }
  1273. if (avctx) {
  1274. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  1275. if (!avpkt->data || avpkt->size < size) {
  1276. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  1277. avpkt->data = avctx->internal->byte_buffer;
  1278. avpkt->size = avctx->internal->byte_buffer_size;
  1279. avpkt->destruct = NULL;
  1280. }
  1281. }
  1282. if (avpkt->data) {
  1283. AVBufferRef *buf = avpkt->buf;
  1284. #if FF_API_DESTRUCT_PACKET
  1285. FF_DISABLE_DEPRECATION_WARNINGS
  1286. void *destruct = avpkt->destruct;
  1287. FF_ENABLE_DEPRECATION_WARNINGS
  1288. #endif
  1289. if (avpkt->size < size) {
  1290. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %"PRId64")\n", avpkt->size, size);
  1291. return AVERROR(EINVAL);
  1292. }
  1293. av_init_packet(avpkt);
  1294. #if FF_API_DESTRUCT_PACKET
  1295. FF_DISABLE_DEPRECATION_WARNINGS
  1296. avpkt->destruct = destruct;
  1297. FF_ENABLE_DEPRECATION_WARNINGS
  1298. #endif
  1299. avpkt->buf = buf;
  1300. avpkt->size = size;
  1301. return 0;
  1302. } else {
  1303. int ret = av_new_packet(avpkt, size);
  1304. if (ret < 0)
  1305. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %"PRId64"\n", size);
  1306. return ret;
  1307. }
  1308. }
  1309. int ff_alloc_packet(AVPacket *avpkt, int size)
  1310. {
  1311. return ff_alloc_packet2(NULL, avpkt, size);
  1312. }
  1313. /**
  1314. * Pad last frame with silence.
  1315. */
  1316. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1317. {
  1318. AVFrame *frame = NULL;
  1319. int ret;
  1320. if (!(frame = avcodec_alloc_frame()))
  1321. return AVERROR(ENOMEM);
  1322. frame->format = src->format;
  1323. frame->channel_layout = src->channel_layout;
  1324. av_frame_set_channels(frame, av_frame_get_channels(src));
  1325. frame->nb_samples = s->frame_size;
  1326. ret = av_frame_get_buffer(frame, 32);
  1327. if (ret < 0)
  1328. goto fail;
  1329. ret = av_frame_copy_props(frame, src);
  1330. if (ret < 0)
  1331. goto fail;
  1332. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1333. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1334. goto fail;
  1335. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1336. frame->nb_samples - src->nb_samples,
  1337. s->channels, s->sample_fmt)) < 0)
  1338. goto fail;
  1339. *dst = frame;
  1340. return 0;
  1341. fail:
  1342. av_frame_free(&frame);
  1343. return ret;
  1344. }
  1345. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1346. AVPacket *avpkt,
  1347. const AVFrame *frame,
  1348. int *got_packet_ptr)
  1349. {
  1350. AVFrame tmp;
  1351. AVFrame *padded_frame = NULL;
  1352. int ret;
  1353. AVPacket user_pkt = *avpkt;
  1354. int needs_realloc = !user_pkt.data;
  1355. *got_packet_ptr = 0;
  1356. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1357. av_free_packet(avpkt);
  1358. av_init_packet(avpkt);
  1359. return 0;
  1360. }
  1361. /* ensure that extended_data is properly set */
  1362. if (frame && !frame->extended_data) {
  1363. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1364. avctx->channels > AV_NUM_DATA_POINTERS) {
  1365. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1366. "with more than %d channels, but extended_data is not set.\n",
  1367. AV_NUM_DATA_POINTERS);
  1368. return AVERROR(EINVAL);
  1369. }
  1370. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1371. tmp = *frame;
  1372. tmp.extended_data = tmp.data;
  1373. frame = &tmp;
  1374. }
  1375. /* check for valid frame size */
  1376. if (frame) {
  1377. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1378. if (frame->nb_samples > avctx->frame_size) {
  1379. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1380. return AVERROR(EINVAL);
  1381. }
  1382. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1383. if (frame->nb_samples < avctx->frame_size &&
  1384. !avctx->internal->last_audio_frame) {
  1385. ret = pad_last_frame(avctx, &padded_frame, frame);
  1386. if (ret < 0)
  1387. return ret;
  1388. frame = padded_frame;
  1389. avctx->internal->last_audio_frame = 1;
  1390. }
  1391. if (frame->nb_samples != avctx->frame_size) {
  1392. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1393. ret = AVERROR(EINVAL);
  1394. goto end;
  1395. }
  1396. }
  1397. }
  1398. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1399. if (!ret) {
  1400. if (*got_packet_ptr) {
  1401. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1402. if (avpkt->pts == AV_NOPTS_VALUE)
  1403. avpkt->pts = frame->pts;
  1404. if (!avpkt->duration)
  1405. avpkt->duration = ff_samples_to_time_base(avctx,
  1406. frame->nb_samples);
  1407. }
  1408. avpkt->dts = avpkt->pts;
  1409. } else {
  1410. avpkt->size = 0;
  1411. }
  1412. }
  1413. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1414. needs_realloc = 0;
  1415. if (user_pkt.data) {
  1416. if (user_pkt.size >= avpkt->size) {
  1417. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1418. } else {
  1419. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1420. avpkt->size = user_pkt.size;
  1421. ret = -1;
  1422. }
  1423. avpkt->buf = user_pkt.buf;
  1424. avpkt->data = user_pkt.data;
  1425. avpkt->destruct = user_pkt.destruct;
  1426. } else {
  1427. if (av_dup_packet(avpkt) < 0) {
  1428. ret = AVERROR(ENOMEM);
  1429. }
  1430. }
  1431. }
  1432. if (!ret) {
  1433. if (needs_realloc && avpkt->data) {
  1434. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1435. if (ret >= 0)
  1436. avpkt->data = avpkt->buf->data;
  1437. }
  1438. avctx->frame_number++;
  1439. }
  1440. if (ret < 0 || !*got_packet_ptr) {
  1441. av_free_packet(avpkt);
  1442. av_init_packet(avpkt);
  1443. goto end;
  1444. }
  1445. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1446. * this needs to be moved to the encoders, but for now we can do it
  1447. * here to simplify things */
  1448. avpkt->flags |= AV_PKT_FLAG_KEY;
  1449. end:
  1450. av_frame_free(&padded_frame);
  1451. return ret;
  1452. }
  1453. #if FF_API_OLD_ENCODE_AUDIO
  1454. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1455. uint8_t *buf, int buf_size,
  1456. const short *samples)
  1457. {
  1458. AVPacket pkt;
  1459. AVFrame frame0 = { { 0 } };
  1460. AVFrame *frame;
  1461. int ret, samples_size, got_packet;
  1462. av_init_packet(&pkt);
  1463. pkt.data = buf;
  1464. pkt.size = buf_size;
  1465. if (samples) {
  1466. frame = &frame0;
  1467. avcodec_get_frame_defaults(frame);
  1468. if (avctx->frame_size) {
  1469. frame->nb_samples = avctx->frame_size;
  1470. } else {
  1471. /* if frame_size is not set, the number of samples must be
  1472. * calculated from the buffer size */
  1473. int64_t nb_samples;
  1474. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1475. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1476. "support this codec\n");
  1477. return AVERROR(EINVAL);
  1478. }
  1479. nb_samples = (int64_t)buf_size * 8 /
  1480. (av_get_bits_per_sample(avctx->codec_id) *
  1481. avctx->channels);
  1482. if (nb_samples >= INT_MAX)
  1483. return AVERROR(EINVAL);
  1484. frame->nb_samples = nb_samples;
  1485. }
  1486. /* it is assumed that the samples buffer is large enough based on the
  1487. * relevant parameters */
  1488. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1489. frame->nb_samples,
  1490. avctx->sample_fmt, 1);
  1491. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1492. avctx->sample_fmt,
  1493. (const uint8_t *)samples,
  1494. samples_size, 1)) < 0)
  1495. return ret;
  1496. /* fabricate frame pts from sample count.
  1497. * this is needed because the avcodec_encode_audio() API does not have
  1498. * a way for the user to provide pts */
  1499. if (avctx->sample_rate && avctx->time_base.num)
  1500. frame->pts = ff_samples_to_time_base(avctx,
  1501. avctx->internal->sample_count);
  1502. else
  1503. frame->pts = AV_NOPTS_VALUE;
  1504. avctx->internal->sample_count += frame->nb_samples;
  1505. } else {
  1506. frame = NULL;
  1507. }
  1508. got_packet = 0;
  1509. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1510. if (!ret && got_packet && avctx->coded_frame) {
  1511. avctx->coded_frame->pts = pkt.pts;
  1512. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1513. }
  1514. /* free any side data since we cannot return it */
  1515. av_packet_free_side_data(&pkt);
  1516. if (frame && frame->extended_data != frame->data)
  1517. av_freep(&frame->extended_data);
  1518. return ret ? ret : pkt.size;
  1519. }
  1520. #endif
  1521. #if FF_API_OLD_ENCODE_VIDEO
  1522. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1523. const AVFrame *pict)
  1524. {
  1525. AVPacket pkt;
  1526. int ret, got_packet = 0;
  1527. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1528. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1529. return -1;
  1530. }
  1531. av_init_packet(&pkt);
  1532. pkt.data = buf;
  1533. pkt.size = buf_size;
  1534. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1535. if (!ret && got_packet && avctx->coded_frame) {
  1536. avctx->coded_frame->pts = pkt.pts;
  1537. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1538. }
  1539. /* free any side data since we cannot return it */
  1540. if (pkt.side_data_elems > 0) {
  1541. int i;
  1542. for (i = 0; i < pkt.side_data_elems; i++)
  1543. av_free(pkt.side_data[i].data);
  1544. av_freep(&pkt.side_data);
  1545. pkt.side_data_elems = 0;
  1546. }
  1547. return ret ? ret : pkt.size;
  1548. }
  1549. #endif
  1550. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1551. AVPacket *avpkt,
  1552. const AVFrame *frame,
  1553. int *got_packet_ptr)
  1554. {
  1555. int ret;
  1556. AVPacket user_pkt = *avpkt;
  1557. int needs_realloc = !user_pkt.data;
  1558. *got_packet_ptr = 0;
  1559. if(CONFIG_FRAME_THREAD_ENCODER &&
  1560. avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1561. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1562. if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
  1563. avctx->stats_out[0] = '\0';
  1564. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1565. av_free_packet(avpkt);
  1566. av_init_packet(avpkt);
  1567. avpkt->size = 0;
  1568. return 0;
  1569. }
  1570. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1571. return AVERROR(EINVAL);
  1572. av_assert0(avctx->codec->encode2);
  1573. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1574. av_assert0(ret <= 0);
  1575. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1576. needs_realloc = 0;
  1577. if (user_pkt.data) {
  1578. if (user_pkt.size >= avpkt->size) {
  1579. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1580. } else {
  1581. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1582. avpkt->size = user_pkt.size;
  1583. ret = -1;
  1584. }
  1585. avpkt->buf = user_pkt.buf;
  1586. avpkt->data = user_pkt.data;
  1587. avpkt->destruct = user_pkt.destruct;
  1588. } else {
  1589. if (av_dup_packet(avpkt) < 0) {
  1590. ret = AVERROR(ENOMEM);
  1591. }
  1592. }
  1593. }
  1594. if (!ret) {
  1595. if (!*got_packet_ptr)
  1596. avpkt->size = 0;
  1597. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1598. avpkt->pts = avpkt->dts = frame->pts;
  1599. if (needs_realloc && avpkt->data) {
  1600. ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1601. if (ret >= 0)
  1602. avpkt->data = avpkt->buf->data;
  1603. }
  1604. avctx->frame_number++;
  1605. }
  1606. if (ret < 0 || !*got_packet_ptr)
  1607. av_free_packet(avpkt);
  1608. else
  1609. av_packet_merge_side_data(avpkt);
  1610. emms_c();
  1611. return ret;
  1612. }
  1613. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1614. const AVSubtitle *sub)
  1615. {
  1616. int ret;
  1617. if (sub->start_display_time) {
  1618. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1619. return -1;
  1620. }
  1621. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1622. avctx->frame_number++;
  1623. return ret;
  1624. }
  1625. /**
  1626. * Attempt to guess proper monotonic timestamps for decoded video frames
  1627. * which might have incorrect times. Input timestamps may wrap around, in
  1628. * which case the output will as well.
  1629. *
  1630. * @param pts the pts field of the decoded AVPacket, as passed through
  1631. * AVFrame.pkt_pts
  1632. * @param dts the dts field of the decoded AVPacket
  1633. * @return one of the input values, may be AV_NOPTS_VALUE
  1634. */
  1635. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1636. int64_t reordered_pts, int64_t dts)
  1637. {
  1638. int64_t pts = AV_NOPTS_VALUE;
  1639. if (dts != AV_NOPTS_VALUE) {
  1640. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1641. ctx->pts_correction_last_dts = dts;
  1642. }
  1643. if (reordered_pts != AV_NOPTS_VALUE) {
  1644. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1645. ctx->pts_correction_last_pts = reordered_pts;
  1646. }
  1647. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1648. && reordered_pts != AV_NOPTS_VALUE)
  1649. pts = reordered_pts;
  1650. else
  1651. pts = dts;
  1652. return pts;
  1653. }
  1654. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1655. {
  1656. int size = 0;
  1657. const uint8_t *data;
  1658. uint32_t flags;
  1659. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1660. return;
  1661. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1662. if (!data || size < 4)
  1663. return;
  1664. flags = bytestream_get_le32(&data);
  1665. size -= 4;
  1666. if (size < 4) /* Required for any of the changes */
  1667. return;
  1668. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1669. avctx->channels = bytestream_get_le32(&data);
  1670. size -= 4;
  1671. }
  1672. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1673. if (size < 8)
  1674. return;
  1675. avctx->channel_layout = bytestream_get_le64(&data);
  1676. size -= 8;
  1677. }
  1678. if (size < 4)
  1679. return;
  1680. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1681. avctx->sample_rate = bytestream_get_le32(&data);
  1682. size -= 4;
  1683. }
  1684. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1685. if (size < 8)
  1686. return;
  1687. avctx->width = bytestream_get_le32(&data);
  1688. avctx->height = bytestream_get_le32(&data);
  1689. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1690. size -= 8;
  1691. }
  1692. }
  1693. static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
  1694. {
  1695. int size, ret = 0;
  1696. const uint8_t *side_metadata;
  1697. const uint8_t *end;
  1698. side_metadata = av_packet_get_side_data(avctx->pkt,
  1699. AV_PKT_DATA_STRINGS_METADATA, &size);
  1700. if (!side_metadata)
  1701. goto end;
  1702. end = side_metadata + size;
  1703. while (side_metadata < end) {
  1704. const uint8_t *key = side_metadata;
  1705. const uint8_t *val = side_metadata + strlen(key) + 1;
  1706. int ret = av_dict_set(avpriv_frame_get_metadatap(frame), key, val, 0);
  1707. if (ret < 0)
  1708. break;
  1709. side_metadata = val + strlen(val) + 1;
  1710. }
  1711. end:
  1712. return ret;
  1713. }
  1714. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1715. int *got_picture_ptr,
  1716. const AVPacket *avpkt)
  1717. {
  1718. AVCodecInternal *avci = avctx->internal;
  1719. int ret;
  1720. // copy to ensure we do not change avpkt
  1721. AVPacket tmp = *avpkt;
  1722. if (!avctx->codec)
  1723. return AVERROR(EINVAL);
  1724. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1725. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1726. return AVERROR(EINVAL);
  1727. }
  1728. *got_picture_ptr = 0;
  1729. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1730. return AVERROR(EINVAL);
  1731. avcodec_get_frame_defaults(picture);
  1732. if (!avctx->refcounted_frames)
  1733. av_frame_unref(&avci->to_free);
  1734. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1735. int did_split = av_packet_split_side_data(&tmp);
  1736. apply_param_change(avctx, &tmp);
  1737. avctx->pkt = &tmp;
  1738. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1739. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1740. &tmp);
  1741. else {
  1742. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1743. &tmp);
  1744. picture->pkt_dts = avpkt->dts;
  1745. if(!avctx->has_b_frames){
  1746. av_frame_set_pkt_pos(picture, avpkt->pos);
  1747. }
  1748. //FIXME these should be under if(!avctx->has_b_frames)
  1749. /* get_buffer is supposed to set frame parameters */
  1750. if (!(avctx->codec->capabilities & CODEC_CAP_DR1)) {
  1751. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1752. if (!picture->width) picture->width = avctx->width;
  1753. if (!picture->height) picture->height = avctx->height;
  1754. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1755. }
  1756. }
  1757. add_metadata_from_side_data(avctx, picture);
  1758. emms_c(); //needed to avoid an emms_c() call before every return;
  1759. avctx->pkt = NULL;
  1760. if (did_split) {
  1761. av_packet_free_side_data(&tmp);
  1762. if(ret == tmp.size)
  1763. ret = avpkt->size;
  1764. }
  1765. if (ret < 0 && picture->data[0])
  1766. av_frame_unref(picture);
  1767. if (*got_picture_ptr) {
  1768. if (!avctx->refcounted_frames) {
  1769. avci->to_free = *picture;
  1770. avci->to_free.extended_data = avci->to_free.data;
  1771. memset(picture->buf, 0, sizeof(picture->buf));
  1772. }
  1773. avctx->frame_number++;
  1774. av_frame_set_best_effort_timestamp(picture,
  1775. guess_correct_pts(avctx,
  1776. picture->pkt_pts,
  1777. picture->pkt_dts));
  1778. }
  1779. } else
  1780. ret = 0;
  1781. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1782. * make sure it's set correctly */
  1783. picture->extended_data = picture->data;
  1784. return ret;
  1785. }
  1786. #if FF_API_OLD_DECODE_AUDIO
  1787. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1788. int *frame_size_ptr,
  1789. AVPacket *avpkt)
  1790. {
  1791. AVFrame frame = { { 0 } };
  1792. int ret, got_frame = 0;
  1793. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1794. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1795. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1796. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1797. "avcodec_decode_audio4()\n");
  1798. avctx->get_buffer = avcodec_default_get_buffer;
  1799. avctx->release_buffer = avcodec_default_release_buffer;
  1800. }
  1801. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1802. if (ret >= 0 && got_frame) {
  1803. int ch, plane_size;
  1804. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1805. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1806. frame.nb_samples,
  1807. avctx->sample_fmt, 1);
  1808. if (*frame_size_ptr < data_size) {
  1809. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1810. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1811. return AVERROR(EINVAL);
  1812. }
  1813. memcpy(samples, frame.extended_data[0], plane_size);
  1814. if (planar && avctx->channels > 1) {
  1815. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1816. for (ch = 1; ch < avctx->channels; ch++) {
  1817. memcpy(out, frame.extended_data[ch], plane_size);
  1818. out += plane_size;
  1819. }
  1820. }
  1821. *frame_size_ptr = data_size;
  1822. } else {
  1823. *frame_size_ptr = 0;
  1824. }
  1825. return ret;
  1826. }
  1827. #endif
  1828. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1829. AVFrame *frame,
  1830. int *got_frame_ptr,
  1831. const AVPacket *avpkt)
  1832. {
  1833. AVCodecInternal *avci = avctx->internal;
  1834. int planar, channels;
  1835. int ret = 0;
  1836. *got_frame_ptr = 0;
  1837. if (!avpkt->data && avpkt->size) {
  1838. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1839. return AVERROR(EINVAL);
  1840. }
  1841. if (!avctx->codec)
  1842. return AVERROR(EINVAL);
  1843. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1844. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1845. return AVERROR(EINVAL);
  1846. }
  1847. avcodec_get_frame_defaults(frame);
  1848. if (!avctx->refcounted_frames)
  1849. av_frame_unref(&avci->to_free);
  1850. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1851. uint8_t *side;
  1852. int side_size;
  1853. uint32_t discard_padding = 0;
  1854. // copy to ensure we do not change avpkt
  1855. AVPacket tmp = *avpkt;
  1856. int did_split = av_packet_split_side_data(&tmp);
  1857. apply_param_change(avctx, &tmp);
  1858. avctx->pkt = &tmp;
  1859. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1860. ret = ff_thread_decode_frame(avctx, frame, got_frame_ptr, &tmp);
  1861. else {
  1862. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1863. frame->pkt_dts = avpkt->dts;
  1864. }
  1865. if (ret >= 0 && *got_frame_ptr) {
  1866. add_metadata_from_side_data(avctx, frame);
  1867. avctx->frame_number++;
  1868. av_frame_set_best_effort_timestamp(frame,
  1869. guess_correct_pts(avctx,
  1870. frame->pkt_pts,
  1871. frame->pkt_dts));
  1872. if (frame->format == AV_SAMPLE_FMT_NONE)
  1873. frame->format = avctx->sample_fmt;
  1874. if (!frame->channel_layout)
  1875. frame->channel_layout = avctx->channel_layout;
  1876. if (!av_frame_get_channels(frame))
  1877. av_frame_set_channels(frame, avctx->channels);
  1878. if (!frame->sample_rate)
  1879. frame->sample_rate = avctx->sample_rate;
  1880. }
  1881. side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1882. if(side && side_size>=10) {
  1883. avctx->internal->skip_samples = AV_RL32(side);
  1884. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  1885. avctx->internal->skip_samples);
  1886. discard_padding = AV_RL32(side + 4);
  1887. }
  1888. if (avctx->internal->skip_samples && *got_frame_ptr) {
  1889. if(frame->nb_samples <= avctx->internal->skip_samples){
  1890. *got_frame_ptr = 0;
  1891. avctx->internal->skip_samples -= frame->nb_samples;
  1892. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1893. avctx->internal->skip_samples);
  1894. } else {
  1895. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1896. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1897. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1898. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1899. (AVRational){1, avctx->sample_rate},
  1900. avctx->pkt_timebase);
  1901. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1902. frame->pkt_pts += diff_ts;
  1903. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1904. frame->pkt_dts += diff_ts;
  1905. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  1906. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  1907. } else {
  1908. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  1909. }
  1910. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  1911. avctx->internal->skip_samples, frame->nb_samples);
  1912. frame->nb_samples -= avctx->internal->skip_samples;
  1913. avctx->internal->skip_samples = 0;
  1914. }
  1915. }
  1916. if (discard_padding > 0 && discard_padding <= frame->nb_samples && *got_frame_ptr) {
  1917. if (discard_padding == frame->nb_samples) {
  1918. *got_frame_ptr = 0;
  1919. } else {
  1920. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1921. int64_t diff_ts = av_rescale_q(frame->nb_samples - discard_padding,
  1922. (AVRational){1, avctx->sample_rate},
  1923. avctx->pkt_timebase);
  1924. if (av_frame_get_pkt_duration(frame) >= diff_ts)
  1925. av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
  1926. } else {
  1927. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for discarded samples.\n");
  1928. }
  1929. av_log(avctx, AV_LOG_DEBUG, "discard %d/%d samples\n",
  1930. discard_padding, frame->nb_samples);
  1931. frame->nb_samples -= discard_padding;
  1932. }
  1933. }
  1934. avctx->pkt = NULL;
  1935. if (did_split) {
  1936. av_packet_free_side_data(&tmp);
  1937. if(ret == tmp.size)
  1938. ret = avpkt->size;
  1939. }
  1940. if (ret >= 0 && *got_frame_ptr) {
  1941. if (!avctx->refcounted_frames) {
  1942. avci->to_free = *frame;
  1943. avci->to_free.extended_data = avci->to_free.data;
  1944. memset(frame->buf, 0, sizeof(frame->buf));
  1945. frame->extended_buf = NULL;
  1946. frame->nb_extended_buf = 0;
  1947. }
  1948. } else if (frame->data[0])
  1949. av_frame_unref(frame);
  1950. }
  1951. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1952. * make sure it's set correctly; assume decoders that actually use
  1953. * extended_data are doing it correctly */
  1954. if (*got_frame_ptr) {
  1955. planar = av_sample_fmt_is_planar(frame->format);
  1956. channels = av_frame_get_channels(frame);
  1957. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1958. frame->extended_data = frame->data;
  1959. } else {
  1960. frame->extended_data = NULL;
  1961. }
  1962. return ret;
  1963. }
  1964. #define UTF8_MAX_BYTES 4 /* 5 and 6 bytes sequences should not be used */
  1965. static int recode_subtitle(AVCodecContext *avctx,
  1966. AVPacket *outpkt, const AVPacket *inpkt)
  1967. {
  1968. #if CONFIG_ICONV
  1969. iconv_t cd = (iconv_t)-1;
  1970. int ret = 0;
  1971. char *inb, *outb;
  1972. size_t inl, outl;
  1973. AVPacket tmp;
  1974. #endif
  1975. if (avctx->sub_charenc_mode != FF_SUB_CHARENC_MODE_PRE_DECODER)
  1976. return 0;
  1977. #if CONFIG_ICONV
  1978. cd = iconv_open("UTF-8", avctx->sub_charenc);
  1979. av_assert0(cd != (iconv_t)-1);
  1980. inb = inpkt->data;
  1981. inl = inpkt->size;
  1982. if (inl >= INT_MAX / UTF8_MAX_BYTES - FF_INPUT_BUFFER_PADDING_SIZE) {
  1983. av_log(avctx, AV_LOG_ERROR, "Subtitles packet is too big for recoding\n");
  1984. ret = AVERROR(ENOMEM);
  1985. goto end;
  1986. }
  1987. ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
  1988. if (ret < 0)
  1989. goto end;
  1990. outpkt->buf = tmp.buf;
  1991. outpkt->data = tmp.data;
  1992. outpkt->size = tmp.size;
  1993. outb = outpkt->data;
  1994. outl = outpkt->size;
  1995. if (iconv(cd, &inb, &inl, &outb, &outl) == (size_t)-1 ||
  1996. iconv(cd, NULL, NULL, &outb, &outl) == (size_t)-1 ||
  1997. outl >= outpkt->size || inl != 0) {
  1998. av_log(avctx, AV_LOG_ERROR, "Unable to recode subtitle event \"%s\" "
  1999. "from %s to UTF-8\n", inpkt->data, avctx->sub_charenc);
  2000. av_free_packet(&tmp);
  2001. ret = AVERROR(errno);
  2002. goto end;
  2003. }
  2004. outpkt->size -= outl;
  2005. memset(outpkt->data + outpkt->size, 0, outl);
  2006. end:
  2007. if (cd != (iconv_t)-1)
  2008. iconv_close(cd);
  2009. return ret;
  2010. #else
  2011. av_assert0(!"requesting subtitles recoding without iconv");
  2012. #endif
  2013. }
  2014. static int utf8_check(const uint8_t *str)
  2015. {
  2016. const uint8_t *byte;
  2017. uint32_t codepoint, min;
  2018. while (*str) {
  2019. byte = str;
  2020. GET_UTF8(codepoint, *(byte++), return 0;);
  2021. min = byte - str == 1 ? 0 : byte - str == 2 ? 0x80 :
  2022. 1 << (5 * (byte - str) - 4);
  2023. if (codepoint < min || codepoint >= 0x110000 ||
  2024. codepoint == 0xFFFE /* BOM */ ||
  2025. codepoint >= 0xD800 && codepoint <= 0xDFFF /* surrogates */)
  2026. return 0;
  2027. str = byte;
  2028. }
  2029. return 1;
  2030. }
  2031. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  2032. int *got_sub_ptr,
  2033. AVPacket *avpkt)
  2034. {
  2035. int i, ret = 0;
  2036. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  2037. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  2038. return AVERROR(EINVAL);
  2039. }
  2040. *got_sub_ptr = 0;
  2041. avcodec_get_subtitle_defaults(sub);
  2042. if (avpkt->size) {
  2043. AVPacket pkt_recoded;
  2044. AVPacket tmp = *avpkt;
  2045. int did_split = av_packet_split_side_data(&tmp);
  2046. //apply_param_change(avctx, &tmp);
  2047. pkt_recoded = tmp;
  2048. ret = recode_subtitle(avctx, &pkt_recoded, &tmp);
  2049. if (ret < 0) {
  2050. *got_sub_ptr = 0;
  2051. } else {
  2052. avctx->pkt = &pkt_recoded;
  2053. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  2054. sub->pts = av_rescale_q(avpkt->pts,
  2055. avctx->pkt_timebase, AV_TIME_BASE_Q);
  2056. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
  2057. av_assert1((ret >= 0) >= !!*got_sub_ptr &&
  2058. !!*got_sub_ptr >= !!sub->num_rects);
  2059. if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
  2060. avctx->pkt_timebase.num) {
  2061. AVRational ms = { 1, 1000 };
  2062. sub->end_display_time = av_rescale_q(avpkt->duration,
  2063. avctx->pkt_timebase, ms);
  2064. }
  2065. for (i = 0; i < sub->num_rects; i++) {
  2066. if (sub->rects[i]->ass && !utf8_check(sub->rects[i]->ass)) {
  2067. av_log(avctx, AV_LOG_ERROR,
  2068. "Invalid UTF-8 in decoded subtitles text; "
  2069. "maybe missing -sub_charenc option\n");
  2070. avsubtitle_free(sub);
  2071. return AVERROR_INVALIDDATA;
  2072. }
  2073. }
  2074. if (tmp.data != pkt_recoded.data) { // did we recode?
  2075. /* prevent from destroying side data from original packet */
  2076. pkt_recoded.side_data = NULL;
  2077. pkt_recoded.side_data_elems = 0;
  2078. av_free_packet(&pkt_recoded);
  2079. }
  2080. if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB)
  2081. sub->format = 0;
  2082. else if (avctx->codec_descriptor->props & AV_CODEC_PROP_TEXT_SUB)
  2083. sub->format = 1;
  2084. avctx->pkt = NULL;
  2085. }
  2086. if (did_split) {
  2087. av_packet_free_side_data(&tmp);
  2088. if(ret == tmp.size)
  2089. ret = avpkt->size;
  2090. }
  2091. if (*got_sub_ptr)
  2092. avctx->frame_number++;
  2093. }
  2094. return ret;
  2095. }
  2096. void avsubtitle_free(AVSubtitle *sub)
  2097. {
  2098. int i;
  2099. for (i = 0; i < sub->num_rects; i++) {
  2100. av_freep(&sub->rects[i]->pict.data[0]);
  2101. av_freep(&sub->rects[i]->pict.data[1]);
  2102. av_freep(&sub->rects[i]->pict.data[2]);
  2103. av_freep(&sub->rects[i]->pict.data[3]);
  2104. av_freep(&sub->rects[i]->text);
  2105. av_freep(&sub->rects[i]->ass);
  2106. av_freep(&sub->rects[i]);
  2107. }
  2108. av_freep(&sub->rects);
  2109. memset(sub, 0, sizeof(AVSubtitle));
  2110. }
  2111. av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
  2112. {
  2113. int ret = 0;
  2114. ff_unlock_avcodec();
  2115. ret = avcodec_close(avctx);
  2116. ff_lock_avcodec(NULL);
  2117. return ret;
  2118. }
  2119. av_cold int avcodec_close(AVCodecContext *avctx)
  2120. {
  2121. int ret;
  2122. if (!avctx)
  2123. return 0;
  2124. ret = ff_lock_avcodec(avctx);
  2125. if (ret < 0)
  2126. return ret;
  2127. if (avcodec_is_open(avctx)) {
  2128. FramePool *pool = avctx->internal->pool;
  2129. int i;
  2130. if (CONFIG_FRAME_THREAD_ENCODER &&
  2131. avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  2132. ff_unlock_avcodec();
  2133. ff_frame_thread_encoder_free(avctx);
  2134. ff_lock_avcodec(avctx);
  2135. }
  2136. if (HAVE_THREADS && avctx->thread_opaque)
  2137. ff_thread_free(avctx);
  2138. if (avctx->codec && avctx->codec->close)
  2139. avctx->codec->close(avctx);
  2140. avctx->coded_frame = NULL;
  2141. avctx->internal->byte_buffer_size = 0;
  2142. av_freep(&avctx->internal->byte_buffer);
  2143. if (!avctx->refcounted_frames)
  2144. av_frame_unref(&avctx->internal->to_free);
  2145. for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
  2146. av_buffer_pool_uninit(&pool->pools[i]);
  2147. av_freep(&avctx->internal->pool);
  2148. av_freep(&avctx->internal);
  2149. }
  2150. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  2151. av_opt_free(avctx->priv_data);
  2152. av_opt_free(avctx);
  2153. av_freep(&avctx->priv_data);
  2154. if (av_codec_is_encoder(avctx->codec))
  2155. av_freep(&avctx->extradata);
  2156. avctx->codec = NULL;
  2157. avctx->active_thread_type = 0;
  2158. ff_unlock_avcodec();
  2159. return 0;
  2160. }
  2161. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  2162. {
  2163. switch(id){
  2164. //This is for future deprecatec codec ids, its empty since
  2165. //last major bump but will fill up again over time, please don't remove it
  2166. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  2167. case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
  2168. case AV_CODEC_ID_TAK_DEPRECATED : return AV_CODEC_ID_TAK;
  2169. case AV_CODEC_ID_PCM_S24LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S24LE_PLANAR;
  2170. case AV_CODEC_ID_PCM_S32LE_PLANAR_DEPRECATED : return AV_CODEC_ID_PCM_S32LE_PLANAR;
  2171. case AV_CODEC_ID_ESCAPE130_DEPRECATED : return AV_CODEC_ID_ESCAPE130;
  2172. case AV_CODEC_ID_G2M_DEPRECATED : return AV_CODEC_ID_G2M;
  2173. case AV_CODEC_ID_WEBP_DEPRECATED: return AV_CODEC_ID_WEBP;
  2174. default : return id;
  2175. }
  2176. }
  2177. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  2178. {
  2179. AVCodec *p, *experimental = NULL;
  2180. p = first_avcodec;
  2181. id= remap_deprecated_codec_id(id);
  2182. while (p) {
  2183. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  2184. p->id == id) {
  2185. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  2186. experimental = p;
  2187. } else
  2188. return p;
  2189. }
  2190. p = p->next;
  2191. }
  2192. return experimental;
  2193. }
  2194. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  2195. {
  2196. return find_encdec(id, 1);
  2197. }
  2198. AVCodec *avcodec_find_encoder_by_name(const char *name)
  2199. {
  2200. AVCodec *p;
  2201. if (!name)
  2202. return NULL;
  2203. p = first_avcodec;
  2204. while (p) {
  2205. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  2206. return p;
  2207. p = p->next;
  2208. }
  2209. return NULL;
  2210. }
  2211. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  2212. {
  2213. return find_encdec(id, 0);
  2214. }
  2215. AVCodec *avcodec_find_decoder_by_name(const char *name)
  2216. {
  2217. AVCodec *p;
  2218. if (!name)
  2219. return NULL;
  2220. p = first_avcodec;
  2221. while (p) {
  2222. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  2223. return p;
  2224. p = p->next;
  2225. }
  2226. return NULL;
  2227. }
  2228. const char *avcodec_get_name(enum AVCodecID id)
  2229. {
  2230. const AVCodecDescriptor *cd;
  2231. AVCodec *codec;
  2232. if (id == AV_CODEC_ID_NONE)
  2233. return "none";
  2234. cd = avcodec_descriptor_get(id);
  2235. if (cd)
  2236. return cd->name;
  2237. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  2238. codec = avcodec_find_decoder(id);
  2239. if (codec)
  2240. return codec->name;
  2241. codec = avcodec_find_encoder(id);
  2242. if (codec)
  2243. return codec->name;
  2244. return "unknown_codec";
  2245. }
  2246. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  2247. {
  2248. int i, len, ret = 0;
  2249. #define TAG_PRINT(x) \
  2250. (((x) >= '0' && (x) <= '9') || \
  2251. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  2252. ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
  2253. for (i = 0; i < 4; i++) {
  2254. len = snprintf(buf, buf_size,
  2255. TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  2256. buf += len;
  2257. buf_size = buf_size > len ? buf_size - len : 0;
  2258. ret += len;
  2259. codec_tag >>= 8;
  2260. }
  2261. return ret;
  2262. }
  2263. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  2264. {
  2265. const char *codec_type;
  2266. const char *codec_name;
  2267. const char *profile = NULL;
  2268. const AVCodec *p;
  2269. int bitrate;
  2270. AVRational display_aspect_ratio;
  2271. if (!buf || buf_size <= 0)
  2272. return;
  2273. codec_type = av_get_media_type_string(enc->codec_type);
  2274. codec_name = avcodec_get_name(enc->codec_id);
  2275. if (enc->profile != FF_PROFILE_UNKNOWN) {
  2276. if (enc->codec)
  2277. p = enc->codec;
  2278. else
  2279. p = encode ? avcodec_find_encoder(enc->codec_id) :
  2280. avcodec_find_decoder(enc->codec_id);
  2281. if (p)
  2282. profile = av_get_profile_name(p, enc->profile);
  2283. }
  2284. snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
  2285. codec_name);
  2286. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  2287. if (enc->codec && strcmp(enc->codec->name, codec_name))
  2288. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
  2289. if (profile)
  2290. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  2291. if (enc->codec_tag) {
  2292. char tag_buf[32];
  2293. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  2294. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2295. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  2296. }
  2297. switch (enc->codec_type) {
  2298. case AVMEDIA_TYPE_VIDEO:
  2299. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  2300. char detail[256] = "(";
  2301. const char *colorspace_name;
  2302. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2303. ", %s",
  2304. av_get_pix_fmt_name(enc->pix_fmt));
  2305. if (enc->bits_per_raw_sample &&
  2306. enc->bits_per_raw_sample <= av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth_minus1)
  2307. av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
  2308. if (enc->color_range != AVCOL_RANGE_UNSPECIFIED)
  2309. av_strlcatf(detail, sizeof(detail),
  2310. enc->color_range == AVCOL_RANGE_MPEG ? "tv, ": "pc, ");
  2311. colorspace_name = av_get_colorspace_name(enc->colorspace);
  2312. if (colorspace_name)
  2313. av_strlcatf(detail, sizeof(detail), "%s, ", colorspace_name);
  2314. if (strlen(detail) > 1) {
  2315. detail[strlen(detail) - 2] = 0;
  2316. av_strlcatf(buf, buf_size, "%s)", detail);
  2317. }
  2318. }
  2319. if (enc->width) {
  2320. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2321. ", %dx%d",
  2322. enc->width, enc->height);
  2323. if (enc->sample_aspect_ratio.num) {
  2324. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  2325. enc->width * enc->sample_aspect_ratio.num,
  2326. enc->height * enc->sample_aspect_ratio.den,
  2327. 1024 * 1024);
  2328. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2329. " [SAR %d:%d DAR %d:%d]",
  2330. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  2331. display_aspect_ratio.num, display_aspect_ratio.den);
  2332. }
  2333. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2334. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2335. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2336. ", %d/%d",
  2337. enc->time_base.num / g, enc->time_base.den / g);
  2338. }
  2339. }
  2340. if (encode) {
  2341. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2342. ", q=%d-%d", enc->qmin, enc->qmax);
  2343. }
  2344. break;
  2345. case AVMEDIA_TYPE_AUDIO:
  2346. if (enc->sample_rate) {
  2347. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2348. ", %d Hz", enc->sample_rate);
  2349. }
  2350. av_strlcat(buf, ", ", buf_size);
  2351. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  2352. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  2353. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2354. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  2355. }
  2356. break;
  2357. case AVMEDIA_TYPE_DATA:
  2358. if (av_log_get_level() >= AV_LOG_DEBUG) {
  2359. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  2360. if (g)
  2361. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2362. ", %d/%d",
  2363. enc->time_base.num / g, enc->time_base.den / g);
  2364. }
  2365. break;
  2366. case AVMEDIA_TYPE_SUBTITLE:
  2367. if (enc->width)
  2368. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2369. ", %dx%d", enc->width, enc->height);
  2370. break;
  2371. default:
  2372. return;
  2373. }
  2374. if (encode) {
  2375. if (enc->flags & CODEC_FLAG_PASS1)
  2376. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2377. ", pass 1");
  2378. if (enc->flags & CODEC_FLAG_PASS2)
  2379. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2380. ", pass 2");
  2381. }
  2382. bitrate = get_bit_rate(enc);
  2383. if (bitrate != 0) {
  2384. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2385. ", %d kb/s", bitrate / 1000);
  2386. } else if (enc->rc_max_rate > 0) {
  2387. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  2388. ", max. %d kb/s", enc->rc_max_rate / 1000);
  2389. }
  2390. }
  2391. const char *av_get_profile_name(const AVCodec *codec, int profile)
  2392. {
  2393. const AVProfile *p;
  2394. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  2395. return NULL;
  2396. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  2397. if (p->profile == profile)
  2398. return p->name;
  2399. return NULL;
  2400. }
  2401. unsigned avcodec_version(void)
  2402. {
  2403. // av_assert0(AV_CODEC_ID_V410==164);
  2404. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  2405. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  2406. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  2407. av_assert0(AV_CODEC_ID_SRT==94216);
  2408. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  2409. av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
  2410. av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
  2411. av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
  2412. av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
  2413. av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
  2414. return LIBAVCODEC_VERSION_INT;
  2415. }
  2416. const char *avcodec_configuration(void)
  2417. {
  2418. return FFMPEG_CONFIGURATION;
  2419. }
  2420. const char *avcodec_license(void)
  2421. {
  2422. #define LICENSE_PREFIX "libavcodec license: "
  2423. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  2424. }
  2425. void avcodec_flush_buffers(AVCodecContext *avctx)
  2426. {
  2427. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  2428. ff_thread_flush(avctx);
  2429. else if (avctx->codec->flush)
  2430. avctx->codec->flush(avctx);
  2431. avctx->pts_correction_last_pts =
  2432. avctx->pts_correction_last_dts = INT64_MIN;
  2433. if (!avctx->refcounted_frames)
  2434. av_frame_unref(&avctx->internal->to_free);
  2435. }
  2436. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  2437. {
  2438. switch (codec_id) {
  2439. case AV_CODEC_ID_8SVX_EXP:
  2440. case AV_CODEC_ID_8SVX_FIB:
  2441. case AV_CODEC_ID_ADPCM_CT:
  2442. case AV_CODEC_ID_ADPCM_IMA_APC:
  2443. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  2444. case AV_CODEC_ID_ADPCM_IMA_OKI:
  2445. case AV_CODEC_ID_ADPCM_IMA_WS:
  2446. case AV_CODEC_ID_ADPCM_G722:
  2447. case AV_CODEC_ID_ADPCM_YAMAHA:
  2448. return 4;
  2449. case AV_CODEC_ID_PCM_ALAW:
  2450. case AV_CODEC_ID_PCM_MULAW:
  2451. case AV_CODEC_ID_PCM_S8:
  2452. case AV_CODEC_ID_PCM_S8_PLANAR:
  2453. case AV_CODEC_ID_PCM_U8:
  2454. case AV_CODEC_ID_PCM_ZORK:
  2455. return 8;
  2456. case AV_CODEC_ID_PCM_S16BE:
  2457. case AV_CODEC_ID_PCM_S16BE_PLANAR:
  2458. case AV_CODEC_ID_PCM_S16LE:
  2459. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  2460. case AV_CODEC_ID_PCM_U16BE:
  2461. case AV_CODEC_ID_PCM_U16LE:
  2462. return 16;
  2463. case AV_CODEC_ID_PCM_S24DAUD:
  2464. case AV_CODEC_ID_PCM_S24BE:
  2465. case AV_CODEC_ID_PCM_S24LE:
  2466. case AV_CODEC_ID_PCM_S24LE_PLANAR:
  2467. case AV_CODEC_ID_PCM_U24BE:
  2468. case AV_CODEC_ID_PCM_U24LE:
  2469. return 24;
  2470. case AV_CODEC_ID_PCM_S32BE:
  2471. case AV_CODEC_ID_PCM_S32LE:
  2472. case AV_CODEC_ID_PCM_S32LE_PLANAR:
  2473. case AV_CODEC_ID_PCM_U32BE:
  2474. case AV_CODEC_ID_PCM_U32LE:
  2475. case AV_CODEC_ID_PCM_F32BE:
  2476. case AV_CODEC_ID_PCM_F32LE:
  2477. return 32;
  2478. case AV_CODEC_ID_PCM_F64BE:
  2479. case AV_CODEC_ID_PCM_F64LE:
  2480. return 64;
  2481. default:
  2482. return 0;
  2483. }
  2484. }
  2485. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2486. {
  2487. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2488. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2489. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2490. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2491. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2492. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2493. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2494. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2495. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2496. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2497. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2498. };
  2499. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2500. return AV_CODEC_ID_NONE;
  2501. if (be < 0 || be > 1)
  2502. be = AV_NE(1, 0);
  2503. return map[fmt][be];
  2504. }
  2505. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2506. {
  2507. switch (codec_id) {
  2508. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2509. return 2;
  2510. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2511. return 3;
  2512. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2513. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2514. case AV_CODEC_ID_ADPCM_IMA_QT:
  2515. case AV_CODEC_ID_ADPCM_SWF:
  2516. case AV_CODEC_ID_ADPCM_MS:
  2517. return 4;
  2518. default:
  2519. return av_get_exact_bits_per_sample(codec_id);
  2520. }
  2521. }
  2522. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2523. {
  2524. int id, sr, ch, ba, tag, bps;
  2525. id = avctx->codec_id;
  2526. sr = avctx->sample_rate;
  2527. ch = avctx->channels;
  2528. ba = avctx->block_align;
  2529. tag = avctx->codec_tag;
  2530. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2531. /* codecs with an exact constant bits per sample */
  2532. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2533. return (frame_bytes * 8LL) / (bps * ch);
  2534. bps = avctx->bits_per_coded_sample;
  2535. /* codecs with a fixed packet duration */
  2536. switch (id) {
  2537. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2538. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2539. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2540. case AV_CODEC_ID_AMR_NB:
  2541. case AV_CODEC_ID_EVRC:
  2542. case AV_CODEC_ID_GSM:
  2543. case AV_CODEC_ID_QCELP:
  2544. case AV_CODEC_ID_RA_288: return 160;
  2545. case AV_CODEC_ID_AMR_WB:
  2546. case AV_CODEC_ID_GSM_MS: return 320;
  2547. case AV_CODEC_ID_MP1: return 384;
  2548. case AV_CODEC_ID_ATRAC1: return 512;
  2549. case AV_CODEC_ID_ATRAC3: return 1024;
  2550. case AV_CODEC_ID_MP2:
  2551. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2552. case AV_CODEC_ID_AC3: return 1536;
  2553. }
  2554. if (sr > 0) {
  2555. /* calc from sample rate */
  2556. if (id == AV_CODEC_ID_TTA)
  2557. return 256 * sr / 245;
  2558. if (ch > 0) {
  2559. /* calc from sample rate and channels */
  2560. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2561. return (480 << (sr / 22050)) / ch;
  2562. }
  2563. }
  2564. if (ba > 0) {
  2565. /* calc from block_align */
  2566. if (id == AV_CODEC_ID_SIPR) {
  2567. switch (ba) {
  2568. case 20: return 160;
  2569. case 19: return 144;
  2570. case 29: return 288;
  2571. case 37: return 480;
  2572. }
  2573. } else if (id == AV_CODEC_ID_ILBC) {
  2574. switch (ba) {
  2575. case 38: return 160;
  2576. case 50: return 240;
  2577. }
  2578. }
  2579. }
  2580. if (frame_bytes > 0) {
  2581. /* calc from frame_bytes only */
  2582. if (id == AV_CODEC_ID_TRUESPEECH)
  2583. return 240 * (frame_bytes / 32);
  2584. if (id == AV_CODEC_ID_NELLYMOSER)
  2585. return 256 * (frame_bytes / 64);
  2586. if (id == AV_CODEC_ID_RA_144)
  2587. return 160 * (frame_bytes / 20);
  2588. if (id == AV_CODEC_ID_G723_1)
  2589. return 240 * (frame_bytes / 24);
  2590. if (bps > 0) {
  2591. /* calc from frame_bytes and bits_per_coded_sample */
  2592. if (id == AV_CODEC_ID_ADPCM_G726)
  2593. return frame_bytes * 8 / bps;
  2594. }
  2595. if (ch > 0) {
  2596. /* calc from frame_bytes and channels */
  2597. switch (id) {
  2598. case AV_CODEC_ID_ADPCM_AFC:
  2599. return frame_bytes / (9 * ch) * 16;
  2600. case AV_CODEC_ID_ADPCM_DTK:
  2601. return frame_bytes / (16 * ch) * 28;
  2602. case AV_CODEC_ID_ADPCM_4XM:
  2603. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2604. return (frame_bytes - 4 * ch) * 2 / ch;
  2605. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2606. return (frame_bytes - 4) * 2 / ch;
  2607. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2608. return (frame_bytes - 8) * 2 / ch;
  2609. case AV_CODEC_ID_ADPCM_XA:
  2610. return (frame_bytes / 128) * 224 / ch;
  2611. case AV_CODEC_ID_INTERPLAY_DPCM:
  2612. return (frame_bytes - 6 - ch) / ch;
  2613. case AV_CODEC_ID_ROQ_DPCM:
  2614. return (frame_bytes - 8) / ch;
  2615. case AV_CODEC_ID_XAN_DPCM:
  2616. return (frame_bytes - 2 * ch) / ch;
  2617. case AV_CODEC_ID_MACE3:
  2618. return 3 * frame_bytes / ch;
  2619. case AV_CODEC_ID_MACE6:
  2620. return 6 * frame_bytes / ch;
  2621. case AV_CODEC_ID_PCM_LXF:
  2622. return 2 * (frame_bytes / (5 * ch));
  2623. case AV_CODEC_ID_IAC:
  2624. case AV_CODEC_ID_IMC:
  2625. return 4 * frame_bytes / ch;
  2626. }
  2627. if (tag) {
  2628. /* calc from frame_bytes, channels, and codec_tag */
  2629. if (id == AV_CODEC_ID_SOL_DPCM) {
  2630. if (tag == 3)
  2631. return frame_bytes / ch;
  2632. else
  2633. return frame_bytes * 2 / ch;
  2634. }
  2635. }
  2636. if (ba > 0) {
  2637. /* calc from frame_bytes, channels, and block_align */
  2638. int blocks = frame_bytes / ba;
  2639. switch (avctx->codec_id) {
  2640. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2641. if (bps < 2 || bps > 5)
  2642. return 0;
  2643. return blocks * (1 + (ba - 4 * ch) / (bps * ch) * 8);
  2644. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2645. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2646. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2647. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2648. case AV_CODEC_ID_ADPCM_IMA_RAD:
  2649. return blocks * ((ba - 4 * ch) * 2 / ch);
  2650. case AV_CODEC_ID_ADPCM_MS:
  2651. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2652. }
  2653. }
  2654. if (bps > 0) {
  2655. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2656. switch (avctx->codec_id) {
  2657. case AV_CODEC_ID_PCM_DVD:
  2658. if(bps<4)
  2659. return 0;
  2660. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2661. case AV_CODEC_ID_PCM_BLURAY:
  2662. if(bps<4)
  2663. return 0;
  2664. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2665. case AV_CODEC_ID_S302M:
  2666. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2667. }
  2668. }
  2669. }
  2670. }
  2671. return 0;
  2672. }
  2673. #if !HAVE_THREADS
  2674. int ff_thread_init(AVCodecContext *s)
  2675. {
  2676. return -1;
  2677. }
  2678. #endif
  2679. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2680. {
  2681. unsigned int n = 0;
  2682. while (v >= 0xff) {
  2683. *s++ = 0xff;
  2684. v -= 0xff;
  2685. n++;
  2686. }
  2687. *s = v;
  2688. n++;
  2689. return n;
  2690. }
  2691. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2692. {
  2693. int i;
  2694. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2695. return i;
  2696. }
  2697. #if FF_API_MISSING_SAMPLE
  2698. FF_DISABLE_DEPRECATION_WARNINGS
  2699. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2700. {
  2701. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2702. "version to the newest one from Git. If the problem still "
  2703. "occurs, it means that your file has a feature which has not "
  2704. "been implemented.\n", feature);
  2705. if(want_sample)
  2706. av_log_ask_for_sample(avc, NULL);
  2707. }
  2708. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2709. {
  2710. va_list argument_list;
  2711. va_start(argument_list, msg);
  2712. if (msg)
  2713. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2714. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2715. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  2716. "and contact the ffmpeg-devel mailing list.\n");
  2717. va_end(argument_list);
  2718. }
  2719. FF_ENABLE_DEPRECATION_WARNINGS
  2720. #endif /* FF_API_MISSING_SAMPLE */
  2721. static AVHWAccel *first_hwaccel = NULL;
  2722. void av_register_hwaccel(AVHWAccel *hwaccel)
  2723. {
  2724. AVHWAccel **p = &first_hwaccel;
  2725. hwaccel->next = NULL;
  2726. while(avpriv_atomic_ptr_cas((void * volatile *)p, NULL, hwaccel))
  2727. p = &(*p)->next;
  2728. }
  2729. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2730. {
  2731. return hwaccel ? hwaccel->next : first_hwaccel;
  2732. }
  2733. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  2734. {
  2735. AVHWAccel *hwaccel = NULL;
  2736. while ((hwaccel = av_hwaccel_next(hwaccel)))
  2737. if (hwaccel->id == codec_id
  2738. && hwaccel->pix_fmt == pix_fmt)
  2739. return hwaccel;
  2740. return NULL;
  2741. }
  2742. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2743. {
  2744. if (lockmgr_cb) {
  2745. if (lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2746. return -1;
  2747. if (lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2748. return -1;
  2749. }
  2750. lockmgr_cb = cb;
  2751. if (lockmgr_cb) {
  2752. if (lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2753. return -1;
  2754. if (lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2755. return -1;
  2756. }
  2757. return 0;
  2758. }
  2759. int ff_lock_avcodec(AVCodecContext *log_ctx)
  2760. {
  2761. if (lockmgr_cb) {
  2762. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  2763. return -1;
  2764. }
  2765. entangled_thread_counter++;
  2766. if (entangled_thread_counter != 1) {
  2767. av_log(log_ctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
  2768. if (!lockmgr_cb)
  2769. av_log(log_ctx, AV_LOG_ERROR, "No lock manager is set, please see av_lockmgr_register()\n");
  2770. ff_avcodec_locked = 1;
  2771. ff_unlock_avcodec();
  2772. return AVERROR(EINVAL);
  2773. }
  2774. av_assert0(!ff_avcodec_locked);
  2775. ff_avcodec_locked = 1;
  2776. return 0;
  2777. }
  2778. int ff_unlock_avcodec(void)
  2779. {
  2780. av_assert0(ff_avcodec_locked);
  2781. ff_avcodec_locked = 0;
  2782. entangled_thread_counter--;
  2783. if (lockmgr_cb) {
  2784. if ((*lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE))
  2785. return -1;
  2786. }
  2787. return 0;
  2788. }
  2789. int avpriv_lock_avformat(void)
  2790. {
  2791. if (lockmgr_cb) {
  2792. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2793. return -1;
  2794. }
  2795. return 0;
  2796. }
  2797. int avpriv_unlock_avformat(void)
  2798. {
  2799. if (lockmgr_cb) {
  2800. if ((*lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2801. return -1;
  2802. }
  2803. return 0;
  2804. }
  2805. unsigned int avpriv_toupper4(unsigned int x)
  2806. {
  2807. return av_toupper(x & 0xFF) +
  2808. (av_toupper((x >> 8) & 0xFF) << 8) +
  2809. (av_toupper((x >> 16) & 0xFF) << 16) +
  2810. (av_toupper((x >> 24) & 0xFF) << 24);
  2811. }
  2812. int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
  2813. {
  2814. int ret;
  2815. dst->owner = src->owner;
  2816. ret = av_frame_ref(dst->f, src->f);
  2817. if (ret < 0)
  2818. return ret;
  2819. if (src->progress &&
  2820. !(dst->progress = av_buffer_ref(src->progress))) {
  2821. ff_thread_release_buffer(dst->owner, dst);
  2822. return AVERROR(ENOMEM);
  2823. }
  2824. return 0;
  2825. }
  2826. #if !HAVE_THREADS
  2827. enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
  2828. {
  2829. return avctx->get_format(avctx, fmt);
  2830. }
  2831. int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
  2832. {
  2833. f->owner = avctx;
  2834. return ff_get_buffer(avctx, f->f, flags);
  2835. }
  2836. void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
  2837. {
  2838. av_frame_unref(f->f);
  2839. }
  2840. void ff_thread_finish_setup(AVCodecContext *avctx)
  2841. {
  2842. }
  2843. void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
  2844. {
  2845. }
  2846. void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
  2847. {
  2848. }
  2849. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2850. {
  2851. return 1;
  2852. }
  2853. #endif
  2854. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  2855. {
  2856. AVCodec *c= avcodec_find_decoder(codec_id);
  2857. if(!c)
  2858. c= avcodec_find_encoder(codec_id);
  2859. if(c)
  2860. return c->type;
  2861. if (codec_id <= AV_CODEC_ID_NONE)
  2862. return AVMEDIA_TYPE_UNKNOWN;
  2863. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  2864. return AVMEDIA_TYPE_VIDEO;
  2865. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  2866. return AVMEDIA_TYPE_AUDIO;
  2867. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  2868. return AVMEDIA_TYPE_SUBTITLE;
  2869. return AVMEDIA_TYPE_UNKNOWN;
  2870. }
  2871. int avcodec_is_open(AVCodecContext *s)
  2872. {
  2873. return !!s->internal;
  2874. }
  2875. int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf)
  2876. {
  2877. int ret;
  2878. char *str;
  2879. ret = av_bprint_finalize(buf, &str);
  2880. if (ret < 0)
  2881. return ret;
  2882. avctx->extradata = str;
  2883. /* Note: the string is NUL terminated (so extradata can be read as a
  2884. * string), but the ending character is not accounted in the size (in
  2885. * binary formats you are likely not supposed to mux that character). When
  2886. * extradata is copied, it is also padded with FF_INPUT_BUFFER_PADDING_SIZE
  2887. * zeros. */
  2888. avctx->extradata_size = buf->len;
  2889. return 0;
  2890. }
  2891. const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
  2892. const uint8_t *end,
  2893. uint32_t *av_restrict state)
  2894. {
  2895. int i;
  2896. av_assert0(p <= end);
  2897. if (p >= end)
  2898. return end;
  2899. for (i = 0; i < 3; i++) {
  2900. uint32_t tmp = *state << 8;
  2901. *state = tmp + *(p++);
  2902. if (tmp == 0x100 || p == end)
  2903. return p;
  2904. }
  2905. while (p < end) {
  2906. if (p[-1] > 1 ) p += 3;
  2907. else if (p[-2] ) p += 2;
  2908. else if (p[-3]|(p[-1]-1)) p++;
  2909. else {
  2910. p++;
  2911. break;
  2912. }
  2913. }
  2914. p = FFMIN(p, end) - 4;
  2915. *state = AV_RB32(p);
  2916. return p + 4;
  2917. }