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.

2714 lines
87KB

  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 "libavutil/avassert.h"
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/channel_layout.h"
  29. #include "libavutil/crc.h"
  30. #include "libavutil/mathematics.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "libavutil/imgutils.h"
  33. #include "libavutil/samplefmt.h"
  34. #include "libavutil/dict.h"
  35. #include "libavutil/avassert.h"
  36. #include "avcodec.h"
  37. #include "dsputil.h"
  38. #include "libavutil/opt.h"
  39. #include "thread.h"
  40. #include "frame_thread_encoder.h"
  41. #include "internal.h"
  42. #include "bytestream.h"
  43. #include <stdlib.h>
  44. #include <stdarg.h>
  45. #include <limits.h>
  46. #include <float.h>
  47. static int volatile entangled_thread_counter = 0;
  48. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  49. static void *codec_mutex;
  50. static void *avformat_mutex;
  51. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  52. {
  53. if (min_size < *size)
  54. return ptr;
  55. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  56. ptr = av_realloc(ptr, min_size);
  57. /* we could set this to the unmodified min_size but this is safer
  58. * if the user lost the ptr and uses NULL now
  59. */
  60. if (!ptr)
  61. min_size = 0;
  62. *size = min_size;
  63. return ptr;
  64. }
  65. static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
  66. {
  67. void **p = ptr;
  68. if (min_size < *size)
  69. return 0;
  70. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  71. av_free(*p);
  72. *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
  73. if (!*p)
  74. min_size = 0;
  75. *size = min_size;
  76. return 1;
  77. }
  78. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  79. {
  80. ff_fast_malloc(ptr, size, min_size, 0);
  81. }
  82. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  83. {
  84. uint8_t **p = ptr;
  85. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  86. av_freep(p);
  87. *size = 0;
  88. return;
  89. }
  90. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  91. memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  92. }
  93. void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size)
  94. {
  95. uint8_t **p = ptr;
  96. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  97. av_freep(p);
  98. *size = 0;
  99. return;
  100. }
  101. if (!ff_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1))
  102. memset(*p, 0, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  103. }
  104. /* encoder management */
  105. static AVCodec *first_avcodec = NULL;
  106. AVCodec *av_codec_next(const AVCodec *c)
  107. {
  108. if (c)
  109. return c->next;
  110. else
  111. return first_avcodec;
  112. }
  113. static void avcodec_init(void)
  114. {
  115. static int initialized = 0;
  116. if (initialized != 0)
  117. return;
  118. initialized = 1;
  119. ff_dsputil_static_init();
  120. }
  121. int av_codec_is_encoder(const AVCodec *codec)
  122. {
  123. return codec && (codec->encode_sub || codec->encode2);
  124. }
  125. int av_codec_is_decoder(const AVCodec *codec)
  126. {
  127. return codec && codec->decode;
  128. }
  129. void avcodec_register(AVCodec *codec)
  130. {
  131. AVCodec **p;
  132. avcodec_init();
  133. p = &first_avcodec;
  134. while (*p != NULL)
  135. p = &(*p)->next;
  136. *p = codec;
  137. codec->next = NULL;
  138. if (codec->init_static_data)
  139. codec->init_static_data(codec);
  140. }
  141. unsigned avcodec_get_edge_width(void)
  142. {
  143. return EDGE_WIDTH;
  144. }
  145. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  146. {
  147. s->coded_width = width;
  148. s->coded_height = height;
  149. s->width = -((-width ) >> s->lowres);
  150. s->height = -((-height) >> s->lowres);
  151. }
  152. #define INTERNAL_BUFFER_SIZE (32 + 1)
  153. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  154. int linesize_align[AV_NUM_DATA_POINTERS])
  155. {
  156. int i;
  157. int w_align = 1;
  158. int h_align = 1;
  159. switch (s->pix_fmt) {
  160. case AV_PIX_FMT_YUV420P:
  161. case AV_PIX_FMT_YUYV422:
  162. case AV_PIX_FMT_UYVY422:
  163. case AV_PIX_FMT_YUV422P:
  164. case AV_PIX_FMT_YUV440P:
  165. case AV_PIX_FMT_YUV444P:
  166. case AV_PIX_FMT_GBRP:
  167. case AV_PIX_FMT_GRAY8:
  168. case AV_PIX_FMT_GRAY16BE:
  169. case AV_PIX_FMT_GRAY16LE:
  170. case AV_PIX_FMT_YUVJ420P:
  171. case AV_PIX_FMT_YUVJ422P:
  172. case AV_PIX_FMT_YUVJ440P:
  173. case AV_PIX_FMT_YUVJ444P:
  174. case AV_PIX_FMT_YUVA420P:
  175. case AV_PIX_FMT_YUVA422P:
  176. case AV_PIX_FMT_YUVA444P:
  177. case AV_PIX_FMT_YUV420P9LE:
  178. case AV_PIX_FMT_YUV420P9BE:
  179. case AV_PIX_FMT_YUV420P10LE:
  180. case AV_PIX_FMT_YUV420P10BE:
  181. case AV_PIX_FMT_YUV420P12LE:
  182. case AV_PIX_FMT_YUV420P12BE:
  183. case AV_PIX_FMT_YUV420P14LE:
  184. case AV_PIX_FMT_YUV420P14BE:
  185. case AV_PIX_FMT_YUV422P9LE:
  186. case AV_PIX_FMT_YUV422P9BE:
  187. case AV_PIX_FMT_YUV422P10LE:
  188. case AV_PIX_FMT_YUV422P10BE:
  189. case AV_PIX_FMT_YUV422P12LE:
  190. case AV_PIX_FMT_YUV422P12BE:
  191. case AV_PIX_FMT_YUV422P14LE:
  192. case AV_PIX_FMT_YUV422P14BE:
  193. case AV_PIX_FMT_YUV444P9LE:
  194. case AV_PIX_FMT_YUV444P9BE:
  195. case AV_PIX_FMT_YUV444P10LE:
  196. case AV_PIX_FMT_YUV444P10BE:
  197. case AV_PIX_FMT_YUV444P12LE:
  198. case AV_PIX_FMT_YUV444P12BE:
  199. case AV_PIX_FMT_YUV444P14LE:
  200. case AV_PIX_FMT_YUV444P14BE:
  201. case AV_PIX_FMT_GBRP9LE:
  202. case AV_PIX_FMT_GBRP9BE:
  203. case AV_PIX_FMT_GBRP10LE:
  204. case AV_PIX_FMT_GBRP10BE:
  205. case AV_PIX_FMT_GBRP12LE:
  206. case AV_PIX_FMT_GBRP12BE:
  207. case AV_PIX_FMT_GBRP14LE:
  208. case AV_PIX_FMT_GBRP14BE:
  209. w_align = 16; //FIXME assume 16 pixel per macroblock
  210. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  211. break;
  212. case AV_PIX_FMT_YUV411P:
  213. case AV_PIX_FMT_UYYVYY411:
  214. w_align = 32;
  215. h_align = 8;
  216. break;
  217. case AV_PIX_FMT_YUV410P:
  218. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  219. w_align = 64;
  220. h_align = 64;
  221. }
  222. break;
  223. case AV_PIX_FMT_RGB555:
  224. if (s->codec_id == AV_CODEC_ID_RPZA) {
  225. w_align = 4;
  226. h_align = 4;
  227. }
  228. break;
  229. case AV_PIX_FMT_PAL8:
  230. case AV_PIX_FMT_BGR8:
  231. case AV_PIX_FMT_RGB8:
  232. if (s->codec_id == AV_CODEC_ID_SMC) {
  233. w_align = 4;
  234. h_align = 4;
  235. }
  236. break;
  237. case AV_PIX_FMT_BGR24:
  238. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  239. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  240. w_align = 4;
  241. h_align = 4;
  242. }
  243. break;
  244. default:
  245. w_align = 1;
  246. h_align = 1;
  247. break;
  248. }
  249. if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
  250. w_align = FFMAX(w_align, 8);
  251. }
  252. *width = FFALIGN(*width, w_align);
  253. *height = FFALIGN(*height, h_align);
  254. if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
  255. // some of the optimized chroma MC reads one line too much
  256. // which is also done in mpeg decoders with lowres > 0
  257. *height += 2;
  258. for (i = 0; i < 4; i++)
  259. linesize_align[i] = STRIDE_ALIGN;
  260. }
  261. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  262. {
  263. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  264. int chroma_shift = desc->log2_chroma_w;
  265. int linesize_align[AV_NUM_DATA_POINTERS];
  266. int align;
  267. avcodec_align_dimensions2(s, width, height, linesize_align);
  268. align = FFMAX(linesize_align[0], linesize_align[3]);
  269. linesize_align[1] <<= chroma_shift;
  270. linesize_align[2] <<= chroma_shift;
  271. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  272. *width = FFALIGN(*width, align);
  273. }
  274. void ff_init_buffer_info(AVCodecContext *s, AVFrame *frame)
  275. {
  276. if (s->pkt) {
  277. frame->pkt_pts = s->pkt->pts;
  278. frame->pkt_pos = s->pkt->pos;
  279. frame->pkt_duration = s->pkt->duration;
  280. } else {
  281. frame->pkt_pts = AV_NOPTS_VALUE;
  282. frame->pkt_pos = -1;
  283. frame->pkt_duration = 0;
  284. }
  285. frame->reordered_opaque = s->reordered_opaque;
  286. switch (s->codec->type) {
  287. case AVMEDIA_TYPE_VIDEO:
  288. frame->width = s->width;
  289. frame->height = s->height;
  290. frame->format = s->pix_fmt;
  291. frame->sample_aspect_ratio = s->sample_aspect_ratio;
  292. break;
  293. case AVMEDIA_TYPE_AUDIO:
  294. frame->sample_rate = s->sample_rate;
  295. frame->format = s->sample_fmt;
  296. frame->channel_layout = s->channel_layout;
  297. frame->channels = s->channels;
  298. break;
  299. }
  300. }
  301. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  302. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  303. int buf_size, int align)
  304. {
  305. int ch, planar, needed_size, ret = 0;
  306. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  307. frame->nb_samples, sample_fmt,
  308. align);
  309. if (buf_size < needed_size)
  310. return AVERROR(EINVAL);
  311. planar = av_sample_fmt_is_planar(sample_fmt);
  312. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  313. if (!(frame->extended_data = av_mallocz(nb_channels *
  314. sizeof(*frame->extended_data))))
  315. return AVERROR(ENOMEM);
  316. } else {
  317. frame->extended_data = frame->data;
  318. }
  319. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  320. (uint8_t *)(intptr_t)buf, nb_channels, frame->nb_samples,
  321. sample_fmt, align)) < 0) {
  322. if (frame->extended_data != frame->data)
  323. av_freep(&frame->extended_data);
  324. return ret;
  325. }
  326. if (frame->extended_data != frame->data) {
  327. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  328. frame->data[ch] = frame->extended_data[ch];
  329. }
  330. return ret;
  331. }
  332. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  333. {
  334. AVCodecInternal *avci = avctx->internal;
  335. InternalBuffer *buf;
  336. int buf_size, ret;
  337. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  338. frame->nb_samples, avctx->sample_fmt,
  339. 0);
  340. if (buf_size < 0)
  341. return AVERROR(EINVAL);
  342. /* allocate InternalBuffer if needed */
  343. if (!avci->buffer) {
  344. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  345. if (!avci->buffer)
  346. return AVERROR(ENOMEM);
  347. }
  348. buf = avci->buffer;
  349. /* if there is a previously-used internal buffer, check its size and
  350. * channel count to see if we can reuse it */
  351. if (buf->extended_data) {
  352. /* if current buffer is too small, free it */
  353. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  354. av_free(buf->extended_data[0]);
  355. if (buf->extended_data != buf->data)
  356. av_free(buf->extended_data);
  357. buf->extended_data = NULL;
  358. buf->data[0] = NULL;
  359. }
  360. /* if number of channels has changed, reset and/or free extended data
  361. * pointers but leave data buffer in buf->data[0] for reuse */
  362. if (buf->nb_channels != avctx->channels) {
  363. if (buf->extended_data != buf->data)
  364. av_free(buf->extended_data);
  365. buf->extended_data = NULL;
  366. }
  367. }
  368. /* if there is no previous buffer or the previous buffer cannot be used
  369. * as-is, allocate a new buffer and/or rearrange the channel pointers */
  370. if (!buf->extended_data) {
  371. if (!buf->data[0]) {
  372. if (!(buf->data[0] = av_mallocz(buf_size)))
  373. return AVERROR(ENOMEM);
  374. buf->audio_data_size = buf_size;
  375. }
  376. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  377. avctx->sample_fmt, buf->data[0],
  378. buf->audio_data_size, 0)))
  379. return ret;
  380. if (frame->extended_data == frame->data)
  381. buf->extended_data = buf->data;
  382. else
  383. buf->extended_data = frame->extended_data;
  384. memcpy(buf->data, frame->data, sizeof(frame->data));
  385. buf->linesize[0] = frame->linesize[0];
  386. buf->nb_channels = avctx->channels;
  387. } else {
  388. /* copy InternalBuffer info to the AVFrame */
  389. frame->extended_data = buf->extended_data;
  390. frame->linesize[0] = buf->linesize[0];
  391. memcpy(frame->data, buf->data, sizeof(frame->data));
  392. }
  393. frame->type = FF_BUFFER_TYPE_INTERNAL;
  394. ff_init_buffer_info(avctx, frame);
  395. if (avctx->debug & FF_DEBUG_BUFFERS)
  396. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  397. "internal audio buffer used\n", frame);
  398. return 0;
  399. }
  400. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  401. {
  402. int i;
  403. int w = s->width;
  404. int h = s->height;
  405. InternalBuffer *buf;
  406. AVCodecInternal *avci = s->internal;
  407. if (pic->data[0] != NULL) {
  408. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  409. return -1;
  410. }
  411. if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  412. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  413. return -1;
  414. }
  415. if (av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
  416. av_log(s, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
  417. return -1;
  418. }
  419. if (!avci->buffer) {
  420. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
  421. sizeof(InternalBuffer));
  422. }
  423. buf = &avci->buffer[avci->buffer_count];
  424. if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
  425. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  426. av_freep(&buf->base[i]);
  427. buf->data[i] = NULL;
  428. }
  429. }
  430. if (!buf->base[0]) {
  431. int h_chroma_shift, v_chroma_shift;
  432. int size[4] = { 0 };
  433. int tmpsize;
  434. int unaligned;
  435. AVPicture picture;
  436. int stride_align[AV_NUM_DATA_POINTERS];
  437. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  438. const int pixel_size = desc->comp[0].step_minus1 + 1;
  439. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  440. avcodec_align_dimensions2(s, &w, &h, stride_align);
  441. if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
  442. w += EDGE_WIDTH * 2;
  443. h += EDGE_WIDTH * 2;
  444. }
  445. do {
  446. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  447. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  448. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  449. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  450. w += w & ~(w - 1);
  451. unaligned = 0;
  452. for (i = 0; i < 4; i++)
  453. unaligned |= picture.linesize[i] % stride_align[i];
  454. } while (unaligned);
  455. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  456. if (tmpsize < 0)
  457. return -1;
  458. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  459. size[i] = picture.data[i + 1] - picture.data[i];
  460. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  461. memset(buf->base, 0, sizeof(buf->base));
  462. memset(buf->data, 0, sizeof(buf->data));
  463. for (i = 0; i < 4 && size[i]; i++) {
  464. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  465. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  466. buf->linesize[i] = picture.linesize[i];
  467. buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
  468. if (buf->base[i] == NULL)
  469. return AVERROR(ENOMEM);
  470. memset(buf->base[i], 128, size[i]);
  471. // no edge if EDGE EMU or not planar YUV
  472. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
  473. buf->data[i] = buf->base[i];
  474. else
  475. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
  476. }
  477. for (; i < AV_NUM_DATA_POINTERS; i++) {
  478. buf->base[i] = buf->data[i] = NULL;
  479. buf->linesize[i] = 0;
  480. }
  481. if (size[1] && !size[2])
  482. avpriv_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
  483. buf->width = s->width;
  484. buf->height = s->height;
  485. buf->pix_fmt = s->pix_fmt;
  486. }
  487. pic->type = FF_BUFFER_TYPE_INTERNAL;
  488. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  489. pic->base[i] = buf->base[i];
  490. pic->data[i] = buf->data[i];
  491. pic->linesize[i] = buf->linesize[i];
  492. }
  493. pic->extended_data = pic->data;
  494. avci->buffer_count++;
  495. pic->width = buf->width;
  496. pic->height = buf->height;
  497. pic->format = buf->pix_fmt;
  498. ff_init_buffer_info(s, pic);
  499. if (s->debug & FF_DEBUG_BUFFERS)
  500. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  501. "buffers used\n", pic, avci->buffer_count);
  502. return 0;
  503. }
  504. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  505. {
  506. switch (avctx->codec_type) {
  507. case AVMEDIA_TYPE_VIDEO:
  508. return video_get_buffer(avctx, frame);
  509. case AVMEDIA_TYPE_AUDIO:
  510. return audio_get_buffer(avctx, frame);
  511. default:
  512. return -1;
  513. }
  514. }
  515. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  516. {
  517. int i;
  518. InternalBuffer *buf, *last;
  519. AVCodecInternal *avci = s->internal;
  520. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  521. assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
  522. assert(avci->buffer_count);
  523. if (avci->buffer) {
  524. buf = NULL; /* avoids warning */
  525. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  526. buf = &avci->buffer[i];
  527. if (buf->data[0] == pic->data[0])
  528. break;
  529. }
  530. av_assert0(i < avci->buffer_count);
  531. avci->buffer_count--;
  532. last = &avci->buffer[avci->buffer_count];
  533. if (buf != last)
  534. FFSWAP(InternalBuffer, *buf, *last);
  535. }
  536. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  537. pic->data[i] = NULL;
  538. // pic->base[i]=NULL;
  539. if (s->debug & FF_DEBUG_BUFFERS)
  540. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  541. "buffers used\n", pic, avci->buffer_count);
  542. }
  543. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  544. {
  545. AVFrame temp_pic;
  546. int i, ret;
  547. av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  548. if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
  549. av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
  550. pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
  551. s->release_buffer(s, pic);
  552. }
  553. ff_init_buffer_info(s, pic);
  554. /* If no picture return a new buffer */
  555. if (pic->data[0] == NULL) {
  556. /* We will copy from buffer, so must be readable */
  557. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  558. return s->get_buffer(s, pic);
  559. }
  560. assert(s->pix_fmt == pic->format);
  561. /* If internal buffer type return the same buffer */
  562. if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
  563. return 0;
  564. }
  565. /*
  566. * Not internal type and reget_buffer not overridden, emulate cr buffer
  567. */
  568. temp_pic = *pic;
  569. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  570. pic->data[i] = pic->base[i] = NULL;
  571. pic->opaque = NULL;
  572. /* Allocate new frame */
  573. if ((ret = s->get_buffer(s, pic)))
  574. return ret;
  575. /* Copy image data from old buffer to new buffer */
  576. av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
  577. s->height);
  578. s->release_buffer(s, &temp_pic); // Release old frame
  579. return 0;
  580. }
  581. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  582. {
  583. int i;
  584. for (i = 0; i < count; i++) {
  585. int r = func(c, (char *)arg + i * size);
  586. if (ret)
  587. ret[i] = r;
  588. }
  589. return 0;
  590. }
  591. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  592. {
  593. int i;
  594. for (i = 0; i < count; i++) {
  595. int r = func(c, arg, i, 0);
  596. if (ret)
  597. ret[i] = r;
  598. }
  599. return 0;
  600. }
  601. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  602. {
  603. while (*fmt != AV_PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  604. ++fmt;
  605. return fmt[0];
  606. }
  607. void avcodec_get_frame_defaults(AVFrame *frame)
  608. {
  609. #if LIBAVCODEC_VERSION_MAJOR >= 55
  610. // extended_data should explicitly be freed when needed, this code is unsafe currently
  611. // also this is not compatible to the <55 ABI/API
  612. if (frame->extended_data != frame->data && 0)
  613. av_freep(&frame->extended_data);
  614. #endif
  615. memset(frame, 0, sizeof(AVFrame));
  616. frame->pts =
  617. frame->pkt_dts =
  618. frame->pkt_pts =
  619. frame->best_effort_timestamp = AV_NOPTS_VALUE;
  620. frame->pkt_duration = 0;
  621. frame->pkt_pos = -1;
  622. frame->key_frame = 1;
  623. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  624. frame->format = -1; /* unknown */
  625. frame->extended_data = frame->data;
  626. }
  627. AVFrame *avcodec_alloc_frame(void)
  628. {
  629. AVFrame *frame = av_malloc(sizeof(AVFrame));
  630. if (frame == NULL)
  631. return NULL;
  632. frame->extended_data = NULL;
  633. avcodec_get_frame_defaults(frame);
  634. return frame;
  635. }
  636. void avcodec_free_frame(AVFrame **frame)
  637. {
  638. AVFrame *f;
  639. if (!frame || !*frame)
  640. return;
  641. f = *frame;
  642. if (f->extended_data != f->data)
  643. av_freep(&f->extended_data);
  644. av_freep(frame);
  645. }
  646. #define MAKE_ACCESSORS(str, name, type, field) \
  647. type av_##name##_get_##field(const str *s) { return s->field; } \
  648. void av_##name##_set_##field(str *s, type v) { s->field = v; }
  649. MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
  650. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
  651. MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
  652. MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
  653. MAKE_ACCESSORS(AVFrame, frame, int, channels)
  654. MAKE_ACCESSORS(AVFrame, frame, int, sample_rate)
  655. MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
  656. MAKE_ACCESSORS(AVFrame, frame, int, decode_error_flags)
  657. MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
  658. MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
  659. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  660. {
  661. memset(sub, 0, sizeof(*sub));
  662. sub->pts = AV_NOPTS_VALUE;
  663. }
  664. static int get_bit_rate(AVCodecContext *ctx)
  665. {
  666. int bit_rate;
  667. int bits_per_sample;
  668. switch (ctx->codec_type) {
  669. case AVMEDIA_TYPE_VIDEO:
  670. case AVMEDIA_TYPE_DATA:
  671. case AVMEDIA_TYPE_SUBTITLE:
  672. case AVMEDIA_TYPE_ATTACHMENT:
  673. bit_rate = ctx->bit_rate;
  674. break;
  675. case AVMEDIA_TYPE_AUDIO:
  676. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  677. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  678. break;
  679. default:
  680. bit_rate = 0;
  681. break;
  682. }
  683. return bit_rate;
  684. }
  685. #if FF_API_AVCODEC_OPEN
  686. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  687. {
  688. return avcodec_open2(avctx, codec, NULL);
  689. }
  690. #endif
  691. int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  692. {
  693. int ret = 0;
  694. entangled_thread_counter--;
  695. /* Release any user-supplied mutex. */
  696. if (ff_lockmgr_cb) {
  697. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  698. }
  699. ret = avcodec_open2(avctx, codec, options);
  700. /* If there is a user-supplied mutex locking routine, call it. */
  701. if (ff_lockmgr_cb) {
  702. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  703. return -1;
  704. }
  705. entangled_thread_counter++;
  706. return ret;
  707. }
  708. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  709. {
  710. int ret = 0;
  711. AVDictionary *tmp = NULL;
  712. if (avcodec_is_open(avctx))
  713. return 0;
  714. if ((!codec && !avctx->codec)) {
  715. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2()\n");
  716. return AVERROR(EINVAL);
  717. }
  718. if ((codec && avctx->codec && codec != avctx->codec)) {
  719. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  720. "but %s passed to avcodec_open2()\n", avctx->codec->name, codec->name);
  721. return AVERROR(EINVAL);
  722. }
  723. if (!codec)
  724. codec = avctx->codec;
  725. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  726. return AVERROR(EINVAL);
  727. if (options)
  728. av_dict_copy(&tmp, *options, 0);
  729. /* If there is a user-supplied mutex locking routine, call it. */
  730. if (ff_lockmgr_cb) {
  731. if ((ret = (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN)) < 0)
  732. return ret;
  733. }
  734. entangled_thread_counter++;
  735. if (entangled_thread_counter != 1) {
  736. av_log(avctx, AV_LOG_ERROR, "Insufficient thread locking around avcodec_open/close()\n");
  737. ret = AVERROR(EINVAL);
  738. goto end;
  739. }
  740. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  741. if (!avctx->internal) {
  742. ret = AVERROR(ENOMEM);
  743. goto end;
  744. }
  745. if (codec->priv_data_size > 0) {
  746. if (!avctx->priv_data) {
  747. avctx->priv_data = av_mallocz(codec->priv_data_size);
  748. if (!avctx->priv_data) {
  749. ret = AVERROR(ENOMEM);
  750. goto end;
  751. }
  752. if (codec->priv_class) {
  753. *(const AVClass **)avctx->priv_data = codec->priv_class;
  754. av_opt_set_defaults(avctx->priv_data);
  755. }
  756. }
  757. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  758. goto free_and_end;
  759. } else {
  760. avctx->priv_data = NULL;
  761. }
  762. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  763. goto free_and_end;
  764. //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
  765. if (!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
  766. if (avctx->coded_width && avctx->coded_height)
  767. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  768. else if (avctx->width && avctx->height)
  769. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  770. }
  771. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  772. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  773. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  774. av_log(avctx, AV_LOG_WARNING, "Ignoring invalid width/height values\n");
  775. avcodec_set_dimensions(avctx, 0, 0);
  776. }
  777. /* if the decoder init function was already called previously,
  778. * free the already allocated subtitle_header before overwriting it */
  779. if (av_codec_is_decoder(codec))
  780. av_freep(&avctx->subtitle_header);
  781. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  782. ret = AVERROR(EINVAL);
  783. goto free_and_end;
  784. }
  785. avctx->codec = codec;
  786. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  787. avctx->codec_id == AV_CODEC_ID_NONE) {
  788. avctx->codec_type = codec->type;
  789. avctx->codec_id = codec->id;
  790. }
  791. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  792. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  793. av_log(avctx, AV_LOG_ERROR, "Codec type or id mismatches\n");
  794. ret = AVERROR(EINVAL);
  795. goto free_and_end;
  796. }
  797. avctx->frame_number = 0;
  798. avctx->codec_descriptor = avcodec_descriptor_get(avctx->codec_id);
  799. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  800. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  801. const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
  802. AVCodec *codec2;
  803. av_log(NULL, AV_LOG_ERROR,
  804. "The %s '%s' is experimental but experimental codecs are not enabled, "
  805. "add '-strict %d' if you want to use it.\n",
  806. codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
  807. codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
  808. if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
  809. av_log(NULL, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
  810. codec_string, codec2->name);
  811. ret = AVERROR_EXPERIMENTAL;
  812. goto free_and_end;
  813. }
  814. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  815. (!avctx->time_base.num || !avctx->time_base.den)) {
  816. avctx->time_base.num = 1;
  817. avctx->time_base.den = avctx->sample_rate;
  818. }
  819. if (!HAVE_THREADS)
  820. av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
  821. if (HAVE_THREADS) {
  822. entangled_thread_counter--; //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
  823. ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
  824. entangled_thread_counter++;
  825. if (ret < 0)
  826. goto free_and_end;
  827. }
  828. if (HAVE_THREADS && !avctx->thread_opaque
  829. && !(avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))) {
  830. ret = ff_thread_init(avctx);
  831. if (ret < 0) {
  832. goto free_and_end;
  833. }
  834. }
  835. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  836. avctx->thread_count = 1;
  837. if (avctx->codec->max_lowres < avctx->lowres || avctx->lowres < 0) {
  838. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  839. avctx->codec->max_lowres);
  840. ret = AVERROR(EINVAL);
  841. goto free_and_end;
  842. }
  843. if (av_codec_is_encoder(avctx->codec)) {
  844. int i;
  845. if (avctx->codec->sample_fmts) {
  846. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  847. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  848. break;
  849. if (avctx->channels == 1 &&
  850. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  851. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  852. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  853. break;
  854. }
  855. }
  856. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  857. char buf[128];
  858. snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
  859. av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
  860. (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
  861. ret = AVERROR(EINVAL);
  862. goto free_and_end;
  863. }
  864. }
  865. if (avctx->codec->pix_fmts) {
  866. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  867. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  868. break;
  869. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE
  870. && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
  871. && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
  872. char buf[128];
  873. snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
  874. av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
  875. (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
  876. ret = AVERROR(EINVAL);
  877. goto free_and_end;
  878. }
  879. }
  880. if (avctx->codec->supported_samplerates) {
  881. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  882. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  883. break;
  884. if (avctx->codec->supported_samplerates[i] == 0) {
  885. av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
  886. avctx->sample_rate);
  887. ret = AVERROR(EINVAL);
  888. goto free_and_end;
  889. }
  890. }
  891. if (avctx->codec->channel_layouts) {
  892. if (!avctx->channel_layout) {
  893. av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
  894. } else {
  895. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  896. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  897. break;
  898. if (avctx->codec->channel_layouts[i] == 0) {
  899. char buf[512];
  900. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  901. av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
  902. ret = AVERROR(EINVAL);
  903. goto free_and_end;
  904. }
  905. }
  906. }
  907. if (avctx->channel_layout && avctx->channels) {
  908. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  909. if (channels != avctx->channels) {
  910. char buf[512];
  911. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  912. av_log(avctx, AV_LOG_ERROR,
  913. "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
  914. buf, channels, avctx->channels);
  915. ret = AVERROR(EINVAL);
  916. goto free_and_end;
  917. }
  918. } else if (avctx->channel_layout) {
  919. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  920. }
  921. }
  922. avctx->pts_correction_num_faulty_pts =
  923. avctx->pts_correction_num_faulty_dts = 0;
  924. avctx->pts_correction_last_pts =
  925. avctx->pts_correction_last_dts = INT64_MIN;
  926. if ( avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
  927. || avctx->internal->frame_thread_encoder)) {
  928. ret = avctx->codec->init(avctx);
  929. if (ret < 0) {
  930. goto free_and_end;
  931. }
  932. }
  933. ret=0;
  934. if (av_codec_is_decoder(avctx->codec)) {
  935. if (!avctx->bit_rate)
  936. avctx->bit_rate = get_bit_rate(avctx);
  937. /* validate channel layout from the decoder */
  938. if (avctx->channel_layout) {
  939. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  940. if (!avctx->channels)
  941. avctx->channels = channels;
  942. else if (channels != avctx->channels) {
  943. char buf[512];
  944. av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
  945. av_log(avctx, AV_LOG_WARNING,
  946. "Channel layout '%s' with %d channels does not match specified number of channels %d: "
  947. "ignoring specified channel layout\n",
  948. buf, channels, avctx->channels);
  949. avctx->channel_layout = 0;
  950. }
  951. }
  952. if (avctx->channels && avctx->channels < 0 ||
  953. avctx->channels > FF_SANE_NB_CHANNELS) {
  954. ret = AVERROR(EINVAL);
  955. goto free_and_end;
  956. }
  957. }
  958. end:
  959. entangled_thread_counter--;
  960. /* Release any user-supplied mutex. */
  961. if (ff_lockmgr_cb) {
  962. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  963. }
  964. if (options) {
  965. av_dict_free(options);
  966. *options = tmp;
  967. }
  968. return ret;
  969. free_and_end:
  970. av_dict_free(&tmp);
  971. av_freep(&avctx->priv_data);
  972. av_freep(&avctx->internal);
  973. avctx->codec = NULL;
  974. goto end;
  975. }
  976. int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
  977. {
  978. if (size < 0 || avpkt->size < 0 || size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  979. av_log(avctx, AV_LOG_ERROR, "Size %d invalid\n", size);
  980. return AVERROR(EINVAL);
  981. }
  982. if (avctx) {
  983. av_assert0(!avpkt->data || avpkt->data != avctx->internal->byte_buffer);
  984. if (!avpkt->data || avpkt->size < size) {
  985. av_fast_padded_malloc(&avctx->internal->byte_buffer, &avctx->internal->byte_buffer_size, size);
  986. avpkt->data = avctx->internal->byte_buffer;
  987. avpkt->size = avctx->internal->byte_buffer_size;
  988. avpkt->destruct = NULL;
  989. }
  990. }
  991. if (avpkt->data) {
  992. void *destruct = avpkt->destruct;
  993. if (avpkt->size < size) {
  994. av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
  995. return AVERROR(EINVAL);
  996. }
  997. av_init_packet(avpkt);
  998. avpkt->destruct = destruct;
  999. avpkt->size = size;
  1000. return 0;
  1001. } else {
  1002. int ret = av_new_packet(avpkt, size);
  1003. if (ret < 0)
  1004. av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
  1005. return ret;
  1006. }
  1007. }
  1008. int ff_alloc_packet(AVPacket *avpkt, int size)
  1009. {
  1010. return ff_alloc_packet2(NULL, avpkt, size);
  1011. }
  1012. /**
  1013. * Pad last frame with silence.
  1014. */
  1015. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  1016. {
  1017. AVFrame *frame = NULL;
  1018. uint8_t *buf = NULL;
  1019. int ret;
  1020. if (!(frame = avcodec_alloc_frame()))
  1021. return AVERROR(ENOMEM);
  1022. *frame = *src;
  1023. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  1024. s->frame_size, s->sample_fmt, 0)) < 0)
  1025. goto fail;
  1026. if (!(buf = av_malloc(ret))) {
  1027. ret = AVERROR(ENOMEM);
  1028. goto fail;
  1029. }
  1030. frame->nb_samples = s->frame_size;
  1031. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  1032. buf, ret, 0)) < 0)
  1033. goto fail;
  1034. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  1035. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  1036. goto fail;
  1037. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  1038. frame->nb_samples - src->nb_samples,
  1039. s->channels, s->sample_fmt)) < 0)
  1040. goto fail;
  1041. *dst = frame;
  1042. return 0;
  1043. fail:
  1044. if (frame->extended_data != frame->data)
  1045. av_freep(&frame->extended_data);
  1046. av_freep(&buf);
  1047. av_freep(&frame);
  1048. return ret;
  1049. }
  1050. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  1051. AVPacket *avpkt,
  1052. const AVFrame *frame,
  1053. int *got_packet_ptr)
  1054. {
  1055. AVFrame tmp;
  1056. AVFrame *padded_frame = NULL;
  1057. int ret;
  1058. AVPacket user_pkt = *avpkt;
  1059. int needs_realloc = !user_pkt.data;
  1060. *got_packet_ptr = 0;
  1061. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1062. av_free_packet(avpkt);
  1063. av_init_packet(avpkt);
  1064. return 0;
  1065. }
  1066. /* ensure that extended_data is properly set */
  1067. if (frame && !frame->extended_data) {
  1068. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  1069. avctx->channels > AV_NUM_DATA_POINTERS) {
  1070. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  1071. "with more than %d channels, but extended_data is not set.\n",
  1072. AV_NUM_DATA_POINTERS);
  1073. return AVERROR(EINVAL);
  1074. }
  1075. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  1076. tmp = *frame;
  1077. tmp.extended_data = tmp.data;
  1078. frame = &tmp;
  1079. }
  1080. /* check for valid frame size */
  1081. if (frame) {
  1082. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  1083. if (frame->nb_samples > avctx->frame_size) {
  1084. av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
  1085. return AVERROR(EINVAL);
  1086. }
  1087. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  1088. if (frame->nb_samples < avctx->frame_size &&
  1089. !avctx->internal->last_audio_frame) {
  1090. ret = pad_last_frame(avctx, &padded_frame, frame);
  1091. if (ret < 0)
  1092. return ret;
  1093. frame = padded_frame;
  1094. avctx->internal->last_audio_frame = 1;
  1095. }
  1096. if (frame->nb_samples != avctx->frame_size) {
  1097. av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
  1098. ret = AVERROR(EINVAL);
  1099. goto end;
  1100. }
  1101. }
  1102. }
  1103. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1104. if (!ret) {
  1105. if (*got_packet_ptr) {
  1106. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  1107. if (avpkt->pts == AV_NOPTS_VALUE)
  1108. avpkt->pts = frame->pts;
  1109. if (!avpkt->duration)
  1110. avpkt->duration = ff_samples_to_time_base(avctx,
  1111. frame->nb_samples);
  1112. }
  1113. avpkt->dts = avpkt->pts;
  1114. } else {
  1115. avpkt->size = 0;
  1116. }
  1117. }
  1118. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1119. needs_realloc = 0;
  1120. if (user_pkt.data) {
  1121. if (user_pkt.size >= avpkt->size) {
  1122. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1123. } else {
  1124. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1125. avpkt->size = user_pkt.size;
  1126. ret = -1;
  1127. }
  1128. avpkt->data = user_pkt.data;
  1129. avpkt->destruct = user_pkt.destruct;
  1130. } else {
  1131. if (av_dup_packet(avpkt) < 0) {
  1132. ret = AVERROR(ENOMEM);
  1133. }
  1134. }
  1135. }
  1136. if (!ret) {
  1137. if (needs_realloc && avpkt->data) {
  1138. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1139. if (new_data)
  1140. avpkt->data = new_data;
  1141. }
  1142. avctx->frame_number++;
  1143. }
  1144. if (ret < 0 || !*got_packet_ptr) {
  1145. av_free_packet(avpkt);
  1146. av_init_packet(avpkt);
  1147. goto end;
  1148. }
  1149. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  1150. * this needs to be moved to the encoders, but for now we can do it
  1151. * here to simplify things */
  1152. avpkt->flags |= AV_PKT_FLAG_KEY;
  1153. end:
  1154. if (padded_frame) {
  1155. av_freep(&padded_frame->data[0]);
  1156. if (padded_frame->extended_data != padded_frame->data)
  1157. av_freep(&padded_frame->extended_data);
  1158. av_freep(&padded_frame);
  1159. }
  1160. return ret;
  1161. }
  1162. #if FF_API_OLD_ENCODE_AUDIO
  1163. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  1164. uint8_t *buf, int buf_size,
  1165. const short *samples)
  1166. {
  1167. AVPacket pkt;
  1168. AVFrame frame0 = { 0 };
  1169. AVFrame *frame;
  1170. int ret, samples_size, got_packet;
  1171. av_init_packet(&pkt);
  1172. pkt.data = buf;
  1173. pkt.size = buf_size;
  1174. if (samples) {
  1175. frame = &frame0;
  1176. avcodec_get_frame_defaults(frame);
  1177. if (avctx->frame_size) {
  1178. frame->nb_samples = avctx->frame_size;
  1179. } else {
  1180. /* if frame_size is not set, the number of samples must be
  1181. * calculated from the buffer size */
  1182. int64_t nb_samples;
  1183. if (!av_get_bits_per_sample(avctx->codec_id)) {
  1184. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  1185. "support this codec\n");
  1186. return AVERROR(EINVAL);
  1187. }
  1188. nb_samples = (int64_t)buf_size * 8 /
  1189. (av_get_bits_per_sample(avctx->codec_id) *
  1190. avctx->channels);
  1191. if (nb_samples >= INT_MAX)
  1192. return AVERROR(EINVAL);
  1193. frame->nb_samples = nb_samples;
  1194. }
  1195. /* it is assumed that the samples buffer is large enough based on the
  1196. * relevant parameters */
  1197. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  1198. frame->nb_samples,
  1199. avctx->sample_fmt, 1);
  1200. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  1201. avctx->sample_fmt,
  1202. (const uint8_t *)samples,
  1203. samples_size, 1)))
  1204. return ret;
  1205. /* fabricate frame pts from sample count.
  1206. * this is needed because the avcodec_encode_audio() API does not have
  1207. * a way for the user to provide pts */
  1208. if (avctx->sample_rate && avctx->time_base.num)
  1209. frame->pts = ff_samples_to_time_base(avctx,
  1210. avctx->internal->sample_count);
  1211. else
  1212. frame->pts = AV_NOPTS_VALUE;
  1213. avctx->internal->sample_count += frame->nb_samples;
  1214. } else {
  1215. frame = NULL;
  1216. }
  1217. got_packet = 0;
  1218. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  1219. if (!ret && got_packet && avctx->coded_frame) {
  1220. avctx->coded_frame->pts = pkt.pts;
  1221. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1222. }
  1223. /* free any side data since we cannot return it */
  1224. ff_packet_free_side_data(&pkt);
  1225. if (frame && frame->extended_data != frame->data)
  1226. av_freep(&frame->extended_data);
  1227. return ret ? ret : pkt.size;
  1228. }
  1229. #endif
  1230. #if FF_API_OLD_ENCODE_VIDEO
  1231. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1232. const AVFrame *pict)
  1233. {
  1234. AVPacket pkt;
  1235. int ret, got_packet = 0;
  1236. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1237. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1238. return -1;
  1239. }
  1240. av_init_packet(&pkt);
  1241. pkt.data = buf;
  1242. pkt.size = buf_size;
  1243. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1244. if (!ret && got_packet && avctx->coded_frame) {
  1245. avctx->coded_frame->pts = pkt.pts;
  1246. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1247. }
  1248. /* free any side data since we cannot return it */
  1249. if (pkt.side_data_elems > 0) {
  1250. int i;
  1251. for (i = 0; i < pkt.side_data_elems; i++)
  1252. av_free(pkt.side_data[i].data);
  1253. av_freep(&pkt.side_data);
  1254. pkt.side_data_elems = 0;
  1255. }
  1256. return ret ? ret : pkt.size;
  1257. }
  1258. #endif
  1259. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1260. AVPacket *avpkt,
  1261. const AVFrame *frame,
  1262. int *got_packet_ptr)
  1263. {
  1264. int ret;
  1265. AVPacket user_pkt = *avpkt;
  1266. int needs_realloc = !user_pkt.data;
  1267. *got_packet_ptr = 0;
  1268. if(HAVE_THREADS && avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
  1269. return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
  1270. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1271. av_free_packet(avpkt);
  1272. av_init_packet(avpkt);
  1273. avpkt->size = 0;
  1274. return 0;
  1275. }
  1276. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1277. return AVERROR(EINVAL);
  1278. av_assert0(avctx->codec->encode2);
  1279. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1280. av_assert0(ret <= 0);
  1281. if (avpkt->data && avpkt->data == avctx->internal->byte_buffer) {
  1282. needs_realloc = 0;
  1283. if (user_pkt.data) {
  1284. if (user_pkt.size >= avpkt->size) {
  1285. memcpy(user_pkt.data, avpkt->data, avpkt->size);
  1286. } else {
  1287. av_log(avctx, AV_LOG_ERROR, "Provided packet is too small, needs to be %d\n", avpkt->size);
  1288. avpkt->size = user_pkt.size;
  1289. ret = -1;
  1290. }
  1291. avpkt->data = user_pkt.data;
  1292. avpkt->destruct = user_pkt.destruct;
  1293. } else {
  1294. if (av_dup_packet(avpkt) < 0) {
  1295. ret = AVERROR(ENOMEM);
  1296. }
  1297. }
  1298. }
  1299. if (!ret) {
  1300. if (!*got_packet_ptr)
  1301. avpkt->size = 0;
  1302. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1303. avpkt->pts = avpkt->dts = frame->pts;
  1304. if (needs_realloc && avpkt->data &&
  1305. avpkt->destruct == av_destruct_packet) {
  1306. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  1307. if (new_data)
  1308. avpkt->data = new_data;
  1309. }
  1310. avctx->frame_number++;
  1311. }
  1312. if (ret < 0 || !*got_packet_ptr)
  1313. av_free_packet(avpkt);
  1314. emms_c();
  1315. return ret;
  1316. }
  1317. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1318. const AVSubtitle *sub)
  1319. {
  1320. int ret;
  1321. if (sub->start_display_time) {
  1322. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1323. return -1;
  1324. }
  1325. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1326. avctx->frame_number++;
  1327. return ret;
  1328. }
  1329. /**
  1330. * Attempt to guess proper monotonic timestamps for decoded video frames
  1331. * which might have incorrect times. Input timestamps may wrap around, in
  1332. * which case the output will as well.
  1333. *
  1334. * @param pts the pts field of the decoded AVPacket, as passed through
  1335. * AVFrame.pkt_pts
  1336. * @param dts the dts field of the decoded AVPacket
  1337. * @return one of the input values, may be AV_NOPTS_VALUE
  1338. */
  1339. static int64_t guess_correct_pts(AVCodecContext *ctx,
  1340. int64_t reordered_pts, int64_t dts)
  1341. {
  1342. int64_t pts = AV_NOPTS_VALUE;
  1343. if (dts != AV_NOPTS_VALUE) {
  1344. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  1345. ctx->pts_correction_last_dts = dts;
  1346. }
  1347. if (reordered_pts != AV_NOPTS_VALUE) {
  1348. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  1349. ctx->pts_correction_last_pts = reordered_pts;
  1350. }
  1351. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  1352. && reordered_pts != AV_NOPTS_VALUE)
  1353. pts = reordered_pts;
  1354. else
  1355. pts = dts;
  1356. return pts;
  1357. }
  1358. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1359. {
  1360. int size = 0;
  1361. const uint8_t *data;
  1362. uint32_t flags;
  1363. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1364. return;
  1365. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1366. if (!data || size < 4)
  1367. return;
  1368. flags = bytestream_get_le32(&data);
  1369. size -= 4;
  1370. if (size < 4) /* Required for any of the changes */
  1371. return;
  1372. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1373. avctx->channels = bytestream_get_le32(&data);
  1374. size -= 4;
  1375. }
  1376. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1377. if (size < 8)
  1378. return;
  1379. avctx->channel_layout = bytestream_get_le64(&data);
  1380. size -= 8;
  1381. }
  1382. if (size < 4)
  1383. return;
  1384. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1385. avctx->sample_rate = bytestream_get_le32(&data);
  1386. size -= 4;
  1387. }
  1388. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1389. if (size < 8)
  1390. return;
  1391. avctx->width = bytestream_get_le32(&data);
  1392. avctx->height = bytestream_get_le32(&data);
  1393. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1394. size -= 8;
  1395. }
  1396. }
  1397. static int add_metadata_from_side_data(AVCodecContext *avctx, AVFrame *frame)
  1398. {
  1399. int size, ret = 0;
  1400. const uint8_t *side_metadata;
  1401. const uint8_t *end;
  1402. av_dict_free(&avctx->metadata);
  1403. side_metadata = av_packet_get_side_data(avctx->pkt,
  1404. AV_PKT_DATA_STRINGS_METADATA, &size);
  1405. if (!side_metadata)
  1406. goto end;
  1407. end = side_metadata + size;
  1408. while (side_metadata < end) {
  1409. const uint8_t *key = side_metadata;
  1410. const uint8_t *val = side_metadata + strlen(key) + 1;
  1411. int ret = av_dict_set(&frame->metadata, key, val, 0);
  1412. if (ret < 0)
  1413. break;
  1414. side_metadata = val + strlen(val) + 1;
  1415. }
  1416. end:
  1417. avctx->metadata = frame->metadata;
  1418. return ret;
  1419. }
  1420. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1421. int *got_picture_ptr,
  1422. const AVPacket *avpkt)
  1423. {
  1424. int ret;
  1425. // copy to ensure we do not change avpkt
  1426. AVPacket tmp = *avpkt;
  1427. if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
  1428. av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
  1429. return AVERROR(EINVAL);
  1430. }
  1431. *got_picture_ptr = 0;
  1432. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1433. return AVERROR(EINVAL);
  1434. avcodec_get_frame_defaults(picture);
  1435. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1436. int did_split = av_packet_split_side_data(&tmp);
  1437. apply_param_change(avctx, &tmp);
  1438. avctx->pkt = &tmp;
  1439. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1440. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1441. &tmp);
  1442. else {
  1443. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1444. &tmp);
  1445. picture->pkt_dts = avpkt->dts;
  1446. if(!avctx->has_b_frames){
  1447. picture->pkt_pos = avpkt->pos;
  1448. }
  1449. //FIXME these should be under if(!avctx->has_b_frames)
  1450. if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1451. if (!picture->width) picture->width = avctx->width;
  1452. if (!picture->height) picture->height = avctx->height;
  1453. if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt;
  1454. }
  1455. add_metadata_from_side_data(avctx, picture);
  1456. emms_c(); //needed to avoid an emms_c() call before every return;
  1457. avctx->pkt = NULL;
  1458. if (did_split) {
  1459. ff_packet_free_side_data(&tmp);
  1460. if(ret == tmp.size)
  1461. ret = avpkt->size;
  1462. }
  1463. if (*got_picture_ptr){
  1464. avctx->frame_number++;
  1465. picture->best_effort_timestamp = guess_correct_pts(avctx,
  1466. picture->pkt_pts,
  1467. picture->pkt_dts);
  1468. }
  1469. } else
  1470. ret = 0;
  1471. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1472. * make sure it's set correctly */
  1473. picture->extended_data = picture->data;
  1474. return ret;
  1475. }
  1476. #if FF_API_OLD_DECODE_AUDIO
  1477. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1478. int *frame_size_ptr,
  1479. AVPacket *avpkt)
  1480. {
  1481. AVFrame frame = {0};
  1482. int ret, got_frame = 0;
  1483. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1484. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1485. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1486. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1487. "avcodec_decode_audio4()\n");
  1488. avctx->get_buffer = avcodec_default_get_buffer;
  1489. avctx->release_buffer = avcodec_default_release_buffer;
  1490. }
  1491. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1492. if (ret >= 0 && got_frame) {
  1493. int ch, plane_size;
  1494. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1495. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1496. frame.nb_samples,
  1497. avctx->sample_fmt, 1);
  1498. if (*frame_size_ptr < data_size) {
  1499. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1500. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1501. return AVERROR(EINVAL);
  1502. }
  1503. memcpy(samples, frame.extended_data[0], plane_size);
  1504. if (planar && avctx->channels > 1) {
  1505. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1506. for (ch = 1; ch < avctx->channels; ch++) {
  1507. memcpy(out, frame.extended_data[ch], plane_size);
  1508. out += plane_size;
  1509. }
  1510. }
  1511. *frame_size_ptr = data_size;
  1512. } else {
  1513. *frame_size_ptr = 0;
  1514. }
  1515. return ret;
  1516. }
  1517. #endif
  1518. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1519. AVFrame *frame,
  1520. int *got_frame_ptr,
  1521. const AVPacket *avpkt)
  1522. {
  1523. int planar, channels;
  1524. int ret = 0;
  1525. *got_frame_ptr = 0;
  1526. if (!avpkt->data && avpkt->size) {
  1527. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1528. return AVERROR(EINVAL);
  1529. }
  1530. if (avctx->codec->type != AVMEDIA_TYPE_AUDIO) {
  1531. av_log(avctx, AV_LOG_ERROR, "Invalid media type for audio\n");
  1532. return AVERROR(EINVAL);
  1533. }
  1534. avcodec_get_frame_defaults(frame);
  1535. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1536. uint8_t *side;
  1537. int side_size;
  1538. // copy to ensure we do not change avpkt
  1539. AVPacket tmp = *avpkt;
  1540. int did_split = av_packet_split_side_data(&tmp);
  1541. apply_param_change(avctx, &tmp);
  1542. avctx->pkt = &tmp;
  1543. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
  1544. if (ret >= 0 && *got_frame_ptr) {
  1545. avctx->frame_number++;
  1546. frame->pkt_dts = avpkt->dts;
  1547. frame->best_effort_timestamp = guess_correct_pts(avctx,
  1548. frame->pkt_pts,
  1549. frame->pkt_dts);
  1550. if (frame->format == AV_SAMPLE_FMT_NONE)
  1551. frame->format = avctx->sample_fmt;
  1552. if (!frame->channel_layout)
  1553. frame->channel_layout = avctx->channel_layout;
  1554. if (!frame->channels)
  1555. frame->channels = avctx->channels;
  1556. if (!frame->sample_rate)
  1557. frame->sample_rate = avctx->sample_rate;
  1558. }
  1559. add_metadata_from_side_data(avctx, frame);
  1560. side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
  1561. if(side && side_size>=10) {
  1562. avctx->internal->skip_samples = AV_RL32(side);
  1563. av_log(avctx, AV_LOG_DEBUG, "skip %d samples due to side data\n",
  1564. avctx->internal->skip_samples);
  1565. }
  1566. if (avctx->internal->skip_samples) {
  1567. if(frame->nb_samples <= avctx->internal->skip_samples){
  1568. *got_frame_ptr = 0;
  1569. avctx->internal->skip_samples -= frame->nb_samples;
  1570. av_log(avctx, AV_LOG_DEBUG, "skip whole frame, skip left: %d\n",
  1571. avctx->internal->skip_samples);
  1572. } else {
  1573. av_samples_copy(frame->extended_data, frame->extended_data, 0, avctx->internal->skip_samples,
  1574. frame->nb_samples - avctx->internal->skip_samples, avctx->channels, frame->format);
  1575. if(avctx->pkt_timebase.num && avctx->sample_rate) {
  1576. int64_t diff_ts = av_rescale_q(avctx->internal->skip_samples,
  1577. (AVRational){1, avctx->sample_rate},
  1578. avctx->pkt_timebase);
  1579. if(frame->pkt_pts!=AV_NOPTS_VALUE)
  1580. frame->pkt_pts += diff_ts;
  1581. if(frame->pkt_dts!=AV_NOPTS_VALUE)
  1582. frame->pkt_dts += diff_ts;
  1583. if (frame->pkt_duration >= diff_ts)
  1584. frame->pkt_duration -= diff_ts;
  1585. } else {
  1586. av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
  1587. }
  1588. av_log(avctx, AV_LOG_DEBUG, "skip %d/%d samples\n",
  1589. avctx->internal->skip_samples, frame->nb_samples);
  1590. frame->nb_samples -= avctx->internal->skip_samples;
  1591. avctx->internal->skip_samples = 0;
  1592. }
  1593. }
  1594. avctx->pkt = NULL;
  1595. if (did_split) {
  1596. ff_packet_free_side_data(&tmp);
  1597. if(ret == tmp.size)
  1598. ret = avpkt->size;
  1599. }
  1600. }
  1601. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1602. * make sure it's set correctly; assume decoders that actually use
  1603. * extended_data are doing it correctly */
  1604. if (*got_frame_ptr) {
  1605. planar = av_sample_fmt_is_planar(frame->format);
  1606. channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  1607. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1608. frame->extended_data = frame->data;
  1609. } else {
  1610. frame->extended_data = NULL;
  1611. }
  1612. return ret;
  1613. }
  1614. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1615. int *got_sub_ptr,
  1616. AVPacket *avpkt)
  1617. {
  1618. int ret = 0;
  1619. if (avctx->codec->type != AVMEDIA_TYPE_SUBTITLE) {
  1620. av_log(avctx, AV_LOG_ERROR, "Invalid media type for subtitles\n");
  1621. return AVERROR(EINVAL);
  1622. }
  1623. *got_sub_ptr = 0;
  1624. avcodec_get_subtitle_defaults(sub);
  1625. if (avpkt->size) {
  1626. AVPacket tmp = *avpkt;
  1627. int did_split = av_packet_split_side_data(&tmp);
  1628. //apply_param_change(avctx, &tmp);
  1629. avctx->pkt = &tmp;
  1630. if (avctx->pkt_timebase.den && avpkt->pts != AV_NOPTS_VALUE)
  1631. sub->pts = av_rescale_q(avpkt->pts,
  1632. avctx->pkt_timebase, AV_TIME_BASE_Q);
  1633. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &tmp);
  1634. avctx->pkt = NULL;
  1635. if (did_split) {
  1636. ff_packet_free_side_data(&tmp);
  1637. if(ret == tmp.size)
  1638. ret = avpkt->size;
  1639. }
  1640. if (*got_sub_ptr)
  1641. avctx->frame_number++;
  1642. }
  1643. return ret;
  1644. }
  1645. void avsubtitle_free(AVSubtitle *sub)
  1646. {
  1647. int i;
  1648. for (i = 0; i < sub->num_rects; i++) {
  1649. av_freep(&sub->rects[i]->pict.data[0]);
  1650. av_freep(&sub->rects[i]->pict.data[1]);
  1651. av_freep(&sub->rects[i]->pict.data[2]);
  1652. av_freep(&sub->rects[i]->pict.data[3]);
  1653. av_freep(&sub->rects[i]->text);
  1654. av_freep(&sub->rects[i]->ass);
  1655. av_freep(&sub->rects[i]);
  1656. }
  1657. av_freep(&sub->rects);
  1658. memset(sub, 0, sizeof(AVSubtitle));
  1659. }
  1660. av_cold int ff_codec_close_recursive(AVCodecContext *avctx)
  1661. {
  1662. int ret = 0;
  1663. entangled_thread_counter--;
  1664. /* Release any user-supplied mutex. */
  1665. if (ff_lockmgr_cb) {
  1666. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1667. }
  1668. ret = avcodec_close(avctx);
  1669. /* If there is a user-supplied mutex locking routine, call it. */
  1670. if (ff_lockmgr_cb) {
  1671. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1672. return -1;
  1673. }
  1674. entangled_thread_counter++;
  1675. return ret;
  1676. }
  1677. av_cold int avcodec_close(AVCodecContext *avctx)
  1678. {
  1679. /* If there is a user-supplied mutex locking routine, call it. */
  1680. if (ff_lockmgr_cb) {
  1681. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1682. return -1;
  1683. }
  1684. entangled_thread_counter++;
  1685. if (entangled_thread_counter != 1) {
  1686. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1687. entangled_thread_counter--;
  1688. return -1;
  1689. }
  1690. if (avcodec_is_open(avctx)) {
  1691. if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
  1692. entangled_thread_counter --;
  1693. ff_frame_thread_encoder_free(avctx);
  1694. entangled_thread_counter ++;
  1695. }
  1696. if (HAVE_THREADS && avctx->thread_opaque)
  1697. ff_thread_free(avctx);
  1698. if (avctx->codec && avctx->codec->close)
  1699. avctx->codec->close(avctx);
  1700. avcodec_default_free_buffers(avctx);
  1701. avctx->coded_frame = NULL;
  1702. avctx->internal->byte_buffer_size = 0;
  1703. av_freep(&avctx->internal->byte_buffer);
  1704. av_freep(&avctx->internal);
  1705. av_dict_free(&avctx->metadata);
  1706. }
  1707. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1708. av_opt_free(avctx->priv_data);
  1709. av_opt_free(avctx);
  1710. av_freep(&avctx->priv_data);
  1711. if (av_codec_is_encoder(avctx->codec))
  1712. av_freep(&avctx->extradata);
  1713. avctx->codec = NULL;
  1714. avctx->active_thread_type = 0;
  1715. entangled_thread_counter--;
  1716. /* Release any user-supplied mutex. */
  1717. if (ff_lockmgr_cb) {
  1718. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1719. }
  1720. return 0;
  1721. }
  1722. static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
  1723. {
  1724. switch(id){
  1725. //This is for future deprecatec codec ids, its empty since
  1726. //last major bump but will fill up again over time, please don't remove it
  1727. // case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
  1728. case AV_CODEC_ID_OPUS_DEPRECATED: return AV_CODEC_ID_OPUS;
  1729. default : return id;
  1730. }
  1731. }
  1732. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  1733. {
  1734. AVCodec *p, *experimental = NULL;
  1735. p = first_avcodec;
  1736. id= remap_deprecated_codec_id(id);
  1737. while (p) {
  1738. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  1739. p->id == id) {
  1740. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1741. experimental = p;
  1742. } else
  1743. return p;
  1744. }
  1745. p = p->next;
  1746. }
  1747. return experimental;
  1748. }
  1749. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1750. {
  1751. return find_encdec(id, 1);
  1752. }
  1753. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1754. {
  1755. AVCodec *p;
  1756. if (!name)
  1757. return NULL;
  1758. p = first_avcodec;
  1759. while (p) {
  1760. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1761. return p;
  1762. p = p->next;
  1763. }
  1764. return NULL;
  1765. }
  1766. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1767. {
  1768. return find_encdec(id, 0);
  1769. }
  1770. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1771. {
  1772. AVCodec *p;
  1773. if (!name)
  1774. return NULL;
  1775. p = first_avcodec;
  1776. while (p) {
  1777. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1778. return p;
  1779. p = p->next;
  1780. }
  1781. return NULL;
  1782. }
  1783. const char *avcodec_get_name(enum AVCodecID id)
  1784. {
  1785. const AVCodecDescriptor *cd;
  1786. AVCodec *codec;
  1787. if (id == AV_CODEC_ID_NONE)
  1788. return "none";
  1789. cd = avcodec_descriptor_get(id);
  1790. if (cd)
  1791. return cd->name;
  1792. av_log(NULL, AV_LOG_WARNING, "Codec 0x%x is not in the full list.\n", id);
  1793. codec = avcodec_find_decoder(id);
  1794. if (codec)
  1795. return codec->name;
  1796. codec = avcodec_find_encoder(id);
  1797. if (codec)
  1798. return codec->name;
  1799. return "unknown_codec";
  1800. }
  1801. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1802. {
  1803. int i, len, ret = 0;
  1804. #define IS_PRINT(x) \
  1805. (((x) >= '0' && (x) <= '9') || \
  1806. ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || \
  1807. ((x) == '.' || (x) == ' ' || (x) == '-'))
  1808. for (i = 0; i < 4; i++) {
  1809. len = snprintf(buf, buf_size,
  1810. IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  1811. buf += len;
  1812. buf_size = buf_size > len ? buf_size - len : 0;
  1813. ret += len;
  1814. codec_tag >>= 8;
  1815. }
  1816. return ret;
  1817. }
  1818. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1819. {
  1820. const char *codec_type;
  1821. const char *codec_name;
  1822. const char *profile = NULL;
  1823. const AVCodec *p;
  1824. int bitrate;
  1825. AVRational display_aspect_ratio;
  1826. if (!buf || buf_size <= 0)
  1827. return;
  1828. codec_type = av_get_media_type_string(enc->codec_type);
  1829. codec_name = avcodec_get_name(enc->codec_id);
  1830. if (enc->profile != FF_PROFILE_UNKNOWN) {
  1831. if (enc->codec)
  1832. p = enc->codec;
  1833. else
  1834. p = encode ? avcodec_find_encoder(enc->codec_id) :
  1835. avcodec_find_decoder(enc->codec_id);
  1836. if (p)
  1837. profile = av_get_profile_name(p, enc->profile);
  1838. }
  1839. snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
  1840. codec_name, enc->mb_decision ? " (hq)" : "");
  1841. buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
  1842. if (profile)
  1843. snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
  1844. if (enc->codec_tag) {
  1845. char tag_buf[32];
  1846. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1847. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1848. " (%s / 0x%04X)", tag_buf, enc->codec_tag);
  1849. }
  1850. switch (enc->codec_type) {
  1851. case AVMEDIA_TYPE_VIDEO:
  1852. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  1853. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1854. ", %s",
  1855. av_get_pix_fmt_name(enc->pix_fmt));
  1856. }
  1857. if (enc->width) {
  1858. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1859. ", %dx%d",
  1860. enc->width, enc->height);
  1861. if (enc->sample_aspect_ratio.num) {
  1862. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1863. enc->width * enc->sample_aspect_ratio.num,
  1864. enc->height * enc->sample_aspect_ratio.den,
  1865. 1024 * 1024);
  1866. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1867. " [SAR %d:%d DAR %d:%d]",
  1868. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1869. display_aspect_ratio.num, display_aspect_ratio.den);
  1870. }
  1871. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1872. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1873. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1874. ", %d/%d",
  1875. enc->time_base.num / g, enc->time_base.den / g);
  1876. }
  1877. }
  1878. if (encode) {
  1879. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1880. ", q=%d-%d", enc->qmin, enc->qmax);
  1881. }
  1882. break;
  1883. case AVMEDIA_TYPE_AUDIO:
  1884. if (enc->sample_rate) {
  1885. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1886. ", %d Hz", enc->sample_rate);
  1887. }
  1888. av_strlcat(buf, ", ", buf_size);
  1889. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1890. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1891. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1892. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1893. }
  1894. break;
  1895. default:
  1896. return;
  1897. }
  1898. if (encode) {
  1899. if (enc->flags & CODEC_FLAG_PASS1)
  1900. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1901. ", pass 1");
  1902. if (enc->flags & CODEC_FLAG_PASS2)
  1903. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1904. ", pass 2");
  1905. }
  1906. bitrate = get_bit_rate(enc);
  1907. if (bitrate != 0) {
  1908. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1909. ", %d kb/s", bitrate / 1000);
  1910. }
  1911. }
  1912. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1913. {
  1914. const AVProfile *p;
  1915. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1916. return NULL;
  1917. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1918. if (p->profile == profile)
  1919. return p->name;
  1920. return NULL;
  1921. }
  1922. unsigned avcodec_version(void)
  1923. {
  1924. // av_assert0(AV_CODEC_ID_V410==164);
  1925. av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
  1926. av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
  1927. // av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
  1928. av_assert0(AV_CODEC_ID_SRT==94216);
  1929. av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
  1930. return LIBAVCODEC_VERSION_INT;
  1931. }
  1932. const char *avcodec_configuration(void)
  1933. {
  1934. return FFMPEG_CONFIGURATION;
  1935. }
  1936. const char *avcodec_license(void)
  1937. {
  1938. #define LICENSE_PREFIX "libavcodec license: "
  1939. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1940. }
  1941. void avcodec_flush_buffers(AVCodecContext *avctx)
  1942. {
  1943. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1944. ff_thread_flush(avctx);
  1945. else if (avctx->codec->flush)
  1946. avctx->codec->flush(avctx);
  1947. avctx->pts_correction_last_pts =
  1948. avctx->pts_correction_last_dts = INT64_MIN;
  1949. }
  1950. static void video_free_buffers(AVCodecContext *s)
  1951. {
  1952. AVCodecInternal *avci = s->internal;
  1953. int i, j;
  1954. if (!avci->buffer)
  1955. return;
  1956. if (avci->buffer_count)
  1957. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1958. avci->buffer_count);
  1959. for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
  1960. InternalBuffer *buf = &avci->buffer[i];
  1961. for (j = 0; j < 4; j++) {
  1962. av_freep(&buf->base[j]);
  1963. buf->data[j] = NULL;
  1964. }
  1965. }
  1966. av_freep(&avci->buffer);
  1967. avci->buffer_count = 0;
  1968. }
  1969. static void audio_free_buffers(AVCodecContext *avctx)
  1970. {
  1971. AVCodecInternal *avci = avctx->internal;
  1972. InternalBuffer *buf;
  1973. if (!avci->buffer)
  1974. return;
  1975. buf = avci->buffer;
  1976. if (buf->extended_data) {
  1977. av_free(buf->extended_data[0]);
  1978. if (buf->extended_data != buf->data)
  1979. av_freep(&buf->extended_data);
  1980. }
  1981. av_freep(&avci->buffer);
  1982. }
  1983. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1984. {
  1985. switch (avctx->codec_type) {
  1986. case AVMEDIA_TYPE_VIDEO:
  1987. video_free_buffers(avctx);
  1988. break;
  1989. case AVMEDIA_TYPE_AUDIO:
  1990. audio_free_buffers(avctx);
  1991. break;
  1992. default:
  1993. break;
  1994. }
  1995. }
  1996. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1997. {
  1998. switch (codec_id) {
  1999. case AV_CODEC_ID_ADPCM_CT:
  2000. case AV_CODEC_ID_ADPCM_IMA_APC:
  2001. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  2002. case AV_CODEC_ID_ADPCM_IMA_WS:
  2003. case AV_CODEC_ID_ADPCM_G722:
  2004. case AV_CODEC_ID_ADPCM_YAMAHA:
  2005. return 4;
  2006. case AV_CODEC_ID_PCM_ALAW:
  2007. case AV_CODEC_ID_PCM_MULAW:
  2008. case AV_CODEC_ID_PCM_S8:
  2009. case AV_CODEC_ID_PCM_U8:
  2010. case AV_CODEC_ID_PCM_ZORK:
  2011. return 8;
  2012. case AV_CODEC_ID_PCM_S16BE:
  2013. case AV_CODEC_ID_PCM_S16LE:
  2014. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  2015. case AV_CODEC_ID_PCM_U16BE:
  2016. case AV_CODEC_ID_PCM_U16LE:
  2017. return 16;
  2018. case AV_CODEC_ID_PCM_S24DAUD:
  2019. case AV_CODEC_ID_PCM_S24BE:
  2020. case AV_CODEC_ID_PCM_S24LE:
  2021. case AV_CODEC_ID_PCM_U24BE:
  2022. case AV_CODEC_ID_PCM_U24LE:
  2023. return 24;
  2024. case AV_CODEC_ID_PCM_S32BE:
  2025. case AV_CODEC_ID_PCM_S32LE:
  2026. case AV_CODEC_ID_PCM_U32BE:
  2027. case AV_CODEC_ID_PCM_U32LE:
  2028. case AV_CODEC_ID_PCM_F32BE:
  2029. case AV_CODEC_ID_PCM_F32LE:
  2030. return 32;
  2031. case AV_CODEC_ID_PCM_F64BE:
  2032. case AV_CODEC_ID_PCM_F64LE:
  2033. return 64;
  2034. default:
  2035. return 0;
  2036. }
  2037. }
  2038. enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
  2039. {
  2040. static const enum AVCodecID map[AV_SAMPLE_FMT_NB][2] = {
  2041. [AV_SAMPLE_FMT_U8 ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2042. [AV_SAMPLE_FMT_S16 ] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2043. [AV_SAMPLE_FMT_S32 ] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2044. [AV_SAMPLE_FMT_FLT ] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2045. [AV_SAMPLE_FMT_DBL ] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2046. [AV_SAMPLE_FMT_U8P ] = { AV_CODEC_ID_PCM_U8, AV_CODEC_ID_PCM_U8 },
  2047. [AV_SAMPLE_FMT_S16P] = { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S16BE },
  2048. [AV_SAMPLE_FMT_S32P] = { AV_CODEC_ID_PCM_S32LE, AV_CODEC_ID_PCM_S32BE },
  2049. [AV_SAMPLE_FMT_FLTP] = { AV_CODEC_ID_PCM_F32LE, AV_CODEC_ID_PCM_F32BE },
  2050. [AV_SAMPLE_FMT_DBLP] = { AV_CODEC_ID_PCM_F64LE, AV_CODEC_ID_PCM_F64BE },
  2051. };
  2052. if (fmt < 0 || fmt >= AV_SAMPLE_FMT_NB)
  2053. return AV_CODEC_ID_NONE;
  2054. if (be < 0 || be > 1)
  2055. be = AV_NE(1, 0);
  2056. return map[fmt][be];
  2057. }
  2058. int av_get_bits_per_sample(enum AVCodecID codec_id)
  2059. {
  2060. switch (codec_id) {
  2061. case AV_CODEC_ID_ADPCM_SBPRO_2:
  2062. return 2;
  2063. case AV_CODEC_ID_ADPCM_SBPRO_3:
  2064. return 3;
  2065. case AV_CODEC_ID_ADPCM_SBPRO_4:
  2066. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2067. case AV_CODEC_ID_ADPCM_IMA_QT:
  2068. case AV_CODEC_ID_ADPCM_SWF:
  2069. case AV_CODEC_ID_ADPCM_MS:
  2070. return 4;
  2071. default:
  2072. return av_get_exact_bits_per_sample(codec_id);
  2073. }
  2074. }
  2075. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  2076. {
  2077. int id, sr, ch, ba, tag, bps;
  2078. id = avctx->codec_id;
  2079. sr = avctx->sample_rate;
  2080. ch = avctx->channels;
  2081. ba = avctx->block_align;
  2082. tag = avctx->codec_tag;
  2083. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  2084. /* codecs with an exact constant bits per sample */
  2085. if (bps > 0 && ch > 0 && frame_bytes > 0 && ch < 32768 && bps < 32768)
  2086. return (frame_bytes * 8LL) / (bps * ch);
  2087. bps = avctx->bits_per_coded_sample;
  2088. /* codecs with a fixed packet duration */
  2089. switch (id) {
  2090. case AV_CODEC_ID_ADPCM_ADX: return 32;
  2091. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  2092. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  2093. case AV_CODEC_ID_AMR_NB:
  2094. case AV_CODEC_ID_GSM:
  2095. case AV_CODEC_ID_QCELP:
  2096. case AV_CODEC_ID_RA_288: return 160;
  2097. case AV_CODEC_ID_AMR_WB:
  2098. case AV_CODEC_ID_GSM_MS: return 320;
  2099. case AV_CODEC_ID_MP1: return 384;
  2100. case AV_CODEC_ID_ATRAC1: return 512;
  2101. case AV_CODEC_ID_ATRAC3: return 1024;
  2102. case AV_CODEC_ID_MP2:
  2103. case AV_CODEC_ID_MUSEPACK7: return 1152;
  2104. case AV_CODEC_ID_AC3: return 1536;
  2105. }
  2106. if (sr > 0) {
  2107. /* calc from sample rate */
  2108. if (id == AV_CODEC_ID_TTA)
  2109. return 256 * sr / 245;
  2110. if (ch > 0) {
  2111. /* calc from sample rate and channels */
  2112. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  2113. return (480 << (sr / 22050)) / ch;
  2114. }
  2115. }
  2116. if (ba > 0) {
  2117. /* calc from block_align */
  2118. if (id == AV_CODEC_ID_SIPR) {
  2119. switch (ba) {
  2120. case 20: return 160;
  2121. case 19: return 144;
  2122. case 29: return 288;
  2123. case 37: return 480;
  2124. }
  2125. } else if (id == AV_CODEC_ID_ILBC) {
  2126. switch (ba) {
  2127. case 38: return 160;
  2128. case 50: return 240;
  2129. }
  2130. }
  2131. }
  2132. if (frame_bytes > 0) {
  2133. /* calc from frame_bytes only */
  2134. if (id == AV_CODEC_ID_TRUESPEECH)
  2135. return 240 * (frame_bytes / 32);
  2136. if (id == AV_CODEC_ID_NELLYMOSER)
  2137. return 256 * (frame_bytes / 64);
  2138. if (id == AV_CODEC_ID_RA_144)
  2139. return 160 * (frame_bytes / 20);
  2140. if (id == AV_CODEC_ID_G723_1)
  2141. return 240 * (frame_bytes / 24);
  2142. if (bps > 0) {
  2143. /* calc from frame_bytes and bits_per_coded_sample */
  2144. if (id == AV_CODEC_ID_ADPCM_G726)
  2145. return frame_bytes * 8 / bps;
  2146. }
  2147. if (ch > 0) {
  2148. /* calc from frame_bytes and channels */
  2149. switch (id) {
  2150. case AV_CODEC_ID_ADPCM_4XM:
  2151. case AV_CODEC_ID_ADPCM_IMA_ISS:
  2152. return (frame_bytes - 4 * ch) * 2 / ch;
  2153. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  2154. return (frame_bytes - 4) * 2 / ch;
  2155. case AV_CODEC_ID_ADPCM_IMA_AMV:
  2156. return (frame_bytes - 8) * 2 / ch;
  2157. case AV_CODEC_ID_ADPCM_XA:
  2158. return (frame_bytes / 128) * 224 / ch;
  2159. case AV_CODEC_ID_INTERPLAY_DPCM:
  2160. return (frame_bytes - 6 - ch) / ch;
  2161. case AV_CODEC_ID_ROQ_DPCM:
  2162. return (frame_bytes - 8) / ch;
  2163. case AV_CODEC_ID_XAN_DPCM:
  2164. return (frame_bytes - 2 * ch) / ch;
  2165. case AV_CODEC_ID_MACE3:
  2166. return 3 * frame_bytes / ch;
  2167. case AV_CODEC_ID_MACE6:
  2168. return 6 * frame_bytes / ch;
  2169. case AV_CODEC_ID_PCM_LXF:
  2170. return 2 * (frame_bytes / (5 * ch));
  2171. case AV_CODEC_ID_IAC:
  2172. case AV_CODEC_ID_IMC:
  2173. return 4 * frame_bytes / ch;
  2174. }
  2175. if (tag) {
  2176. /* calc from frame_bytes, channels, and codec_tag */
  2177. if (id == AV_CODEC_ID_SOL_DPCM) {
  2178. if (tag == 3)
  2179. return frame_bytes / ch;
  2180. else
  2181. return frame_bytes * 2 / ch;
  2182. }
  2183. }
  2184. if (ba > 0) {
  2185. /* calc from frame_bytes, channels, and block_align */
  2186. int blocks = frame_bytes / ba;
  2187. switch (avctx->codec_id) {
  2188. case AV_CODEC_ID_ADPCM_IMA_WAV:
  2189. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  2190. case AV_CODEC_ID_ADPCM_IMA_DK3:
  2191. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  2192. case AV_CODEC_ID_ADPCM_IMA_DK4:
  2193. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  2194. case AV_CODEC_ID_ADPCM_MS:
  2195. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  2196. }
  2197. }
  2198. if (bps > 0) {
  2199. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  2200. switch (avctx->codec_id) {
  2201. case AV_CODEC_ID_PCM_DVD:
  2202. if(bps<4)
  2203. return 0;
  2204. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  2205. case AV_CODEC_ID_PCM_BLURAY:
  2206. if(bps<4)
  2207. return 0;
  2208. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  2209. case AV_CODEC_ID_S302M:
  2210. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  2211. }
  2212. }
  2213. }
  2214. }
  2215. return 0;
  2216. }
  2217. #if !HAVE_THREADS
  2218. int ff_thread_init(AVCodecContext *s)
  2219. {
  2220. return -1;
  2221. }
  2222. #endif
  2223. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  2224. {
  2225. unsigned int n = 0;
  2226. while (v >= 0xff) {
  2227. *s++ = 0xff;
  2228. v -= 0xff;
  2229. n++;
  2230. }
  2231. *s = v;
  2232. n++;
  2233. return n;
  2234. }
  2235. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  2236. {
  2237. int i;
  2238. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  2239. return i;
  2240. }
  2241. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  2242. {
  2243. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
  2244. "version to the newest one from Git. If the problem still "
  2245. "occurs, it means that your file has a feature which has not "
  2246. "been implemented.\n", feature);
  2247. if(want_sample)
  2248. av_log_ask_for_sample(avc, NULL);
  2249. }
  2250. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  2251. {
  2252. va_list argument_list;
  2253. va_start(argument_list, msg);
  2254. if (msg)
  2255. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  2256. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  2257. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  2258. "and contact the ffmpeg-devel mailing list.\n");
  2259. va_end(argument_list);
  2260. }
  2261. static AVHWAccel *first_hwaccel = NULL;
  2262. void av_register_hwaccel(AVHWAccel *hwaccel)
  2263. {
  2264. AVHWAccel **p = &first_hwaccel;
  2265. while (*p)
  2266. p = &(*p)->next;
  2267. *p = hwaccel;
  2268. hwaccel->next = NULL;
  2269. }
  2270. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  2271. {
  2272. return hwaccel ? hwaccel->next : first_hwaccel;
  2273. }
  2274. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  2275. {
  2276. AVHWAccel *hwaccel = NULL;
  2277. while ((hwaccel = av_hwaccel_next(hwaccel)))
  2278. if (hwaccel->id == codec_id
  2279. && hwaccel->pix_fmt == pix_fmt)
  2280. return hwaccel;
  2281. return NULL;
  2282. }
  2283. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  2284. {
  2285. if (ff_lockmgr_cb) {
  2286. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  2287. return -1;
  2288. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  2289. return -1;
  2290. }
  2291. ff_lockmgr_cb = cb;
  2292. if (ff_lockmgr_cb) {
  2293. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  2294. return -1;
  2295. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  2296. return -1;
  2297. }
  2298. return 0;
  2299. }
  2300. int avpriv_lock_avformat(void)
  2301. {
  2302. if (ff_lockmgr_cb) {
  2303. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  2304. return -1;
  2305. }
  2306. return 0;
  2307. }
  2308. int avpriv_unlock_avformat(void)
  2309. {
  2310. if (ff_lockmgr_cb) {
  2311. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  2312. return -1;
  2313. }
  2314. return 0;
  2315. }
  2316. unsigned int avpriv_toupper4(unsigned int x)
  2317. {
  2318. return toupper(x & 0xFF)
  2319. + (toupper((x >> 8) & 0xFF) << 8)
  2320. + (toupper((x >> 16) & 0xFF) << 16)
  2321. + (toupper((x >> 24) & 0xFF) << 24);
  2322. }
  2323. #if !HAVE_THREADS
  2324. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  2325. {
  2326. f->owner = avctx;
  2327. ff_init_buffer_info(avctx, f);
  2328. return avctx->get_buffer(avctx, f);
  2329. }
  2330. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  2331. {
  2332. f->owner->release_buffer(f->owner, f);
  2333. }
  2334. void ff_thread_finish_setup(AVCodecContext *avctx)
  2335. {
  2336. }
  2337. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  2338. {
  2339. }
  2340. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  2341. {
  2342. }
  2343. int ff_thread_can_start_frame(AVCodecContext *avctx)
  2344. {
  2345. return 1;
  2346. }
  2347. #endif
  2348. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  2349. {
  2350. AVCodec *c= avcodec_find_decoder(codec_id);
  2351. if(!c)
  2352. c= avcodec_find_encoder(codec_id);
  2353. if(c)
  2354. return c->type;
  2355. if (codec_id <= AV_CODEC_ID_NONE)
  2356. return AVMEDIA_TYPE_UNKNOWN;
  2357. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  2358. return AVMEDIA_TYPE_VIDEO;
  2359. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  2360. return AVMEDIA_TYPE_AUDIO;
  2361. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  2362. return AVMEDIA_TYPE_SUBTITLE;
  2363. return AVMEDIA_TYPE_UNKNOWN;
  2364. }
  2365. int avcodec_is_open(AVCodecContext *s)
  2366. {
  2367. return !!s->internal;
  2368. }