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.

2456 lines
76KB

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