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.

1909 lines
58KB

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