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.

2750 lines
88KB

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