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.

3618 lines
118KB

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