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.

3654 lines
119KB

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