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.

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