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.

3663 lines
120KB

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