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.

3463 lines
112KB

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