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.

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