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.

2434 lines
75KB

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