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.

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