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.

3505 lines
114KB

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