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.

2498 lines
78KB

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