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.

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