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.

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