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.

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