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.

3328 lines
108KB

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