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.

2188 lines
66KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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 "avcodec.h"
  36. #include "dsputil.h"
  37. #include "libavutil/opt.h"
  38. #include "thread.h"
  39. #include "internal.h"
  40. #include "bytestream.h"
  41. #include <stdlib.h>
  42. #include <stdarg.h>
  43. #include <limits.h>
  44. #include <float.h>
  45. static int volatile entangled_thread_counter = 0;
  46. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  47. static void *codec_mutex;
  48. static void *avformat_mutex;
  49. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  50. {
  51. if (min_size < *size)
  52. return ptr;
  53. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  54. ptr = av_realloc(ptr, min_size);
  55. /* we could set this to the unmodified min_size but this is safer
  56. * if the user lost the ptr and uses NULL now
  57. */
  58. if (!ptr)
  59. min_size = 0;
  60. *size = min_size;
  61. return ptr;
  62. }
  63. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  64. {
  65. void **p = ptr;
  66. if (min_size < *size)
  67. return;
  68. min_size = FFMAX(17 * min_size / 16 + 32, min_size);
  69. av_free(*p);
  70. *p = av_malloc(min_size);
  71. if (!*p)
  72. min_size = 0;
  73. *size = min_size;
  74. }
  75. void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
  76. {
  77. void **p = ptr;
  78. if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
  79. av_freep(p);
  80. *size = 0;
  81. return;
  82. }
  83. av_fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE);
  84. if (*size)
  85. memset((uint8_t *)*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  86. }
  87. /* encoder management */
  88. static AVCodec *first_avcodec = NULL;
  89. AVCodec *av_codec_next(const AVCodec *c)
  90. {
  91. if (c)
  92. return c->next;
  93. else
  94. return first_avcodec;
  95. }
  96. static void avcodec_init(void)
  97. {
  98. static int initialized = 0;
  99. if (initialized != 0)
  100. return;
  101. initialized = 1;
  102. ff_dsputil_static_init();
  103. }
  104. int av_codec_is_encoder(const AVCodec *codec)
  105. {
  106. return codec && (codec->encode_sub || codec->encode2);
  107. }
  108. int av_codec_is_decoder(const AVCodec *codec)
  109. {
  110. return codec && codec->decode;
  111. }
  112. void avcodec_register(AVCodec *codec)
  113. {
  114. AVCodec **p;
  115. avcodec_init();
  116. p = &first_avcodec;
  117. while (*p != NULL)
  118. p = &(*p)->next;
  119. *p = codec;
  120. codec->next = NULL;
  121. if (codec->init_static_data)
  122. codec->init_static_data(codec);
  123. }
  124. unsigned avcodec_get_edge_width(void)
  125. {
  126. return EDGE_WIDTH;
  127. }
  128. void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
  129. {
  130. s->coded_width = width;
  131. s->coded_height = height;
  132. s->width = width;
  133. s->height = height;
  134. }
  135. #define INTERNAL_BUFFER_SIZE (32 + 1)
  136. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  137. int linesize_align[AV_NUM_DATA_POINTERS])
  138. {
  139. int i;
  140. int w_align = 1;
  141. int h_align = 1;
  142. switch (s->pix_fmt) {
  143. case AV_PIX_FMT_YUV420P:
  144. case AV_PIX_FMT_YUYV422:
  145. case AV_PIX_FMT_UYVY422:
  146. case AV_PIX_FMT_YUV422P:
  147. case AV_PIX_FMT_YUV440P:
  148. case AV_PIX_FMT_YUV444P:
  149. case AV_PIX_FMT_GBRP:
  150. case AV_PIX_FMT_GRAY8:
  151. case AV_PIX_FMT_GRAY16BE:
  152. case AV_PIX_FMT_GRAY16LE:
  153. case AV_PIX_FMT_YUVJ420P:
  154. case AV_PIX_FMT_YUVJ422P:
  155. case AV_PIX_FMT_YUVJ440P:
  156. case AV_PIX_FMT_YUVJ444P:
  157. case AV_PIX_FMT_YUVA420P:
  158. case AV_PIX_FMT_YUVA422P:
  159. case AV_PIX_FMT_YUVA444P:
  160. case AV_PIX_FMT_YUV420P9LE:
  161. case AV_PIX_FMT_YUV420P9BE:
  162. case AV_PIX_FMT_YUV420P10LE:
  163. case AV_PIX_FMT_YUV420P10BE:
  164. case AV_PIX_FMT_YUV422P9LE:
  165. case AV_PIX_FMT_YUV422P9BE:
  166. case AV_PIX_FMT_YUV422P10LE:
  167. case AV_PIX_FMT_YUV422P10BE:
  168. case AV_PIX_FMT_YUV444P9LE:
  169. case AV_PIX_FMT_YUV444P9BE:
  170. case AV_PIX_FMT_YUV444P10LE:
  171. case AV_PIX_FMT_YUV444P10BE:
  172. case AV_PIX_FMT_GBRP9LE:
  173. case AV_PIX_FMT_GBRP9BE:
  174. case AV_PIX_FMT_GBRP10LE:
  175. case AV_PIX_FMT_GBRP10BE:
  176. w_align = 16; //FIXME assume 16 pixel per macroblock
  177. h_align = 16 * 2; // interlaced needs 2 macroblocks height
  178. break;
  179. case AV_PIX_FMT_YUV411P:
  180. case AV_PIX_FMT_UYYVYY411:
  181. w_align = 32;
  182. h_align = 8;
  183. break;
  184. case AV_PIX_FMT_YUV410P:
  185. if (s->codec_id == AV_CODEC_ID_SVQ1) {
  186. w_align = 64;
  187. h_align = 64;
  188. }
  189. case AV_PIX_FMT_RGB555:
  190. if (s->codec_id == AV_CODEC_ID_RPZA) {
  191. w_align = 4;
  192. h_align = 4;
  193. }
  194. case AV_PIX_FMT_PAL8:
  195. case AV_PIX_FMT_BGR8:
  196. case AV_PIX_FMT_RGB8:
  197. if (s->codec_id == AV_CODEC_ID_SMC) {
  198. w_align = 4;
  199. h_align = 4;
  200. }
  201. break;
  202. case AV_PIX_FMT_BGR24:
  203. if ((s->codec_id == AV_CODEC_ID_MSZH) ||
  204. (s->codec_id == AV_CODEC_ID_ZLIB)) {
  205. w_align = 4;
  206. h_align = 4;
  207. }
  208. break;
  209. default:
  210. w_align = 1;
  211. h_align = 1;
  212. break;
  213. }
  214. *width = FFALIGN(*width, w_align);
  215. *height = FFALIGN(*height, h_align);
  216. if (s->codec_id == AV_CODEC_ID_H264)
  217. // some of the optimized chroma MC reads one line too much
  218. *height += 2;
  219. for (i = 0; i < 4; i++)
  220. linesize_align[i] = STRIDE_ALIGN;
  221. }
  222. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height)
  223. {
  224. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  225. int chroma_shift = desc->log2_chroma_w;
  226. int linesize_align[AV_NUM_DATA_POINTERS];
  227. int align;
  228. avcodec_align_dimensions2(s, width, height, linesize_align);
  229. align = FFMAX(linesize_align[0], linesize_align[3]);
  230. linesize_align[1] <<= chroma_shift;
  231. linesize_align[2] <<= chroma_shift;
  232. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  233. *width = FFALIGN(*width, align);
  234. }
  235. int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
  236. enum AVSampleFormat sample_fmt, const uint8_t *buf,
  237. int buf_size, int align)
  238. {
  239. int ch, planar, needed_size, ret = 0;
  240. needed_size = av_samples_get_buffer_size(NULL, nb_channels,
  241. frame->nb_samples, sample_fmt,
  242. align);
  243. if (buf_size < needed_size)
  244. return AVERROR(EINVAL);
  245. planar = av_sample_fmt_is_planar(sample_fmt);
  246. if (planar && nb_channels > AV_NUM_DATA_POINTERS) {
  247. if (!(frame->extended_data = av_mallocz(nb_channels *
  248. sizeof(*frame->extended_data))))
  249. return AVERROR(ENOMEM);
  250. } else {
  251. frame->extended_data = frame->data;
  252. }
  253. if ((ret = av_samples_fill_arrays(frame->extended_data, &frame->linesize[0],
  254. buf, nb_channels, frame->nb_samples,
  255. sample_fmt, align)) < 0) {
  256. if (frame->extended_data != frame->data)
  257. av_free(frame->extended_data);
  258. return ret;
  259. }
  260. if (frame->extended_data != frame->data) {
  261. for (ch = 0; ch < AV_NUM_DATA_POINTERS; ch++)
  262. frame->data[ch] = frame->extended_data[ch];
  263. }
  264. return ret;
  265. }
  266. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  267. {
  268. AVCodecInternal *avci = avctx->internal;
  269. InternalBuffer *buf;
  270. int buf_size, ret;
  271. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  272. frame->nb_samples, avctx->sample_fmt,
  273. 0);
  274. if (buf_size < 0)
  275. return AVERROR(EINVAL);
  276. /* allocate InternalBuffer if needed */
  277. if (!avci->buffer) {
  278. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  279. if (!avci->buffer)
  280. return AVERROR(ENOMEM);
  281. }
  282. buf = avci->buffer;
  283. /* if there is a previously-used internal buffer, check its size and
  284. * channel count to see if we can reuse it */
  285. if (buf->extended_data) {
  286. /* if current buffer is too small, free it */
  287. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  288. av_free(buf->extended_data[0]);
  289. if (buf->extended_data != buf->data)
  290. av_free(buf->extended_data);
  291. buf->extended_data = NULL;
  292. buf->data[0] = NULL;
  293. }
  294. /* if number of channels has changed, reset and/or free extended data
  295. * pointers but leave data buffer in buf->data[0] for reuse */
  296. if (buf->nb_channels != avctx->channels) {
  297. if (buf->extended_data != buf->data)
  298. av_free(buf->extended_data);
  299. buf->extended_data = NULL;
  300. }
  301. }
  302. /* if there is no previous buffer or the previous buffer cannot be used
  303. * as-is, allocate a new buffer and/or rearrange the channel pointers */
  304. if (!buf->extended_data) {
  305. if (!buf->data[0]) {
  306. if (!(buf->data[0] = av_mallocz(buf_size)))
  307. return AVERROR(ENOMEM);
  308. buf->audio_data_size = buf_size;
  309. }
  310. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  311. avctx->sample_fmt, buf->data[0],
  312. buf->audio_data_size, 0)))
  313. return ret;
  314. if (frame->extended_data == frame->data)
  315. buf->extended_data = buf->data;
  316. else
  317. buf->extended_data = frame->extended_data;
  318. memcpy(buf->data, frame->data, sizeof(frame->data));
  319. buf->linesize[0] = frame->linesize[0];
  320. buf->nb_channels = avctx->channels;
  321. } else {
  322. /* copy InternalBuffer info to the AVFrame */
  323. frame->extended_data = buf->extended_data;
  324. frame->linesize[0] = buf->linesize[0];
  325. memcpy(frame->data, buf->data, sizeof(frame->data));
  326. }
  327. frame->type = FF_BUFFER_TYPE_INTERNAL;
  328. if (avctx->pkt)
  329. frame->pkt_pts = avctx->pkt->pts;
  330. else
  331. frame->pkt_pts = AV_NOPTS_VALUE;
  332. frame->reordered_opaque = avctx->reordered_opaque;
  333. frame->sample_rate = avctx->sample_rate;
  334. frame->format = avctx->sample_fmt;
  335. frame->channel_layout = avctx->channel_layout;
  336. if (avctx->debug & FF_DEBUG_BUFFERS)
  337. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  338. "internal audio buffer used\n", frame);
  339. return 0;
  340. }
  341. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  342. {
  343. int i;
  344. int w = s->width;
  345. int h = s->height;
  346. InternalBuffer *buf;
  347. AVCodecInternal *avci = s->internal;
  348. if (pic->data[0] != NULL) {
  349. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  350. return -1;
  351. }
  352. if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  353. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  354. return -1;
  355. }
  356. if (av_image_check_size(w, h, 0, s))
  357. return -1;
  358. if (!avci->buffer) {
  359. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
  360. sizeof(InternalBuffer));
  361. }
  362. buf = &avci->buffer[avci->buffer_count];
  363. if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
  364. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  365. av_freep(&buf->base[i]);
  366. buf->data[i] = NULL;
  367. }
  368. }
  369. if (!buf->base[0]) {
  370. int h_chroma_shift, v_chroma_shift;
  371. int size[4] = { 0 };
  372. int tmpsize;
  373. int unaligned;
  374. AVPicture picture;
  375. int stride_align[AV_NUM_DATA_POINTERS];
  376. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
  377. const int pixel_size = desc->comp[0].step_minus1 + 1;
  378. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  379. avcodec_align_dimensions2(s, &w, &h, stride_align);
  380. if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
  381. w += EDGE_WIDTH * 2;
  382. h += EDGE_WIDTH * 2;
  383. }
  384. do {
  385. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  386. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  387. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  388. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  389. w += w & ~(w - 1);
  390. unaligned = 0;
  391. for (i = 0; i < 4; i++)
  392. unaligned |= picture.linesize[i] % stride_align[i];
  393. } while (unaligned);
  394. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  395. if (tmpsize < 0)
  396. return -1;
  397. for (i = 0; i < 3 && picture.data[i + 1]; i++)
  398. size[i] = picture.data[i + 1] - picture.data[i];
  399. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  400. memset(buf->base, 0, sizeof(buf->base));
  401. memset(buf->data, 0, sizeof(buf->data));
  402. for (i = 0; i < 4 && size[i]; i++) {
  403. const int h_shift = i == 0 ? 0 : h_chroma_shift;
  404. const int v_shift = i == 0 ? 0 : v_chroma_shift;
  405. buf->linesize[i] = picture.linesize[i];
  406. buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
  407. if (buf->base[i] == NULL)
  408. return -1;
  409. memset(buf->base[i], 128, size[i]);
  410. // no edge if EDGE EMU or not planar YUV
  411. if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
  412. buf->data[i] = buf->base[i];
  413. else
  414. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
  415. }
  416. for (; i < AV_NUM_DATA_POINTERS; i++) {
  417. buf->base[i] = buf->data[i] = NULL;
  418. buf->linesize[i] = 0;
  419. }
  420. if (size[1] && !size[2])
  421. avpriv_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
  422. buf->width = s->width;
  423. buf->height = s->height;
  424. buf->pix_fmt = s->pix_fmt;
  425. }
  426. pic->type = FF_BUFFER_TYPE_INTERNAL;
  427. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  428. pic->base[i] = buf->base[i];
  429. pic->data[i] = buf->data[i];
  430. pic->linesize[i] = buf->linesize[i];
  431. }
  432. pic->extended_data = pic->data;
  433. avci->buffer_count++;
  434. pic->width = buf->width;
  435. pic->height = buf->height;
  436. pic->format = buf->pix_fmt;
  437. pic->sample_aspect_ratio = s->sample_aspect_ratio;
  438. if (s->pkt)
  439. pic->pkt_pts = s->pkt->pts;
  440. else
  441. pic->pkt_pts = AV_NOPTS_VALUE;
  442. pic->reordered_opaque = s->reordered_opaque;
  443. if (s->debug & FF_DEBUG_BUFFERS)
  444. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  445. "buffers used\n", pic, avci->buffer_count);
  446. return 0;
  447. }
  448. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  449. {
  450. switch (avctx->codec_type) {
  451. case AVMEDIA_TYPE_VIDEO:
  452. return video_get_buffer(avctx, frame);
  453. case AVMEDIA_TYPE_AUDIO:
  454. return audio_get_buffer(avctx, frame);
  455. default:
  456. return -1;
  457. }
  458. }
  459. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
  460. {
  461. int i;
  462. InternalBuffer *buf, *last;
  463. AVCodecInternal *avci = s->internal;
  464. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  465. assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
  466. assert(avci->buffer_count);
  467. if (avci->buffer) {
  468. buf = NULL; /* avoids warning */
  469. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  470. buf = &avci->buffer[i];
  471. if (buf->data[0] == pic->data[0])
  472. break;
  473. }
  474. assert(i < avci->buffer_count);
  475. avci->buffer_count--;
  476. last = &avci->buffer[avci->buffer_count];
  477. if (buf != last)
  478. FFSWAP(InternalBuffer, *buf, *last);
  479. }
  480. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  481. pic->data[i] = NULL;
  482. // pic->base[i]=NULL;
  483. if (s->debug & FF_DEBUG_BUFFERS)
  484. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  485. "buffers used\n", pic, avci->buffer_count);
  486. }
  487. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
  488. {
  489. AVFrame temp_pic;
  490. int i;
  491. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  492. /* If no picture return a new buffer */
  493. if (pic->data[0] == NULL) {
  494. /* We will copy from buffer, so must be readable */
  495. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  496. return s->get_buffer(s, pic);
  497. }
  498. assert(s->pix_fmt == pic->format);
  499. /* If internal buffer type return the same buffer */
  500. if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
  501. if (s->pkt)
  502. pic->pkt_pts = s->pkt->pts;
  503. else
  504. pic->pkt_pts = AV_NOPTS_VALUE;
  505. pic->reordered_opaque = s->reordered_opaque;
  506. return 0;
  507. }
  508. /*
  509. * Not internal type and reget_buffer not overridden, emulate cr buffer
  510. */
  511. temp_pic = *pic;
  512. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  513. pic->data[i] = pic->base[i] = NULL;
  514. pic->opaque = NULL;
  515. /* Allocate new frame */
  516. if (s->get_buffer(s, pic))
  517. return -1;
  518. /* Copy image data from old buffer to new buffer */
  519. av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
  520. s->height);
  521. s->release_buffer(s, &temp_pic); // Release old frame
  522. return 0;
  523. }
  524. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
  525. {
  526. int i;
  527. for (i = 0; i < count; i++) {
  528. int r = func(c, (char *)arg + i * size);
  529. if (ret)
  530. ret[i] = r;
  531. }
  532. return 0;
  533. }
  534. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr), void *arg, int *ret, int count)
  535. {
  536. int i;
  537. for (i = 0; i < count; i++) {
  538. int r = func(c, arg, i, 0);
  539. if (ret)
  540. ret[i] = r;
  541. }
  542. return 0;
  543. }
  544. enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
  545. {
  546. while (*fmt != AV_PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  547. ++fmt;
  548. return fmt[0];
  549. }
  550. void avcodec_get_frame_defaults(AVFrame *frame)
  551. {
  552. if (frame->extended_data != frame->data)
  553. av_freep(&frame->extended_data);
  554. memset(frame, 0, sizeof(AVFrame));
  555. frame->pts = AV_NOPTS_VALUE;
  556. frame->key_frame = 1;
  557. frame->sample_aspect_ratio = (AVRational) {0, 1 };
  558. frame->format = -1; /* unknown */
  559. frame->extended_data = frame->data;
  560. }
  561. AVFrame *avcodec_alloc_frame(void)
  562. {
  563. AVFrame *frame = av_mallocz(sizeof(AVFrame));
  564. if (frame == NULL)
  565. return NULL;
  566. avcodec_get_frame_defaults(frame);
  567. return frame;
  568. }
  569. void avcodec_free_frame(AVFrame **frame)
  570. {
  571. AVFrame *f;
  572. if (!frame || !*frame)
  573. return;
  574. f = *frame;
  575. if (f->extended_data != f->data)
  576. av_freep(&f->extended_data);
  577. av_freep(frame);
  578. }
  579. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  580. {
  581. int ret = 0;
  582. AVDictionary *tmp = NULL;
  583. if (avcodec_is_open(avctx))
  584. return 0;
  585. if ((!codec && !avctx->codec)) {
  586. av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
  587. return AVERROR(EINVAL);
  588. }
  589. if ((codec && avctx->codec && codec != avctx->codec)) {
  590. av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
  591. "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
  592. return AVERROR(EINVAL);
  593. }
  594. if (!codec)
  595. codec = avctx->codec;
  596. if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
  597. return AVERROR(EINVAL);
  598. if (options)
  599. av_dict_copy(&tmp, *options, 0);
  600. /* If there is a user-supplied mutex locking routine, call it. */
  601. if (ff_lockmgr_cb) {
  602. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  603. return -1;
  604. }
  605. entangled_thread_counter++;
  606. if (entangled_thread_counter != 1) {
  607. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  608. ret = -1;
  609. goto end;
  610. }
  611. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  612. if (!avctx->internal) {
  613. ret = AVERROR(ENOMEM);
  614. goto end;
  615. }
  616. if (codec->priv_data_size > 0) {
  617. if (!avctx->priv_data) {
  618. avctx->priv_data = av_mallocz(codec->priv_data_size);
  619. if (!avctx->priv_data) {
  620. ret = AVERROR(ENOMEM);
  621. goto end;
  622. }
  623. if (codec->priv_class) {
  624. *(const AVClass **)avctx->priv_data = codec->priv_class;
  625. av_opt_set_defaults(avctx->priv_data);
  626. }
  627. }
  628. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  629. goto free_and_end;
  630. } else {
  631. avctx->priv_data = NULL;
  632. }
  633. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  634. goto free_and_end;
  635. if (avctx->coded_width && avctx->coded_height)
  636. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  637. else if (avctx->width && avctx->height)
  638. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  639. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  640. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  641. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  642. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  643. avcodec_set_dimensions(avctx, 0, 0);
  644. }
  645. /* if the decoder init function was already called previously,
  646. * free the already allocated subtitle_header before overwriting it */
  647. if (av_codec_is_decoder(codec))
  648. av_freep(&avctx->subtitle_header);
  649. if (avctx->channels > FF_SANE_NB_CHANNELS) {
  650. ret = AVERROR(EINVAL);
  651. goto free_and_end;
  652. }
  653. avctx->codec = codec;
  654. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  655. avctx->codec_id == AV_CODEC_ID_NONE) {
  656. avctx->codec_type = codec->type;
  657. avctx->codec_id = codec->id;
  658. }
  659. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  660. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  661. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  662. ret = AVERROR(EINVAL);
  663. goto free_and_end;
  664. }
  665. avctx->frame_number = 0;
  666. if (avctx->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  667. avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  668. ret = AVERROR_EXPERIMENTAL;
  669. goto free_and_end;
  670. }
  671. if (avctx->codec_type == AVMEDIA_TYPE_AUDIO &&
  672. (!avctx->time_base.num || !avctx->time_base.den)) {
  673. avctx->time_base.num = 1;
  674. avctx->time_base.den = avctx->sample_rate;
  675. }
  676. if (HAVE_THREADS && !avctx->thread_opaque) {
  677. ret = ff_thread_init(avctx);
  678. if (ret < 0) {
  679. goto free_and_end;
  680. }
  681. }
  682. if (!HAVE_THREADS && !(codec->capabilities & CODEC_CAP_AUTO_THREADS))
  683. avctx->thread_count = 1;
  684. if (av_codec_is_encoder(avctx->codec)) {
  685. int i;
  686. if (avctx->codec->sample_fmts) {
  687. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
  688. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  689. break;
  690. if (avctx->channels == 1 &&
  691. av_get_planar_sample_fmt(avctx->sample_fmt) ==
  692. av_get_planar_sample_fmt(avctx->codec->sample_fmts[i])) {
  693. avctx->sample_fmt = avctx->codec->sample_fmts[i];
  694. break;
  695. }
  696. }
  697. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  698. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  699. ret = AVERROR(EINVAL);
  700. goto free_and_end;
  701. }
  702. }
  703. if (avctx->codec->pix_fmts) {
  704. for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
  705. if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
  706. break;
  707. if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
  708. av_log(avctx, AV_LOG_ERROR, "Specified pix_fmt is not supported\n");
  709. ret = AVERROR(EINVAL);
  710. goto free_and_end;
  711. }
  712. }
  713. if (avctx->codec->supported_samplerates) {
  714. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  715. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  716. break;
  717. if (avctx->codec->supported_samplerates[i] == 0) {
  718. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  719. ret = AVERROR(EINVAL);
  720. goto free_and_end;
  721. }
  722. }
  723. if (avctx->codec->channel_layouts) {
  724. if (!avctx->channel_layout) {
  725. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  726. } else {
  727. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  728. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  729. break;
  730. if (avctx->codec->channel_layouts[i] == 0) {
  731. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  732. ret = AVERROR(EINVAL);
  733. goto free_and_end;
  734. }
  735. }
  736. }
  737. if (avctx->channel_layout && avctx->channels) {
  738. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  739. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  740. ret = AVERROR(EINVAL);
  741. goto free_and_end;
  742. }
  743. } else if (avctx->channel_layout) {
  744. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  745. }
  746. }
  747. if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  748. ret = avctx->codec->init(avctx);
  749. if (ret < 0) {
  750. goto free_and_end;
  751. }
  752. }
  753. if (av_codec_is_decoder(avctx->codec)) {
  754. /* validate channel layout from the decoder */
  755. if (avctx->channel_layout) {
  756. int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  757. if (!avctx->channels)
  758. avctx->channels = channels;
  759. else if (channels != avctx->channels) {
  760. av_log(avctx, AV_LOG_WARNING,
  761. "channel layout does not match number of channels\n");
  762. avctx->channel_layout = 0;
  763. }
  764. }
  765. if (avctx->channels && avctx->channels < 0 ||
  766. avctx->channels > FF_SANE_NB_CHANNELS) {
  767. ret = AVERROR(EINVAL);
  768. goto free_and_end;
  769. }
  770. }
  771. end:
  772. entangled_thread_counter--;
  773. /* Release any user-supplied mutex. */
  774. if (ff_lockmgr_cb) {
  775. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  776. }
  777. if (options) {
  778. av_dict_free(options);
  779. *options = tmp;
  780. }
  781. return ret;
  782. free_and_end:
  783. av_dict_free(&tmp);
  784. av_freep(&avctx->priv_data);
  785. av_freep(&avctx->internal);
  786. avctx->codec = NULL;
  787. goto end;
  788. }
  789. int ff_alloc_packet(AVPacket *avpkt, int size)
  790. {
  791. if (size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  792. return AVERROR(EINVAL);
  793. if (avpkt->data) {
  794. void *destruct = avpkt->destruct;
  795. if (avpkt->size < size)
  796. return AVERROR(EINVAL);
  797. av_init_packet(avpkt);
  798. avpkt->destruct = destruct;
  799. avpkt->size = size;
  800. return 0;
  801. } else {
  802. return av_new_packet(avpkt, size);
  803. }
  804. }
  805. /**
  806. * Pad last frame with silence.
  807. */
  808. static int pad_last_frame(AVCodecContext *s, AVFrame **dst, const AVFrame *src)
  809. {
  810. AVFrame *frame = NULL;
  811. uint8_t *buf = NULL;
  812. int ret;
  813. if (!(frame = avcodec_alloc_frame()))
  814. return AVERROR(ENOMEM);
  815. *frame = *src;
  816. if ((ret = av_samples_get_buffer_size(&frame->linesize[0], s->channels,
  817. s->frame_size, s->sample_fmt, 0)) < 0)
  818. goto fail;
  819. if (!(buf = av_malloc(ret))) {
  820. ret = AVERROR(ENOMEM);
  821. goto fail;
  822. }
  823. frame->nb_samples = s->frame_size;
  824. if ((ret = avcodec_fill_audio_frame(frame, s->channels, s->sample_fmt,
  825. buf, ret, 0)) < 0)
  826. goto fail;
  827. if ((ret = av_samples_copy(frame->extended_data, src->extended_data, 0, 0,
  828. src->nb_samples, s->channels, s->sample_fmt)) < 0)
  829. goto fail;
  830. if ((ret = av_samples_set_silence(frame->extended_data, src->nb_samples,
  831. frame->nb_samples - src->nb_samples,
  832. s->channels, s->sample_fmt)) < 0)
  833. goto fail;
  834. *dst = frame;
  835. return 0;
  836. fail:
  837. if (frame->extended_data != frame->data)
  838. av_freep(&frame->extended_data);
  839. av_freep(&buf);
  840. av_freep(&frame);
  841. return ret;
  842. }
  843. int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
  844. AVPacket *avpkt,
  845. const AVFrame *frame,
  846. int *got_packet_ptr)
  847. {
  848. AVFrame tmp;
  849. AVFrame *padded_frame = NULL;
  850. int ret;
  851. int user_packet = !!avpkt->data;
  852. *got_packet_ptr = 0;
  853. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  854. av_free_packet(avpkt);
  855. av_init_packet(avpkt);
  856. return 0;
  857. }
  858. /* ensure that extended_data is properly set */
  859. if (frame && !frame->extended_data) {
  860. if (av_sample_fmt_is_planar(avctx->sample_fmt) &&
  861. avctx->channels > AV_NUM_DATA_POINTERS) {
  862. av_log(avctx, AV_LOG_ERROR, "Encoding to a planar sample format, "
  863. "with more than %d channels, but extended_data is not set.\n",
  864. AV_NUM_DATA_POINTERS);
  865. return AVERROR(EINVAL);
  866. }
  867. av_log(avctx, AV_LOG_WARNING, "extended_data is not set.\n");
  868. tmp = *frame;
  869. tmp.extended_data = tmp.data;
  870. frame = &tmp;
  871. }
  872. /* check for valid frame size */
  873. if (frame) {
  874. if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
  875. if (frame->nb_samples > avctx->frame_size)
  876. return AVERROR(EINVAL);
  877. } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {
  878. if (frame->nb_samples < avctx->frame_size &&
  879. !avctx->internal->last_audio_frame) {
  880. ret = pad_last_frame(avctx, &padded_frame, frame);
  881. if (ret < 0)
  882. return ret;
  883. frame = padded_frame;
  884. avctx->internal->last_audio_frame = 1;
  885. }
  886. if (frame->nb_samples != avctx->frame_size) {
  887. ret = AVERROR(EINVAL);
  888. goto end;
  889. }
  890. }
  891. }
  892. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  893. if (!ret) {
  894. if (*got_packet_ptr) {
  895. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
  896. if (avpkt->pts == AV_NOPTS_VALUE)
  897. avpkt->pts = frame->pts;
  898. if (!avpkt->duration)
  899. avpkt->duration = ff_samples_to_time_base(avctx,
  900. frame->nb_samples);
  901. }
  902. avpkt->dts = avpkt->pts;
  903. } else {
  904. avpkt->size = 0;
  905. }
  906. if (!user_packet && avpkt->size) {
  907. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
  908. if (new_data)
  909. avpkt->data = new_data;
  910. }
  911. avctx->frame_number++;
  912. }
  913. if (ret < 0 || !*got_packet_ptr) {
  914. av_free_packet(avpkt);
  915. av_init_packet(avpkt);
  916. goto end;
  917. }
  918. /* NOTE: if we add any audio encoders which output non-keyframe packets,
  919. * this needs to be moved to the encoders, but for now we can do it
  920. * here to simplify things */
  921. avpkt->flags |= AV_PKT_FLAG_KEY;
  922. end:
  923. if (padded_frame) {
  924. av_freep(&padded_frame->data[0]);
  925. if (padded_frame->extended_data != padded_frame->data)
  926. av_freep(&padded_frame->extended_data);
  927. av_freep(&padded_frame);
  928. }
  929. return ret;
  930. }
  931. #if FF_API_OLD_ENCODE_AUDIO
  932. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
  933. uint8_t *buf, int buf_size,
  934. const short *samples)
  935. {
  936. AVPacket pkt;
  937. AVFrame frame0 = { 0 };
  938. AVFrame *frame;
  939. int ret, samples_size, got_packet;
  940. av_init_packet(&pkt);
  941. pkt.data = buf;
  942. pkt.size = buf_size;
  943. if (samples) {
  944. frame = &frame0;
  945. avcodec_get_frame_defaults(frame);
  946. if (avctx->frame_size) {
  947. frame->nb_samples = avctx->frame_size;
  948. } else {
  949. /* if frame_size is not set, the number of samples must be
  950. * calculated from the buffer size */
  951. int64_t nb_samples;
  952. if (!av_get_bits_per_sample(avctx->codec_id)) {
  953. av_log(avctx, AV_LOG_ERROR, "avcodec_encode_audio() does not "
  954. "support this codec\n");
  955. return AVERROR(EINVAL);
  956. }
  957. nb_samples = (int64_t)buf_size * 8 /
  958. (av_get_bits_per_sample(avctx->codec_id) *
  959. avctx->channels);
  960. if (nb_samples >= INT_MAX)
  961. return AVERROR(EINVAL);
  962. frame->nb_samples = nb_samples;
  963. }
  964. /* it is assumed that the samples buffer is large enough based on the
  965. * relevant parameters */
  966. samples_size = av_samples_get_buffer_size(NULL, avctx->channels,
  967. frame->nb_samples,
  968. avctx->sample_fmt, 1);
  969. if ((ret = avcodec_fill_audio_frame(frame, avctx->channels,
  970. avctx->sample_fmt,
  971. (const uint8_t *)samples,
  972. samples_size, 1)))
  973. return ret;
  974. /* fabricate frame pts from sample count.
  975. * this is needed because the avcodec_encode_audio() API does not have
  976. * a way for the user to provide pts */
  977. frame->pts = ff_samples_to_time_base(avctx,
  978. avctx->internal->sample_count);
  979. avctx->internal->sample_count += frame->nb_samples;
  980. } else {
  981. frame = NULL;
  982. }
  983. got_packet = 0;
  984. ret = avcodec_encode_audio2(avctx, &pkt, frame, &got_packet);
  985. if (!ret && got_packet && avctx->coded_frame) {
  986. avctx->coded_frame->pts = pkt.pts;
  987. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  988. }
  989. /* free any side data since we cannot return it */
  990. if (pkt.side_data_elems > 0) {
  991. int i;
  992. for (i = 0; i < pkt.side_data_elems; i++)
  993. av_free(pkt.side_data[i].data);
  994. av_freep(&pkt.side_data);
  995. pkt.side_data_elems = 0;
  996. }
  997. if (frame && frame->extended_data != frame->data)
  998. av_free(frame->extended_data);
  999. return ret ? ret : pkt.size;
  1000. }
  1001. #endif
  1002. #if FF_API_OLD_ENCODE_VIDEO
  1003. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1004. const AVFrame *pict)
  1005. {
  1006. AVPacket pkt;
  1007. int ret, got_packet = 0;
  1008. if (buf_size < FF_MIN_BUFFER_SIZE) {
  1009. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  1010. return -1;
  1011. }
  1012. av_init_packet(&pkt);
  1013. pkt.data = buf;
  1014. pkt.size = buf_size;
  1015. ret = avcodec_encode_video2(avctx, &pkt, pict, &got_packet);
  1016. if (!ret && got_packet && avctx->coded_frame) {
  1017. avctx->coded_frame->pts = pkt.pts;
  1018. avctx->coded_frame->key_frame = !!(pkt.flags & AV_PKT_FLAG_KEY);
  1019. }
  1020. /* free any side data since we cannot return it */
  1021. if (pkt.side_data_elems > 0) {
  1022. int i;
  1023. for (i = 0; i < pkt.side_data_elems; i++)
  1024. av_free(pkt.side_data[i].data);
  1025. av_freep(&pkt.side_data);
  1026. pkt.side_data_elems = 0;
  1027. }
  1028. return ret ? ret : pkt.size;
  1029. }
  1030. #endif
  1031. int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
  1032. AVPacket *avpkt,
  1033. const AVFrame *frame,
  1034. int *got_packet_ptr)
  1035. {
  1036. int ret;
  1037. int user_packet = !!avpkt->data;
  1038. *got_packet_ptr = 0;
  1039. if (!(avctx->codec->capabilities & CODEC_CAP_DELAY) && !frame) {
  1040. av_free_packet(avpkt);
  1041. av_init_packet(avpkt);
  1042. avpkt->size = 0;
  1043. return 0;
  1044. }
  1045. if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
  1046. return AVERROR(EINVAL);
  1047. av_assert0(avctx->codec->encode2);
  1048. ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
  1049. if (!ret) {
  1050. if (!*got_packet_ptr)
  1051. avpkt->size = 0;
  1052. else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
  1053. avpkt->pts = avpkt->dts = frame->pts;
  1054. if (!user_packet && avpkt->size) {
  1055. uint8_t *new_data = av_realloc(avpkt->data, avpkt->size);
  1056. if (new_data)
  1057. avpkt->data = new_data;
  1058. }
  1059. avctx->frame_number++;
  1060. }
  1061. if (ret < 0 || !*got_packet_ptr)
  1062. av_free_packet(avpkt);
  1063. emms_c();
  1064. return ret;
  1065. }
  1066. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  1067. const AVSubtitle *sub)
  1068. {
  1069. int ret;
  1070. if (sub->start_display_time) {
  1071. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  1072. return -1;
  1073. }
  1074. if (sub->num_rects == 0 || !sub->rects)
  1075. return -1;
  1076. ret = avctx->codec->encode_sub(avctx, buf, buf_size, sub);
  1077. avctx->frame_number++;
  1078. return ret;
  1079. }
  1080. static void apply_param_change(AVCodecContext *avctx, AVPacket *avpkt)
  1081. {
  1082. int size = 0;
  1083. const uint8_t *data;
  1084. uint32_t flags;
  1085. if (!(avctx->codec->capabilities & CODEC_CAP_PARAM_CHANGE))
  1086. return;
  1087. data = av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, &size);
  1088. if (!data || size < 4)
  1089. return;
  1090. flags = bytestream_get_le32(&data);
  1091. size -= 4;
  1092. if (size < 4) /* Required for any of the changes */
  1093. return;
  1094. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
  1095. avctx->channels = bytestream_get_le32(&data);
  1096. size -= 4;
  1097. }
  1098. if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
  1099. if (size < 8)
  1100. return;
  1101. avctx->channel_layout = bytestream_get_le64(&data);
  1102. size -= 8;
  1103. }
  1104. if (size < 4)
  1105. return;
  1106. if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
  1107. avctx->sample_rate = bytestream_get_le32(&data);
  1108. size -= 4;
  1109. }
  1110. if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
  1111. if (size < 8)
  1112. return;
  1113. avctx->width = bytestream_get_le32(&data);
  1114. avctx->height = bytestream_get_le32(&data);
  1115. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  1116. size -= 8;
  1117. }
  1118. }
  1119. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  1120. int *got_picture_ptr,
  1121. AVPacket *avpkt)
  1122. {
  1123. int ret;
  1124. *got_picture_ptr = 0;
  1125. if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1126. return -1;
  1127. avctx->pkt = avpkt;
  1128. apply_param_change(avctx, avpkt);
  1129. avcodec_get_frame_defaults(picture);
  1130. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
  1131. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1132. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  1133. avpkt);
  1134. else {
  1135. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1136. avpkt);
  1137. picture->pkt_dts = avpkt->dts;
  1138. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  1139. picture->width = avctx->width;
  1140. picture->height = avctx->height;
  1141. picture->format = avctx->pix_fmt;
  1142. }
  1143. emms_c(); //needed to avoid an emms_c() call before every return;
  1144. if (*got_picture_ptr)
  1145. avctx->frame_number++;
  1146. } else
  1147. ret = 0;
  1148. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1149. * make sure it's set correctly */
  1150. picture->extended_data = picture->data;
  1151. return ret;
  1152. }
  1153. #if FF_API_OLD_DECODE_AUDIO
  1154. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  1155. int *frame_size_ptr,
  1156. AVPacket *avpkt)
  1157. {
  1158. AVFrame frame = {0};
  1159. int ret, got_frame = 0;
  1160. if (avctx->get_buffer != avcodec_default_get_buffer) {
  1161. av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
  1162. "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
  1163. av_log(avctx, AV_LOG_ERROR, "Please port your application to "
  1164. "avcodec_decode_audio4()\n");
  1165. avctx->get_buffer = avcodec_default_get_buffer;
  1166. }
  1167. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  1168. if (ret >= 0 && got_frame) {
  1169. int ch, plane_size;
  1170. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  1171. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  1172. frame.nb_samples,
  1173. avctx->sample_fmt, 1);
  1174. if (*frame_size_ptr < data_size) {
  1175. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  1176. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  1177. return AVERROR(EINVAL);
  1178. }
  1179. memcpy(samples, frame.extended_data[0], plane_size);
  1180. if (planar && avctx->channels > 1) {
  1181. uint8_t *out = ((uint8_t *)samples) + plane_size;
  1182. for (ch = 1; ch < avctx->channels; ch++) {
  1183. memcpy(out, frame.extended_data[ch], plane_size);
  1184. out += plane_size;
  1185. }
  1186. }
  1187. *frame_size_ptr = data_size;
  1188. } else {
  1189. *frame_size_ptr = 0;
  1190. }
  1191. return ret;
  1192. }
  1193. #endif
  1194. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  1195. AVFrame *frame,
  1196. int *got_frame_ptr,
  1197. AVPacket *avpkt)
  1198. {
  1199. int planar, channels;
  1200. int ret = 0;
  1201. *got_frame_ptr = 0;
  1202. avctx->pkt = avpkt;
  1203. if (!avpkt->data && avpkt->size) {
  1204. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  1205. return AVERROR(EINVAL);
  1206. }
  1207. apply_param_change(avctx, avpkt);
  1208. avcodec_get_frame_defaults(frame);
  1209. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  1210. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  1211. if (ret >= 0 && *got_frame_ptr) {
  1212. avctx->frame_number++;
  1213. frame->pkt_dts = avpkt->dts;
  1214. if (frame->format == AV_SAMPLE_FMT_NONE)
  1215. frame->format = avctx->sample_fmt;
  1216. }
  1217. }
  1218. /* many decoders assign whole AVFrames, thus overwriting extended_data;
  1219. * make sure it's set correctly; assume decoders that actually use
  1220. * extended_data are doing it correctly */
  1221. planar = av_sample_fmt_is_planar(frame->format);
  1222. channels = av_get_channel_layout_nb_channels(frame->channel_layout);
  1223. if (!(planar && channels > AV_NUM_DATA_POINTERS))
  1224. frame->extended_data = frame->data;
  1225. return ret;
  1226. }
  1227. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  1228. int *got_sub_ptr,
  1229. AVPacket *avpkt)
  1230. {
  1231. int ret;
  1232. avctx->pkt = avpkt;
  1233. *got_sub_ptr = 0;
  1234. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  1235. if (*got_sub_ptr)
  1236. avctx->frame_number++;
  1237. return ret;
  1238. }
  1239. void avsubtitle_free(AVSubtitle *sub)
  1240. {
  1241. int i;
  1242. for (i = 0; i < sub->num_rects; i++) {
  1243. av_freep(&sub->rects[i]->pict.data[0]);
  1244. av_freep(&sub->rects[i]->pict.data[1]);
  1245. av_freep(&sub->rects[i]->pict.data[2]);
  1246. av_freep(&sub->rects[i]->pict.data[3]);
  1247. av_freep(&sub->rects[i]->text);
  1248. av_freep(&sub->rects[i]->ass);
  1249. av_freep(&sub->rects[i]);
  1250. }
  1251. av_freep(&sub->rects);
  1252. memset(sub, 0, sizeof(AVSubtitle));
  1253. }
  1254. av_cold int avcodec_close(AVCodecContext *avctx)
  1255. {
  1256. /* If there is a user-supplied mutex locking routine, call it. */
  1257. if (ff_lockmgr_cb) {
  1258. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  1259. return -1;
  1260. }
  1261. entangled_thread_counter++;
  1262. if (entangled_thread_counter != 1) {
  1263. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  1264. entangled_thread_counter--;
  1265. return -1;
  1266. }
  1267. if (avcodec_is_open(avctx)) {
  1268. if (HAVE_THREADS && avctx->thread_opaque)
  1269. ff_thread_free(avctx);
  1270. if (avctx->codec && avctx->codec->close)
  1271. avctx->codec->close(avctx);
  1272. avcodec_default_free_buffers(avctx);
  1273. avctx->coded_frame = NULL;
  1274. av_freep(&avctx->internal);
  1275. }
  1276. if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
  1277. av_opt_free(avctx->priv_data);
  1278. av_opt_free(avctx);
  1279. av_freep(&avctx->priv_data);
  1280. if (av_codec_is_encoder(avctx->codec))
  1281. av_freep(&avctx->extradata);
  1282. avctx->codec = NULL;
  1283. avctx->active_thread_type = 0;
  1284. entangled_thread_counter--;
  1285. /* Release any user-supplied mutex. */
  1286. if (ff_lockmgr_cb) {
  1287. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  1288. }
  1289. return 0;
  1290. }
  1291. static AVCodec *find_encdec(enum AVCodecID id, int encoder)
  1292. {
  1293. AVCodec *p, *experimental = NULL;
  1294. p = first_avcodec;
  1295. while (p) {
  1296. if ((encoder ? av_codec_is_encoder(p) : av_codec_is_decoder(p)) &&
  1297. p->id == id) {
  1298. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  1299. experimental = p;
  1300. } else
  1301. return p;
  1302. }
  1303. p = p->next;
  1304. }
  1305. return experimental;
  1306. }
  1307. AVCodec *avcodec_find_encoder(enum AVCodecID id)
  1308. {
  1309. return find_encdec(id, 1);
  1310. }
  1311. AVCodec *avcodec_find_encoder_by_name(const char *name)
  1312. {
  1313. AVCodec *p;
  1314. if (!name)
  1315. return NULL;
  1316. p = first_avcodec;
  1317. while (p) {
  1318. if (av_codec_is_encoder(p) && strcmp(name, p->name) == 0)
  1319. return p;
  1320. p = p->next;
  1321. }
  1322. return NULL;
  1323. }
  1324. AVCodec *avcodec_find_decoder(enum AVCodecID id)
  1325. {
  1326. return find_encdec(id, 0);
  1327. }
  1328. AVCodec *avcodec_find_decoder_by_name(const char *name)
  1329. {
  1330. AVCodec *p;
  1331. if (!name)
  1332. return NULL;
  1333. p = first_avcodec;
  1334. while (p) {
  1335. if (av_codec_is_decoder(p) && strcmp(name, p->name) == 0)
  1336. return p;
  1337. p = p->next;
  1338. }
  1339. return NULL;
  1340. }
  1341. static int get_bit_rate(AVCodecContext *ctx)
  1342. {
  1343. int bit_rate;
  1344. int bits_per_sample;
  1345. switch (ctx->codec_type) {
  1346. case AVMEDIA_TYPE_VIDEO:
  1347. case AVMEDIA_TYPE_DATA:
  1348. case AVMEDIA_TYPE_SUBTITLE:
  1349. case AVMEDIA_TYPE_ATTACHMENT:
  1350. bit_rate = ctx->bit_rate;
  1351. break;
  1352. case AVMEDIA_TYPE_AUDIO:
  1353. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  1354. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  1355. break;
  1356. default:
  1357. bit_rate = 0;
  1358. break;
  1359. }
  1360. return bit_rate;
  1361. }
  1362. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  1363. {
  1364. int i, len, ret = 0;
  1365. for (i = 0; i < 4; i++) {
  1366. len = snprintf(buf, buf_size,
  1367. isprint(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
  1368. buf += len;
  1369. buf_size = buf_size > len ? buf_size - len : 0;
  1370. ret += len;
  1371. codec_tag >>= 8;
  1372. }
  1373. return ret;
  1374. }
  1375. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  1376. {
  1377. const char *codec_name;
  1378. const char *profile = NULL;
  1379. const AVCodec *p;
  1380. char buf1[32];
  1381. int bitrate;
  1382. AVRational display_aspect_ratio;
  1383. if (enc->codec)
  1384. p = enc->codec;
  1385. else if (encode)
  1386. p = avcodec_find_encoder(enc->codec_id);
  1387. else
  1388. p = avcodec_find_decoder(enc->codec_id);
  1389. if (p) {
  1390. codec_name = p->name;
  1391. profile = av_get_profile_name(p, enc->profile);
  1392. } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
  1393. /* fake mpeg2 transport stream codec (currently not
  1394. * registered) */
  1395. codec_name = "mpeg2ts";
  1396. } else if (enc->codec_name[0] != '\0') {
  1397. codec_name = enc->codec_name;
  1398. } else {
  1399. /* output avi tags */
  1400. char tag_buf[32];
  1401. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1402. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1403. codec_name = buf1;
  1404. }
  1405. switch (enc->codec_type) {
  1406. case AVMEDIA_TYPE_VIDEO:
  1407. snprintf(buf, buf_size,
  1408. "Video: %s%s",
  1409. codec_name, enc->mb_decision ? " (hq)" : "");
  1410. if (profile)
  1411. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1412. " (%s)", profile);
  1413. if (enc->pix_fmt != AV_PIX_FMT_NONE) {
  1414. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1415. ", %s",
  1416. av_get_pix_fmt_name(enc->pix_fmt));
  1417. }
  1418. if (enc->width) {
  1419. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1420. ", %dx%d",
  1421. enc->width, enc->height);
  1422. if (enc->sample_aspect_ratio.num) {
  1423. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1424. enc->width * enc->sample_aspect_ratio.num,
  1425. enc->height * enc->sample_aspect_ratio.den,
  1426. 1024 * 1024);
  1427. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1428. " [PAR %d:%d DAR %d:%d]",
  1429. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1430. display_aspect_ratio.num, display_aspect_ratio.den);
  1431. }
  1432. if (av_log_get_level() >= AV_LOG_DEBUG) {
  1433. int g = av_gcd(enc->time_base.num, enc->time_base.den);
  1434. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1435. ", %d/%d",
  1436. enc->time_base.num / g, enc->time_base.den / g);
  1437. }
  1438. }
  1439. if (encode) {
  1440. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1441. ", q=%d-%d", enc->qmin, enc->qmax);
  1442. }
  1443. break;
  1444. case AVMEDIA_TYPE_AUDIO:
  1445. snprintf(buf, buf_size,
  1446. "Audio: %s",
  1447. codec_name);
  1448. if (profile)
  1449. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1450. " (%s)", profile);
  1451. if (enc->sample_rate) {
  1452. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1453. ", %d Hz", enc->sample_rate);
  1454. }
  1455. av_strlcat(buf, ", ", buf_size);
  1456. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1457. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1458. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1459. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1460. }
  1461. break;
  1462. case AVMEDIA_TYPE_DATA:
  1463. snprintf(buf, buf_size, "Data: %s", codec_name);
  1464. break;
  1465. case AVMEDIA_TYPE_SUBTITLE:
  1466. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1467. break;
  1468. case AVMEDIA_TYPE_ATTACHMENT:
  1469. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1470. break;
  1471. default:
  1472. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1473. return;
  1474. }
  1475. if (encode) {
  1476. if (enc->flags & CODEC_FLAG_PASS1)
  1477. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1478. ", pass 1");
  1479. if (enc->flags & CODEC_FLAG_PASS2)
  1480. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1481. ", pass 2");
  1482. }
  1483. bitrate = get_bit_rate(enc);
  1484. if (bitrate != 0) {
  1485. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1486. ", %d kb/s", bitrate / 1000);
  1487. }
  1488. }
  1489. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1490. {
  1491. const AVProfile *p;
  1492. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1493. return NULL;
  1494. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1495. if (p->profile == profile)
  1496. return p->name;
  1497. return NULL;
  1498. }
  1499. unsigned avcodec_version(void)
  1500. {
  1501. return LIBAVCODEC_VERSION_INT;
  1502. }
  1503. const char *avcodec_configuration(void)
  1504. {
  1505. return LIBAV_CONFIGURATION;
  1506. }
  1507. const char *avcodec_license(void)
  1508. {
  1509. #define LICENSE_PREFIX "libavcodec license: "
  1510. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1511. }
  1512. void avcodec_flush_buffers(AVCodecContext *avctx)
  1513. {
  1514. if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
  1515. ff_thread_flush(avctx);
  1516. else if (avctx->codec->flush)
  1517. avctx->codec->flush(avctx);
  1518. }
  1519. static void video_free_buffers(AVCodecContext *s)
  1520. {
  1521. AVCodecInternal *avci = s->internal;
  1522. int i, j;
  1523. if (!avci->buffer)
  1524. return;
  1525. if (avci->buffer_count)
  1526. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1527. avci->buffer_count);
  1528. for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
  1529. InternalBuffer *buf = &avci->buffer[i];
  1530. for (j = 0; j < 4; j++) {
  1531. av_freep(&buf->base[j]);
  1532. buf->data[j] = NULL;
  1533. }
  1534. }
  1535. av_freep(&avci->buffer);
  1536. avci->buffer_count = 0;
  1537. }
  1538. static void audio_free_buffers(AVCodecContext *avctx)
  1539. {
  1540. AVCodecInternal *avci = avctx->internal;
  1541. InternalBuffer *buf;
  1542. if (!avci->buffer)
  1543. return;
  1544. buf = avci->buffer;
  1545. if (buf->extended_data) {
  1546. av_free(buf->extended_data[0]);
  1547. if (buf->extended_data != buf->data)
  1548. av_free(buf->extended_data);
  1549. }
  1550. av_freep(&avci->buffer);
  1551. }
  1552. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1553. {
  1554. switch (avctx->codec_type) {
  1555. case AVMEDIA_TYPE_VIDEO:
  1556. video_free_buffers(avctx);
  1557. break;
  1558. case AVMEDIA_TYPE_AUDIO:
  1559. audio_free_buffers(avctx);
  1560. break;
  1561. default:
  1562. break;
  1563. }
  1564. }
  1565. int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
  1566. {
  1567. switch (codec_id) {
  1568. case AV_CODEC_ID_ADPCM_CT:
  1569. case AV_CODEC_ID_ADPCM_IMA_APC:
  1570. case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
  1571. case AV_CODEC_ID_ADPCM_IMA_WS:
  1572. case AV_CODEC_ID_ADPCM_G722:
  1573. case AV_CODEC_ID_ADPCM_YAMAHA:
  1574. return 4;
  1575. case AV_CODEC_ID_PCM_ALAW:
  1576. case AV_CODEC_ID_PCM_MULAW:
  1577. case AV_CODEC_ID_PCM_S8:
  1578. case AV_CODEC_ID_PCM_U8:
  1579. case AV_CODEC_ID_PCM_ZORK:
  1580. return 8;
  1581. case AV_CODEC_ID_PCM_S16BE:
  1582. case AV_CODEC_ID_PCM_S16LE:
  1583. case AV_CODEC_ID_PCM_S16LE_PLANAR:
  1584. case AV_CODEC_ID_PCM_U16BE:
  1585. case AV_CODEC_ID_PCM_U16LE:
  1586. return 16;
  1587. case AV_CODEC_ID_PCM_S24DAUD:
  1588. case AV_CODEC_ID_PCM_S24BE:
  1589. case AV_CODEC_ID_PCM_S24LE:
  1590. case AV_CODEC_ID_PCM_U24BE:
  1591. case AV_CODEC_ID_PCM_U24LE:
  1592. return 24;
  1593. case AV_CODEC_ID_PCM_S32BE:
  1594. case AV_CODEC_ID_PCM_S32LE:
  1595. case AV_CODEC_ID_PCM_U32BE:
  1596. case AV_CODEC_ID_PCM_U32LE:
  1597. case AV_CODEC_ID_PCM_F32BE:
  1598. case AV_CODEC_ID_PCM_F32LE:
  1599. return 32;
  1600. case AV_CODEC_ID_PCM_F64BE:
  1601. case AV_CODEC_ID_PCM_F64LE:
  1602. return 64;
  1603. default:
  1604. return 0;
  1605. }
  1606. }
  1607. int av_get_bits_per_sample(enum AVCodecID codec_id)
  1608. {
  1609. switch (codec_id) {
  1610. case AV_CODEC_ID_ADPCM_SBPRO_2:
  1611. return 2;
  1612. case AV_CODEC_ID_ADPCM_SBPRO_3:
  1613. return 3;
  1614. case AV_CODEC_ID_ADPCM_SBPRO_4:
  1615. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1616. case AV_CODEC_ID_ADPCM_IMA_QT:
  1617. case AV_CODEC_ID_ADPCM_SWF:
  1618. case AV_CODEC_ID_ADPCM_MS:
  1619. return 4;
  1620. default:
  1621. return av_get_exact_bits_per_sample(codec_id);
  1622. }
  1623. }
  1624. int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
  1625. {
  1626. int id, sr, ch, ba, tag, bps;
  1627. id = avctx->codec_id;
  1628. sr = avctx->sample_rate;
  1629. ch = avctx->channels;
  1630. ba = avctx->block_align;
  1631. tag = avctx->codec_tag;
  1632. bps = av_get_exact_bits_per_sample(avctx->codec_id);
  1633. /* codecs with an exact constant bits per sample */
  1634. if (bps > 0 && ch > 0 && frame_bytes > 0)
  1635. return (frame_bytes * 8) / (bps * ch);
  1636. bps = avctx->bits_per_coded_sample;
  1637. /* codecs with a fixed packet duration */
  1638. switch (id) {
  1639. case AV_CODEC_ID_ADPCM_ADX: return 32;
  1640. case AV_CODEC_ID_ADPCM_IMA_QT: return 64;
  1641. case AV_CODEC_ID_ADPCM_EA_XAS: return 128;
  1642. case AV_CODEC_ID_AMR_NB:
  1643. case AV_CODEC_ID_GSM:
  1644. case AV_CODEC_ID_QCELP:
  1645. case AV_CODEC_ID_RA_144:
  1646. case AV_CODEC_ID_RA_288: return 160;
  1647. case AV_CODEC_ID_IMC: return 256;
  1648. case AV_CODEC_ID_AMR_WB:
  1649. case AV_CODEC_ID_GSM_MS: return 320;
  1650. case AV_CODEC_ID_MP1: return 384;
  1651. case AV_CODEC_ID_ATRAC1: return 512;
  1652. case AV_CODEC_ID_ATRAC3: return 1024;
  1653. case AV_CODEC_ID_MP2:
  1654. case AV_CODEC_ID_MUSEPACK7: return 1152;
  1655. case AV_CODEC_ID_AC3: return 1536;
  1656. }
  1657. if (sr > 0) {
  1658. /* calc from sample rate */
  1659. if (id == AV_CODEC_ID_TTA)
  1660. return 256 * sr / 245;
  1661. if (ch > 0) {
  1662. /* calc from sample rate and channels */
  1663. if (id == AV_CODEC_ID_BINKAUDIO_DCT)
  1664. return (480 << (sr / 22050)) / ch;
  1665. }
  1666. }
  1667. if (ba > 0) {
  1668. /* calc from block_align */
  1669. if (id == AV_CODEC_ID_SIPR) {
  1670. switch (ba) {
  1671. case 20: return 160;
  1672. case 19: return 144;
  1673. case 29: return 288;
  1674. case 37: return 480;
  1675. }
  1676. } else if (id == AV_CODEC_ID_ILBC) {
  1677. switch (ba) {
  1678. case 38: return 160;
  1679. case 50: return 240;
  1680. }
  1681. }
  1682. }
  1683. if (frame_bytes > 0) {
  1684. /* calc from frame_bytes only */
  1685. if (id == AV_CODEC_ID_TRUESPEECH)
  1686. return 240 * (frame_bytes / 32);
  1687. if (id == AV_CODEC_ID_NELLYMOSER)
  1688. return 256 * (frame_bytes / 64);
  1689. if (bps > 0) {
  1690. /* calc from frame_bytes and bits_per_coded_sample */
  1691. if (id == AV_CODEC_ID_ADPCM_G726)
  1692. return frame_bytes * 8 / bps;
  1693. }
  1694. if (ch > 0) {
  1695. /* calc from frame_bytes and channels */
  1696. switch (id) {
  1697. case AV_CODEC_ID_ADPCM_4XM:
  1698. case AV_CODEC_ID_ADPCM_IMA_ISS:
  1699. return (frame_bytes - 4 * ch) * 2 / ch;
  1700. case AV_CODEC_ID_ADPCM_IMA_SMJPEG:
  1701. return (frame_bytes - 4) * 2 / ch;
  1702. case AV_CODEC_ID_ADPCM_IMA_AMV:
  1703. return (frame_bytes - 8) * 2 / ch;
  1704. case AV_CODEC_ID_ADPCM_XA:
  1705. return (frame_bytes / 128) * 224 / ch;
  1706. case AV_CODEC_ID_INTERPLAY_DPCM:
  1707. return (frame_bytes - 6 - ch) / ch;
  1708. case AV_CODEC_ID_ROQ_DPCM:
  1709. return (frame_bytes - 8) / ch;
  1710. case AV_CODEC_ID_XAN_DPCM:
  1711. return (frame_bytes - 2 * ch) / ch;
  1712. case AV_CODEC_ID_MACE3:
  1713. return 3 * frame_bytes / ch;
  1714. case AV_CODEC_ID_MACE6:
  1715. return 6 * frame_bytes / ch;
  1716. case AV_CODEC_ID_PCM_LXF:
  1717. return 2 * (frame_bytes / (5 * ch));
  1718. }
  1719. if (tag) {
  1720. /* calc from frame_bytes, channels, and codec_tag */
  1721. if (id == AV_CODEC_ID_SOL_DPCM) {
  1722. if (tag == 3)
  1723. return frame_bytes / ch;
  1724. else
  1725. return frame_bytes * 2 / ch;
  1726. }
  1727. }
  1728. if (ba > 0) {
  1729. /* calc from frame_bytes, channels, and block_align */
  1730. int blocks = frame_bytes / ba;
  1731. switch (avctx->codec_id) {
  1732. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1733. return blocks * (1 + (ba - 4 * ch) / (4 * ch) * 8);
  1734. case AV_CODEC_ID_ADPCM_IMA_DK3:
  1735. return blocks * (((ba - 16) * 2 / 3 * 4) / ch);
  1736. case AV_CODEC_ID_ADPCM_IMA_DK4:
  1737. return blocks * (1 + (ba - 4 * ch) * 2 / ch);
  1738. case AV_CODEC_ID_ADPCM_MS:
  1739. return blocks * (2 + (ba - 7 * ch) * 2 / ch);
  1740. }
  1741. }
  1742. if (bps > 0) {
  1743. /* calc from frame_bytes, channels, and bits_per_coded_sample */
  1744. switch (avctx->codec_id) {
  1745. case AV_CODEC_ID_PCM_DVD:
  1746. return 2 * (frame_bytes / ((bps * 2 / 8) * ch));
  1747. case AV_CODEC_ID_PCM_BLURAY:
  1748. return frame_bytes / ((FFALIGN(ch, 2) * bps) / 8);
  1749. case AV_CODEC_ID_S302M:
  1750. return 2 * (frame_bytes / ((bps + 4) / 4)) / ch;
  1751. }
  1752. }
  1753. }
  1754. }
  1755. return 0;
  1756. }
  1757. #if !HAVE_THREADS
  1758. int ff_thread_init(AVCodecContext *s)
  1759. {
  1760. return -1;
  1761. }
  1762. #endif
  1763. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1764. {
  1765. unsigned int n = 0;
  1766. while (v >= 0xff) {
  1767. *s++ = 0xff;
  1768. v -= 0xff;
  1769. n++;
  1770. }
  1771. *s = v;
  1772. n++;
  1773. return n;
  1774. }
  1775. int ff_match_2uint16(const uint16_t(*tab)[2], int size, int a, int b)
  1776. {
  1777. int i;
  1778. for (i = 0; i < size && !(tab[i][0] == a && tab[i][1] == b); i++) ;
  1779. return i;
  1780. }
  1781. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1782. {
  1783. av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your Libav "
  1784. "version to the newest one from Git. If the problem still "
  1785. "occurs, it means that your file has a feature which has not "
  1786. "been implemented.\n", feature);
  1787. if(want_sample)
  1788. av_log_ask_for_sample(avc, NULL);
  1789. }
  1790. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1791. {
  1792. va_list argument_list;
  1793. va_start(argument_list, msg);
  1794. if (msg)
  1795. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1796. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1797. "of this file to ftp://upload.libav.org/incoming/ "
  1798. "and contact the libav-devel mailing list.\n");
  1799. va_end(argument_list);
  1800. }
  1801. static AVHWAccel *first_hwaccel = NULL;
  1802. void av_register_hwaccel(AVHWAccel *hwaccel)
  1803. {
  1804. AVHWAccel **p = &first_hwaccel;
  1805. while (*p)
  1806. p = &(*p)->next;
  1807. *p = hwaccel;
  1808. hwaccel->next = NULL;
  1809. }
  1810. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1811. {
  1812. return hwaccel ? hwaccel->next : first_hwaccel;
  1813. }
  1814. AVHWAccel *ff_find_hwaccel(enum AVCodecID codec_id, enum AVPixelFormat pix_fmt)
  1815. {
  1816. AVHWAccel *hwaccel = NULL;
  1817. while ((hwaccel = av_hwaccel_next(hwaccel)))
  1818. if (hwaccel->id == codec_id
  1819. && hwaccel->pix_fmt == pix_fmt)
  1820. return hwaccel;
  1821. return NULL;
  1822. }
  1823. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1824. {
  1825. if (ff_lockmgr_cb) {
  1826. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1827. return -1;
  1828. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1829. return -1;
  1830. }
  1831. ff_lockmgr_cb = cb;
  1832. if (ff_lockmgr_cb) {
  1833. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1834. return -1;
  1835. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1836. return -1;
  1837. }
  1838. return 0;
  1839. }
  1840. int avpriv_lock_avformat(void)
  1841. {
  1842. if (ff_lockmgr_cb) {
  1843. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1844. return -1;
  1845. }
  1846. return 0;
  1847. }
  1848. int avpriv_unlock_avformat(void)
  1849. {
  1850. if (ff_lockmgr_cb) {
  1851. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1852. return -1;
  1853. }
  1854. return 0;
  1855. }
  1856. unsigned int avpriv_toupper4(unsigned int x)
  1857. {
  1858. return toupper(x & 0xFF)
  1859. + (toupper((x >> 8) & 0xFF) << 8)
  1860. + (toupper((x >> 16) & 0xFF) << 16)
  1861. + (toupper((x >> 24) & 0xFF) << 24);
  1862. }
  1863. #if !HAVE_THREADS
  1864. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1865. {
  1866. f->owner = avctx;
  1867. return avctx->get_buffer(avctx, f);
  1868. }
  1869. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1870. {
  1871. f->owner->release_buffer(f->owner, f);
  1872. }
  1873. void ff_thread_finish_setup(AVCodecContext *avctx)
  1874. {
  1875. }
  1876. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1877. {
  1878. }
  1879. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1880. {
  1881. }
  1882. #endif
  1883. enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
  1884. {
  1885. if (codec_id <= AV_CODEC_ID_NONE)
  1886. return AVMEDIA_TYPE_UNKNOWN;
  1887. else if (codec_id < AV_CODEC_ID_FIRST_AUDIO)
  1888. return AVMEDIA_TYPE_VIDEO;
  1889. else if (codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
  1890. return AVMEDIA_TYPE_AUDIO;
  1891. else if (codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
  1892. return AVMEDIA_TYPE_SUBTITLE;
  1893. return AVMEDIA_TYPE_UNKNOWN;
  1894. }
  1895. int avcodec_is_open(AVCodecContext *s)
  1896. {
  1897. return !!s->internal;
  1898. }