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.

2538 lines
81KB

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