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.

1559 lines
46KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * utils.
  25. */
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/crc.h"
  28. #include "libavutil/mathematics.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/audioconvert.h"
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/samplefmt.h"
  33. #include "libavutil/dict.h"
  34. #include "avcodec.h"
  35. #include "dsputil.h"
  36. #include "libavutil/opt.h"
  37. #include "imgconvert.h"
  38. #include "thread.h"
  39. #include "audioconvert.h"
  40. #include "internal.h"
  41. #include <stdlib.h>
  42. #include <stdarg.h>
  43. #include <limits.h>
  44. #include <float.h>
  45. static int volatile entangled_thread_counter=0;
  46. static int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  47. static void *codec_mutex;
  48. static void *avformat_mutex;
  49. void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
  50. {
  51. if(min_size < *size)
  52. return ptr;
  53. min_size= FFMAX(17*min_size/16 + 32, min_size);
  54. ptr= av_realloc(ptr, min_size);
  55. 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
  56. min_size= 0;
  57. *size= min_size;
  58. return ptr;
  59. }
  60. void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
  61. {
  62. void **p = ptr;
  63. if (min_size < *size)
  64. return;
  65. min_size= FFMAX(17*min_size/16 + 32, min_size);
  66. av_free(*p);
  67. *p = av_malloc(min_size);
  68. if (!*p) min_size = 0;
  69. *size= min_size;
  70. }
  71. /* encoder management */
  72. static AVCodec *first_avcodec = NULL;
  73. AVCodec *av_codec_next(AVCodec *c){
  74. if(c) return c->next;
  75. else return first_avcodec;
  76. }
  77. #if !FF_API_AVCODEC_INIT
  78. static
  79. #endif
  80. void avcodec_init(void)
  81. {
  82. static int initialized = 0;
  83. if (initialized != 0)
  84. return;
  85. initialized = 1;
  86. dsputil_static_init();
  87. }
  88. void avcodec_register(AVCodec *codec)
  89. {
  90. AVCodec **p;
  91. avcodec_init();
  92. p = &first_avcodec;
  93. while (*p != NULL) p = &(*p)->next;
  94. *p = codec;
  95. codec->next = NULL;
  96. if (codec->init_static_data)
  97. codec->init_static_data(codec);
  98. }
  99. unsigned avcodec_get_edge_width(void)
  100. {
  101. return EDGE_WIDTH;
  102. }
  103. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  104. s->coded_width = width;
  105. s->coded_height= height;
  106. s->width = -((-width )>>s->lowres);
  107. s->height= -((-height)>>s->lowres);
  108. }
  109. #define INTERNAL_BUFFER_SIZE (32+1)
  110. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
  111. int linesize_align[AV_NUM_DATA_POINTERS])
  112. {
  113. int i;
  114. int w_align= 1;
  115. int h_align= 1;
  116. switch(s->pix_fmt){
  117. case PIX_FMT_YUV420P:
  118. case PIX_FMT_YUYV422:
  119. case PIX_FMT_UYVY422:
  120. case PIX_FMT_YUV422P:
  121. case PIX_FMT_YUV440P:
  122. case PIX_FMT_YUV444P:
  123. case PIX_FMT_GBRP:
  124. case PIX_FMT_GRAY8:
  125. case PIX_FMT_GRAY16BE:
  126. case PIX_FMT_GRAY16LE:
  127. case PIX_FMT_YUVJ420P:
  128. case PIX_FMT_YUVJ422P:
  129. case PIX_FMT_YUVJ440P:
  130. case PIX_FMT_YUVJ444P:
  131. case PIX_FMT_YUVA420P:
  132. case PIX_FMT_YUV420P9LE:
  133. case PIX_FMT_YUV420P9BE:
  134. case PIX_FMT_YUV420P10LE:
  135. case PIX_FMT_YUV420P10BE:
  136. case PIX_FMT_YUV422P9LE:
  137. case PIX_FMT_YUV422P9BE:
  138. case PIX_FMT_YUV422P10LE:
  139. case PIX_FMT_YUV422P10BE:
  140. case PIX_FMT_YUV444P9LE:
  141. case PIX_FMT_YUV444P9BE:
  142. case PIX_FMT_YUV444P10LE:
  143. case PIX_FMT_YUV444P10BE:
  144. case PIX_FMT_GBRP9LE:
  145. case PIX_FMT_GBRP9BE:
  146. case PIX_FMT_GBRP10LE:
  147. case PIX_FMT_GBRP10BE:
  148. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  149. h_align= 16;
  150. if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP || s->codec_id == CODEC_ID_H264)
  151. h_align= 32; // interlaced is rounded up to 2 MBs
  152. break;
  153. case PIX_FMT_YUV411P:
  154. case PIX_FMT_UYYVYY411:
  155. w_align=32;
  156. h_align=8;
  157. break;
  158. case PIX_FMT_YUV410P:
  159. if(s->codec_id == CODEC_ID_SVQ1){
  160. w_align=64;
  161. h_align=64;
  162. }
  163. case PIX_FMT_RGB555:
  164. if(s->codec_id == CODEC_ID_RPZA){
  165. w_align=4;
  166. h_align=4;
  167. }
  168. case PIX_FMT_PAL8:
  169. case PIX_FMT_BGR8:
  170. case PIX_FMT_RGB8:
  171. if(s->codec_id == CODEC_ID_SMC){
  172. w_align=4;
  173. h_align=4;
  174. }
  175. break;
  176. case PIX_FMT_BGR24:
  177. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  178. w_align=4;
  179. h_align=4;
  180. }
  181. break;
  182. default:
  183. w_align= 1;
  184. h_align= 1;
  185. break;
  186. }
  187. *width = FFALIGN(*width , w_align);
  188. *height= FFALIGN(*height, h_align);
  189. if(s->codec_id == CODEC_ID_H264 || s->lowres)
  190. *height+=2; // some of the optimized chroma MC reads one line too much
  191. // which is also done in mpeg decoders with lowres > 0
  192. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  193. linesize_align[i] = STRIDE_ALIGN;
  194. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  195. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  196. //picture size unneccessarily in some cases. The solution here is not
  197. //pretty and better ideas are welcome!
  198. #if HAVE_MMX
  199. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  200. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  201. s->codec_id == CODEC_ID_VP6A) {
  202. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  203. linesize_align[i] = 16;
  204. }
  205. #endif
  206. }
  207. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  208. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  209. int linesize_align[AV_NUM_DATA_POINTERS];
  210. int align;
  211. avcodec_align_dimensions2(s, width, height, linesize_align);
  212. align = FFMAX(linesize_align[0], linesize_align[3]);
  213. linesize_align[1] <<= chroma_shift;
  214. linesize_align[2] <<= chroma_shift;
  215. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  216. *width=FFALIGN(*width, align);
  217. }
  218. static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  219. {
  220. AVCodecInternal *avci = avctx->internal;
  221. InternalBuffer *buf;
  222. int buf_size, ret, i, needs_extended_data;
  223. buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
  224. frame->nb_samples, avctx->sample_fmt,
  225. 32);
  226. if (buf_size < 0)
  227. return AVERROR(EINVAL);
  228. needs_extended_data = av_sample_fmt_is_planar(avctx->sample_fmt) &&
  229. avctx->channels > AV_NUM_DATA_POINTERS;
  230. /* allocate InternalBuffer if needed */
  231. if (!avci->buffer) {
  232. avci->buffer = av_mallocz(sizeof(InternalBuffer));
  233. if (!avci->buffer)
  234. return AVERROR(ENOMEM);
  235. }
  236. buf = avci->buffer;
  237. /* if there is a previously-used internal buffer, check its size and
  238. channel count to see if we can reuse it */
  239. if (buf->extended_data) {
  240. /* if current buffer is too small, free it */
  241. if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
  242. av_free(buf->extended_data[0]);
  243. if (buf->extended_data != buf->data)
  244. av_free(&buf->extended_data);
  245. buf->extended_data = NULL;
  246. buf->data[0] = NULL;
  247. }
  248. /* if number of channels has changed, reset and/or free extended data
  249. pointers but leave data buffer in buf->data[0] for reuse */
  250. if (buf->nb_channels != avctx->channels) {
  251. if (buf->extended_data != buf->data)
  252. av_free(buf->extended_data);
  253. buf->extended_data = NULL;
  254. }
  255. }
  256. /* if there is no previous buffer or the previous buffer cannot be used
  257. as-is, allocate a new buffer and/or rearrange the channel pointers */
  258. if (!buf->extended_data) {
  259. /* if the channel pointers will fit, just set extended_data to data,
  260. otherwise allocate the extended_data channel pointers */
  261. if (needs_extended_data) {
  262. buf->extended_data = av_mallocz(avctx->channels *
  263. sizeof(*buf->extended_data));
  264. if (!buf->extended_data)
  265. return AVERROR(ENOMEM);
  266. } else {
  267. buf->extended_data = buf->data;
  268. }
  269. /* if there is a previous buffer and it is large enough, reuse it and
  270. just fill-in new channel pointers and linesize, otherwise allocate
  271. a new buffer */
  272. if (buf->extended_data[0]) {
  273. ret = av_samples_fill_arrays(buf->extended_data, &buf->linesize[0],
  274. buf->extended_data[0], avctx->channels,
  275. frame->nb_samples, avctx->sample_fmt,
  276. 32);
  277. } else {
  278. ret = av_samples_alloc(buf->extended_data, &buf->linesize[0],
  279. avctx->channels, frame->nb_samples,
  280. avctx->sample_fmt, 32);
  281. }
  282. if (ret)
  283. return ret;
  284. /* if data was not used for extended_data, we need to copy as many of
  285. the extended_data channel pointers as will fit */
  286. if (needs_extended_data) {
  287. for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
  288. buf->data[i] = buf->extended_data[i];
  289. }
  290. buf->audio_data_size = buf_size;
  291. buf->nb_channels = avctx->channels;
  292. }
  293. /* copy InternalBuffer info to the AVFrame */
  294. frame->type = FF_BUFFER_TYPE_INTERNAL;
  295. frame->extended_data = buf->extended_data;
  296. frame->linesize[0] = buf->linesize[0];
  297. memcpy(frame->data, buf->data, sizeof(frame->data));
  298. if (avctx->pkt) frame->pkt_pts = avctx->pkt->pts;
  299. else frame->pkt_pts = AV_NOPTS_VALUE;
  300. frame->reordered_opaque = avctx->reordered_opaque;
  301. if (avctx->debug & FF_DEBUG_BUFFERS)
  302. av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
  303. "internal audio buffer used\n", frame);
  304. return 0;
  305. }
  306. static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
  307. {
  308. int i;
  309. int w= s->width;
  310. int h= s->height;
  311. InternalBuffer *buf;
  312. int *picture_number;
  313. AVCodecInternal *avci = s->internal;
  314. if(pic->data[0]!=NULL) {
  315. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  316. return -1;
  317. }
  318. if(avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
  319. av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
  320. return -1;
  321. }
  322. if(av_image_check_size(w, h, 0, s))
  323. return -1;
  324. if (!avci->buffer) {
  325. avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
  326. sizeof(InternalBuffer));
  327. }
  328. buf = &avci->buffer[avci->buffer_count];
  329. picture_number = &(avci->buffer[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  330. (*picture_number)++;
  331. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  332. if(s->active_thread_type&FF_THREAD_FRAME) {
  333. av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  334. return -1;
  335. }
  336. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  337. av_freep(&buf->base[i]);
  338. buf->data[i]= NULL;
  339. }
  340. }
  341. if(buf->base[0]){
  342. pic->age= *picture_number - buf->last_pic_num;
  343. buf->last_pic_num= *picture_number;
  344. }else{
  345. int h_chroma_shift, v_chroma_shift;
  346. int size[4] = {0};
  347. int tmpsize;
  348. int unaligned;
  349. AVPicture picture;
  350. int stride_align[AV_NUM_DATA_POINTERS];
  351. const int pixel_size = av_pix_fmt_descriptors[s->pix_fmt].comp[0].step_minus1+1;
  352. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  353. avcodec_align_dimensions2(s, &w, &h, stride_align);
  354. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  355. w+= EDGE_WIDTH*2;
  356. h+= EDGE_WIDTH*2;
  357. }
  358. do {
  359. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  360. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  361. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  362. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  363. w += w & ~(w-1);
  364. unaligned = 0;
  365. for (i=0; i<4; i++){
  366. unaligned |= picture.linesize[i] % stride_align[i];
  367. }
  368. } while (unaligned);
  369. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  370. if (tmpsize < 0)
  371. return -1;
  372. for (i=0; i<3 && picture.data[i+1]; i++)
  373. size[i] = picture.data[i+1] - picture.data[i];
  374. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  375. buf->last_pic_num= -256*256*256*64;
  376. memset(buf->base, 0, sizeof(buf->base));
  377. memset(buf->data, 0, sizeof(buf->data));
  378. for(i=0; i<4 && size[i]; i++){
  379. const int h_shift= i==0 ? 0 : h_chroma_shift;
  380. const int v_shift= i==0 ? 0 : v_chroma_shift;
  381. buf->linesize[i]= picture.linesize[i];
  382. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  383. if(buf->base[i]==NULL) return -1;
  384. memset(buf->base[i], 128, size[i]);
  385. // no edge if EDGE EMU or not planar YUV
  386. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  387. buf->data[i] = buf->base[i];
  388. else
  389. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (pixel_size*EDGE_WIDTH>>h_shift), stride_align[i]);
  390. }
  391. for (; i < AV_NUM_DATA_POINTERS; i++) {
  392. buf->base[i] = buf->data[i] = NULL;
  393. buf->linesize[i] = 0;
  394. }
  395. if(size[1] && !size[2])
  396. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  397. buf->width = s->width;
  398. buf->height = s->height;
  399. buf->pix_fmt= s->pix_fmt;
  400. pic->age= 256*256*256*64;
  401. }
  402. pic->type= FF_BUFFER_TYPE_INTERNAL;
  403. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  404. pic->base[i]= buf->base[i];
  405. pic->data[i]= buf->data[i];
  406. pic->linesize[i]= buf->linesize[i];
  407. }
  408. pic->extended_data = pic->data;
  409. avci->buffer_count++;
  410. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  411. else pic->pkt_pts= AV_NOPTS_VALUE;
  412. pic->reordered_opaque= s->reordered_opaque;
  413. if(s->debug&FF_DEBUG_BUFFERS)
  414. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
  415. "buffers used\n", pic, avci->buffer_count);
  416. return 0;
  417. }
  418. int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
  419. {
  420. switch (avctx->codec_type) {
  421. case AVMEDIA_TYPE_VIDEO:
  422. return video_get_buffer(avctx, frame);
  423. case AVMEDIA_TYPE_AUDIO:
  424. return audio_get_buffer(avctx, frame);
  425. default:
  426. return -1;
  427. }
  428. }
  429. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  430. int i;
  431. InternalBuffer *buf, *last;
  432. AVCodecInternal *avci = s->internal;
  433. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  434. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  435. assert(avci->buffer_count);
  436. if (avci->buffer) {
  437. buf = NULL; /* avoids warning */
  438. for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
  439. buf = &avci->buffer[i];
  440. if (buf->data[0] == pic->data[0])
  441. break;
  442. }
  443. assert(i < avci->buffer_count);
  444. avci->buffer_count--;
  445. last = &avci->buffer[avci->buffer_count];
  446. FFSWAP(InternalBuffer, *buf, *last);
  447. }
  448. for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
  449. pic->data[i]=NULL;
  450. // pic->base[i]=NULL;
  451. }
  452. //printf("R%X\n", pic->opaque);
  453. if(s->debug&FF_DEBUG_BUFFERS)
  454. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
  455. "buffers used\n", pic, avci->buffer_count);
  456. }
  457. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  458. AVFrame temp_pic;
  459. int i;
  460. assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
  461. /* If no picture return a new buffer */
  462. if(pic->data[0] == NULL) {
  463. /* We will copy from buffer, so must be readable */
  464. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  465. return s->get_buffer(s, pic);
  466. }
  467. /* If internal buffer type return the same buffer */
  468. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  469. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  470. else pic->pkt_pts= AV_NOPTS_VALUE;
  471. pic->reordered_opaque= s->reordered_opaque;
  472. return 0;
  473. }
  474. /*
  475. * Not internal type and reget_buffer not overridden, emulate cr buffer
  476. */
  477. temp_pic = *pic;
  478. for(i = 0; i < AV_NUM_DATA_POINTERS; i++)
  479. pic->data[i] = pic->base[i] = NULL;
  480. pic->opaque = NULL;
  481. /* Allocate new frame */
  482. if (s->get_buffer(s, pic))
  483. return -1;
  484. /* Copy image data from old buffer to new buffer */
  485. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  486. s->height);
  487. s->release_buffer(s, &temp_pic); // Release old frame
  488. return 0;
  489. }
  490. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  491. int i;
  492. for(i=0; i<count; i++){
  493. int r= func(c, (char*)arg + i*size);
  494. if(ret) ret[i]= r;
  495. }
  496. return 0;
  497. }
  498. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  499. int i;
  500. for(i=0; i<count; i++){
  501. int r= func(c, arg, i, 0);
  502. if(ret) ret[i]= r;
  503. }
  504. return 0;
  505. }
  506. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  507. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  508. ++fmt;
  509. return fmt[0];
  510. }
  511. void avcodec_get_frame_defaults(AVFrame *pic){
  512. memset(pic, 0, sizeof(AVFrame));
  513. pic->pts= AV_NOPTS_VALUE;
  514. pic->key_frame= 1;
  515. }
  516. AVFrame *avcodec_alloc_frame(void){
  517. AVFrame *pic= av_malloc(sizeof(AVFrame));
  518. if(pic==NULL) return NULL;
  519. avcodec_get_frame_defaults(pic);
  520. return pic;
  521. }
  522. #if FF_API_AVCODEC_OPEN
  523. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  524. {
  525. return avcodec_open2(avctx, codec, NULL);
  526. }
  527. #endif
  528. int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
  529. {
  530. int ret = 0;
  531. AVDictionary *tmp = NULL;
  532. if (options)
  533. av_dict_copy(&tmp, *options, 0);
  534. /* If there is a user-supplied mutex locking routine, call it. */
  535. if (ff_lockmgr_cb) {
  536. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  537. return -1;
  538. }
  539. entangled_thread_counter++;
  540. if(entangled_thread_counter != 1){
  541. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  542. ret = -1;
  543. goto end;
  544. }
  545. if(avctx->codec || !codec) {
  546. ret = AVERROR(EINVAL);
  547. goto end;
  548. }
  549. avctx->internal = av_mallocz(sizeof(AVCodecInternal));
  550. if (!avctx->internal) {
  551. ret = AVERROR(ENOMEM);
  552. goto end;
  553. }
  554. if (codec->priv_data_size > 0) {
  555. if(!avctx->priv_data){
  556. avctx->priv_data = av_mallocz(codec->priv_data_size);
  557. if (!avctx->priv_data) {
  558. ret = AVERROR(ENOMEM);
  559. goto end;
  560. }
  561. if (codec->priv_class) {
  562. *(AVClass**)avctx->priv_data= codec->priv_class;
  563. av_opt_set_defaults(avctx->priv_data);
  564. }
  565. }
  566. if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp)) < 0)
  567. goto free_and_end;
  568. } else {
  569. avctx->priv_data = NULL;
  570. }
  571. if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
  572. goto free_and_end;
  573. if(avctx->coded_width && avctx->coded_height)
  574. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  575. else if(avctx->width && avctx->height)
  576. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  577. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  578. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  579. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  580. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  581. avcodec_set_dimensions(avctx, 0, 0);
  582. }
  583. /* if the decoder init function was already called previously,
  584. free the already allocated subtitle_header before overwriting it */
  585. if (codec->decode)
  586. av_freep(&avctx->subtitle_header);
  587. #define SANE_NB_CHANNELS 128U
  588. if (avctx->channels > SANE_NB_CHANNELS) {
  589. ret = AVERROR(EINVAL);
  590. goto free_and_end;
  591. }
  592. avctx->codec = codec;
  593. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  594. avctx->codec_id == CODEC_ID_NONE) {
  595. avctx->codec_type = codec->type;
  596. avctx->codec_id = codec->id;
  597. }
  598. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  599. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  600. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  601. ret = AVERROR(EINVAL);
  602. goto free_and_end;
  603. }
  604. avctx->frame_number = 0;
  605. #if FF_API_ER
  606. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition separate: %d; %d\n",
  607. avctx->error_recognition, avctx->err_recognition);
  608. /* FF_ER_CAREFUL (==1) implies AV_EF_CRCCHECK (== 1<<1 - 1),
  609. FF_ER_COMPLIANT (==2) implies AV_EF_{CRCCHECK,BITSTREAM} (== 1<<2 - 1), et cetera} */
  610. avctx->err_recognition |= (1<<(avctx->error_recognition-(avctx->error_recognition>=FF_ER_VERY_AGGRESSIVE))) - 1;
  611. av_log(avctx, AV_LOG_DEBUG, "err{or,}_recognition combined: %d; %d\n",
  612. avctx->error_recognition, avctx->err_recognition);
  613. #endif
  614. if (HAVE_THREADS && !avctx->thread_opaque) {
  615. ret = ff_thread_init(avctx);
  616. if (ret < 0) {
  617. goto free_and_end;
  618. }
  619. }
  620. if (avctx->codec->max_lowres < avctx->lowres) {
  621. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  622. avctx->codec->max_lowres);
  623. ret = AVERROR(EINVAL);
  624. goto free_and_end;
  625. }
  626. if (avctx->codec->encode) {
  627. int i;
  628. if (avctx->codec->sample_fmts) {
  629. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  630. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  631. break;
  632. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  633. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  634. ret = AVERROR(EINVAL);
  635. goto free_and_end;
  636. }
  637. }
  638. if (avctx->codec->supported_samplerates) {
  639. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  640. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  641. break;
  642. if (avctx->codec->supported_samplerates[i] == 0) {
  643. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  644. ret = AVERROR(EINVAL);
  645. goto free_and_end;
  646. }
  647. }
  648. if (avctx->codec->channel_layouts) {
  649. if (!avctx->channel_layout) {
  650. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  651. } else {
  652. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  653. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  654. break;
  655. if (avctx->codec->channel_layouts[i] == 0) {
  656. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  657. ret = AVERROR(EINVAL);
  658. goto free_and_end;
  659. }
  660. }
  661. }
  662. if (avctx->channel_layout && avctx->channels) {
  663. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  664. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  665. ret = AVERROR(EINVAL);
  666. goto free_and_end;
  667. }
  668. } else if (avctx->channel_layout) {
  669. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  670. }
  671. }
  672. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  673. ret = avctx->codec->init(avctx);
  674. if (ret < 0) {
  675. goto free_and_end;
  676. }
  677. }
  678. end:
  679. entangled_thread_counter--;
  680. /* Release any user-supplied mutex. */
  681. if (ff_lockmgr_cb) {
  682. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  683. }
  684. if (options) {
  685. av_dict_free(options);
  686. *options = tmp;
  687. }
  688. return ret;
  689. free_and_end:
  690. av_dict_free(&tmp);
  691. av_freep(&avctx->priv_data);
  692. av_freep(&avctx->internal);
  693. avctx->codec= NULL;
  694. goto end;
  695. }
  696. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  697. const short *samples)
  698. {
  699. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  700. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  701. return -1;
  702. }
  703. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  704. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  705. avctx->frame_number++;
  706. return ret;
  707. }else
  708. return 0;
  709. }
  710. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  711. const AVFrame *pict)
  712. {
  713. if(buf_size < FF_MIN_BUFFER_SIZE){
  714. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  715. return -1;
  716. }
  717. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  718. return -1;
  719. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  720. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  721. avctx->frame_number++;
  722. emms_c(); //needed to avoid an emms_c() call before every return;
  723. return ret;
  724. }else
  725. return 0;
  726. }
  727. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  728. const AVSubtitle *sub)
  729. {
  730. int ret;
  731. if(sub->start_display_time) {
  732. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  733. return -1;
  734. }
  735. if(sub->num_rects == 0 || !sub->rects)
  736. return -1;
  737. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  738. avctx->frame_number++;
  739. return ret;
  740. }
  741. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  742. int *got_picture_ptr,
  743. AVPacket *avpkt)
  744. {
  745. int ret;
  746. *got_picture_ptr= 0;
  747. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  748. return -1;
  749. avctx->pkt = avpkt;
  750. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
  751. if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  752. ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
  753. avpkt);
  754. else {
  755. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  756. avpkt);
  757. picture->pkt_dts= avpkt->dts;
  758. }
  759. emms_c(); //needed to avoid an emms_c() call before every return;
  760. if (*got_picture_ptr)
  761. avctx->frame_number++;
  762. }else
  763. ret= 0;
  764. return ret;
  765. }
  766. #if FF_API_OLD_DECODE_AUDIO
  767. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  768. int *frame_size_ptr,
  769. AVPacket *avpkt)
  770. {
  771. AVFrame frame;
  772. int ret, got_frame = 0;
  773. if (avctx->get_buffer != avcodec_default_get_buffer) {
  774. av_log(avctx, AV_LOG_ERROR, "A custom get_buffer() cannot be used with "
  775. "avcodec_decode_audio3()\n");
  776. return AVERROR(EINVAL);
  777. }
  778. ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
  779. if (ret >= 0 && got_frame) {
  780. int ch, plane_size;
  781. int planar = av_sample_fmt_is_planar(avctx->sample_fmt);
  782. int data_size = av_samples_get_buffer_size(&plane_size, avctx->channels,
  783. frame.nb_samples,
  784. avctx->sample_fmt, 1);
  785. if (*frame_size_ptr < data_size) {
  786. av_log(avctx, AV_LOG_ERROR, "output buffer size is too small for "
  787. "the current frame (%d < %d)\n", *frame_size_ptr, data_size);
  788. return AVERROR(EINVAL);
  789. }
  790. memcpy(samples, frame.extended_data[0], plane_size);
  791. if (planar && avctx->channels > 1) {
  792. uint8_t *out = ((uint8_t *)samples) + plane_size;
  793. for (ch = 1; ch < avctx->channels; ch++) {
  794. memcpy(out, frame.extended_data[ch], plane_size);
  795. out += plane_size;
  796. }
  797. }
  798. *frame_size_ptr = data_size;
  799. } else {
  800. *frame_size_ptr = 0;
  801. }
  802. return ret;
  803. }
  804. #endif
  805. int attribute_align_arg avcodec_decode_audio4(AVCodecContext *avctx,
  806. AVFrame *frame,
  807. int *got_frame_ptr,
  808. AVPacket *avpkt)
  809. {
  810. int ret = 0;
  811. *got_frame_ptr = 0;
  812. avctx->pkt = avpkt;
  813. if (!avpkt->data && avpkt->size) {
  814. av_log(avctx, AV_LOG_ERROR, "invalid packet: NULL data, size != 0\n");
  815. return AVERROR(EINVAL);
  816. }
  817. if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
  818. ret = avctx->codec->decode(avctx, frame, got_frame_ptr, avpkt);
  819. if (ret >= 0 && *got_frame_ptr) {
  820. avctx->frame_number++;
  821. frame->pkt_dts = avpkt->dts;
  822. }
  823. }
  824. return ret;
  825. }
  826. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  827. int *got_sub_ptr,
  828. AVPacket *avpkt)
  829. {
  830. int ret;
  831. avctx->pkt = avpkt;
  832. *got_sub_ptr = 0;
  833. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  834. if (*got_sub_ptr)
  835. avctx->frame_number++;
  836. return ret;
  837. }
  838. void avsubtitle_free(AVSubtitle *sub)
  839. {
  840. int i;
  841. for (i = 0; i < sub->num_rects; i++)
  842. {
  843. av_freep(&sub->rects[i]->pict.data[0]);
  844. av_freep(&sub->rects[i]->pict.data[1]);
  845. av_freep(&sub->rects[i]->pict.data[2]);
  846. av_freep(&sub->rects[i]->pict.data[3]);
  847. av_freep(&sub->rects[i]->text);
  848. av_freep(&sub->rects[i]->ass);
  849. av_freep(&sub->rects[i]);
  850. }
  851. av_freep(&sub->rects);
  852. memset(sub, 0, sizeof(AVSubtitle));
  853. }
  854. av_cold int avcodec_close(AVCodecContext *avctx)
  855. {
  856. /* If there is a user-supplied mutex locking routine, call it. */
  857. if (ff_lockmgr_cb) {
  858. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  859. return -1;
  860. }
  861. entangled_thread_counter++;
  862. if(entangled_thread_counter != 1){
  863. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  864. entangled_thread_counter--;
  865. return -1;
  866. }
  867. if (HAVE_THREADS && avctx->thread_opaque)
  868. ff_thread_free(avctx);
  869. if (avctx->codec && avctx->codec->close)
  870. avctx->codec->close(avctx);
  871. avcodec_default_free_buffers(avctx);
  872. avctx->coded_frame = NULL;
  873. av_freep(&avctx->internal);
  874. if (avctx->codec && avctx->codec->priv_class)
  875. av_opt_free(avctx->priv_data);
  876. av_opt_free(avctx);
  877. av_freep(&avctx->priv_data);
  878. if(avctx->codec && avctx->codec->encode)
  879. av_freep(&avctx->extradata);
  880. avctx->codec = NULL;
  881. avctx->active_thread_type = 0;
  882. entangled_thread_counter--;
  883. /* Release any user-supplied mutex. */
  884. if (ff_lockmgr_cb) {
  885. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  886. }
  887. return 0;
  888. }
  889. AVCodec *avcodec_find_encoder(enum CodecID id)
  890. {
  891. AVCodec *p, *experimental=NULL;
  892. p = first_avcodec;
  893. while (p) {
  894. if (p->encode != NULL && p->id == id) {
  895. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  896. experimental = p;
  897. } else
  898. return p;
  899. }
  900. p = p->next;
  901. }
  902. return experimental;
  903. }
  904. AVCodec *avcodec_find_encoder_by_name(const char *name)
  905. {
  906. AVCodec *p;
  907. if (!name)
  908. return NULL;
  909. p = first_avcodec;
  910. while (p) {
  911. if (p->encode != NULL && strcmp(name,p->name) == 0)
  912. return p;
  913. p = p->next;
  914. }
  915. return NULL;
  916. }
  917. AVCodec *avcodec_find_decoder(enum CodecID id)
  918. {
  919. AVCodec *p;
  920. p = first_avcodec;
  921. while (p) {
  922. if (p->decode != NULL && p->id == id)
  923. return p;
  924. p = p->next;
  925. }
  926. return NULL;
  927. }
  928. AVCodec *avcodec_find_decoder_by_name(const char *name)
  929. {
  930. AVCodec *p;
  931. if (!name)
  932. return NULL;
  933. p = first_avcodec;
  934. while (p) {
  935. if (p->decode != NULL && strcmp(name,p->name) == 0)
  936. return p;
  937. p = p->next;
  938. }
  939. return NULL;
  940. }
  941. static int get_bit_rate(AVCodecContext *ctx)
  942. {
  943. int bit_rate;
  944. int bits_per_sample;
  945. switch(ctx->codec_type) {
  946. case AVMEDIA_TYPE_VIDEO:
  947. case AVMEDIA_TYPE_DATA:
  948. case AVMEDIA_TYPE_SUBTITLE:
  949. case AVMEDIA_TYPE_ATTACHMENT:
  950. bit_rate = ctx->bit_rate;
  951. break;
  952. case AVMEDIA_TYPE_AUDIO:
  953. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  954. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  955. break;
  956. default:
  957. bit_rate = 0;
  958. break;
  959. }
  960. return bit_rate;
  961. }
  962. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  963. {
  964. int i, len, ret = 0;
  965. for (i = 0; i < 4; i++) {
  966. len = snprintf(buf, buf_size,
  967. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  968. buf += len;
  969. buf_size = buf_size > len ? buf_size - len : 0;
  970. ret += len;
  971. codec_tag>>=8;
  972. }
  973. return ret;
  974. }
  975. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  976. {
  977. const char *codec_name;
  978. const char *profile = NULL;
  979. AVCodec *p;
  980. char buf1[32];
  981. int bitrate;
  982. AVRational display_aspect_ratio;
  983. if (encode)
  984. p = avcodec_find_encoder(enc->codec_id);
  985. else
  986. p = avcodec_find_decoder(enc->codec_id);
  987. if (p) {
  988. codec_name = p->name;
  989. profile = av_get_profile_name(p, enc->profile);
  990. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  991. /* fake mpeg2 transport stream codec (currently not
  992. registered) */
  993. codec_name = "mpeg2ts";
  994. } else if (enc->codec_name[0] != '\0') {
  995. codec_name = enc->codec_name;
  996. } else {
  997. /* output avi tags */
  998. char tag_buf[32];
  999. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  1000. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  1001. codec_name = buf1;
  1002. }
  1003. switch(enc->codec_type) {
  1004. case AVMEDIA_TYPE_VIDEO:
  1005. snprintf(buf, buf_size,
  1006. "Video: %s%s",
  1007. codec_name, enc->mb_decision ? " (hq)" : "");
  1008. if (profile)
  1009. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1010. " (%s)", profile);
  1011. if (enc->pix_fmt != PIX_FMT_NONE) {
  1012. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1013. ", %s",
  1014. av_get_pix_fmt_name(enc->pix_fmt));
  1015. }
  1016. if (enc->width) {
  1017. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1018. ", %dx%d",
  1019. enc->width, enc->height);
  1020. if (enc->sample_aspect_ratio.num) {
  1021. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  1022. enc->width*enc->sample_aspect_ratio.num,
  1023. enc->height*enc->sample_aspect_ratio.den,
  1024. 1024*1024);
  1025. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1026. " [PAR %d:%d DAR %d:%d]",
  1027. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  1028. display_aspect_ratio.num, display_aspect_ratio.den);
  1029. }
  1030. if(av_log_get_level() >= AV_LOG_DEBUG){
  1031. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  1032. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1033. ", %d/%d",
  1034. enc->time_base.num/g, enc->time_base.den/g);
  1035. }
  1036. }
  1037. if (encode) {
  1038. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1039. ", q=%d-%d", enc->qmin, enc->qmax);
  1040. }
  1041. break;
  1042. case AVMEDIA_TYPE_AUDIO:
  1043. snprintf(buf, buf_size,
  1044. "Audio: %s",
  1045. codec_name);
  1046. if (profile)
  1047. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1048. " (%s)", profile);
  1049. if (enc->sample_rate) {
  1050. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1051. ", %d Hz", enc->sample_rate);
  1052. }
  1053. av_strlcat(buf, ", ", buf_size);
  1054. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  1055. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  1056. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1057. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  1058. }
  1059. break;
  1060. case AVMEDIA_TYPE_DATA:
  1061. snprintf(buf, buf_size, "Data: %s", codec_name);
  1062. break;
  1063. case AVMEDIA_TYPE_SUBTITLE:
  1064. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  1065. break;
  1066. case AVMEDIA_TYPE_ATTACHMENT:
  1067. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  1068. break;
  1069. default:
  1070. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  1071. return;
  1072. }
  1073. if (encode) {
  1074. if (enc->flags & CODEC_FLAG_PASS1)
  1075. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1076. ", pass 1");
  1077. if (enc->flags & CODEC_FLAG_PASS2)
  1078. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1079. ", pass 2");
  1080. }
  1081. bitrate = get_bit_rate(enc);
  1082. if (bitrate != 0) {
  1083. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  1084. ", %d kb/s", bitrate / 1000);
  1085. }
  1086. }
  1087. const char *av_get_profile_name(const AVCodec *codec, int profile)
  1088. {
  1089. const AVProfile *p;
  1090. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  1091. return NULL;
  1092. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  1093. if (p->profile == profile)
  1094. return p->name;
  1095. return NULL;
  1096. }
  1097. unsigned avcodec_version( void )
  1098. {
  1099. return LIBAVCODEC_VERSION_INT;
  1100. }
  1101. const char *avcodec_configuration(void)
  1102. {
  1103. return LIBAV_CONFIGURATION;
  1104. }
  1105. const char *avcodec_license(void)
  1106. {
  1107. #define LICENSE_PREFIX "libavcodec license: "
  1108. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  1109. }
  1110. void avcodec_flush_buffers(AVCodecContext *avctx)
  1111. {
  1112. if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1113. ff_thread_flush(avctx);
  1114. else if(avctx->codec->flush)
  1115. avctx->codec->flush(avctx);
  1116. }
  1117. static void video_free_buffers(AVCodecContext *s)
  1118. {
  1119. AVCodecInternal *avci = s->internal;
  1120. int i, j;
  1121. if (!avci->buffer)
  1122. return;
  1123. if (avci->buffer_count)
  1124. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
  1125. avci->buffer_count);
  1126. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  1127. InternalBuffer *buf = &avci->buffer[i];
  1128. for(j=0; j<4; j++){
  1129. av_freep(&buf->base[j]);
  1130. buf->data[j]= NULL;
  1131. }
  1132. }
  1133. av_freep(&avci->buffer);
  1134. avci->buffer_count=0;
  1135. }
  1136. static void audio_free_buffers(AVCodecContext *avctx)
  1137. {
  1138. AVCodecInternal *avci = avctx->internal;
  1139. InternalBuffer *buf;
  1140. if (!avci->buffer)
  1141. return;
  1142. buf = avci->buffer;
  1143. if (buf->extended_data) {
  1144. av_free(buf->extended_data[0]);
  1145. if (buf->extended_data != buf->data)
  1146. av_free(buf->extended_data);
  1147. }
  1148. av_freep(&avci->buffer);
  1149. }
  1150. void avcodec_default_free_buffers(AVCodecContext *avctx)
  1151. {
  1152. switch (avctx->codec_type) {
  1153. case AVMEDIA_TYPE_VIDEO:
  1154. video_free_buffers(avctx);
  1155. break;
  1156. case AVMEDIA_TYPE_AUDIO:
  1157. audio_free_buffers(avctx);
  1158. break;
  1159. default:
  1160. break;
  1161. }
  1162. }
  1163. #if FF_API_OLD_FF_PICT_TYPES
  1164. char av_get_pict_type_char(int pict_type){
  1165. return av_get_picture_type_char(pict_type);
  1166. }
  1167. #endif
  1168. int av_get_bits_per_sample(enum CodecID codec_id){
  1169. switch(codec_id){
  1170. case CODEC_ID_ADPCM_SBPRO_2:
  1171. return 2;
  1172. case CODEC_ID_ADPCM_SBPRO_3:
  1173. return 3;
  1174. case CODEC_ID_ADPCM_SBPRO_4:
  1175. case CODEC_ID_ADPCM_CT:
  1176. case CODEC_ID_ADPCM_IMA_WAV:
  1177. case CODEC_ID_ADPCM_IMA_QT:
  1178. case CODEC_ID_ADPCM_SWF:
  1179. case CODEC_ID_ADPCM_MS:
  1180. case CODEC_ID_ADPCM_YAMAHA:
  1181. return 4;
  1182. case CODEC_ID_ADPCM_G722:
  1183. case CODEC_ID_PCM_ALAW:
  1184. case CODEC_ID_PCM_MULAW:
  1185. case CODEC_ID_PCM_S8:
  1186. case CODEC_ID_PCM_U8:
  1187. case CODEC_ID_PCM_ZORK:
  1188. return 8;
  1189. case CODEC_ID_PCM_S16BE:
  1190. case CODEC_ID_PCM_S16LE:
  1191. case CODEC_ID_PCM_S16LE_PLANAR:
  1192. case CODEC_ID_PCM_U16BE:
  1193. case CODEC_ID_PCM_U16LE:
  1194. return 16;
  1195. case CODEC_ID_PCM_S24DAUD:
  1196. case CODEC_ID_PCM_S24BE:
  1197. case CODEC_ID_PCM_S24LE:
  1198. case CODEC_ID_PCM_U24BE:
  1199. case CODEC_ID_PCM_U24LE:
  1200. return 24;
  1201. case CODEC_ID_PCM_S32BE:
  1202. case CODEC_ID_PCM_S32LE:
  1203. case CODEC_ID_PCM_U32BE:
  1204. case CODEC_ID_PCM_U32LE:
  1205. case CODEC_ID_PCM_F32BE:
  1206. case CODEC_ID_PCM_F32LE:
  1207. return 32;
  1208. case CODEC_ID_PCM_F64BE:
  1209. case CODEC_ID_PCM_F64LE:
  1210. return 64;
  1211. default:
  1212. return 0;
  1213. }
  1214. }
  1215. #if FF_API_OLD_SAMPLE_FMT
  1216. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  1217. return av_get_bytes_per_sample(sample_fmt) << 3;
  1218. }
  1219. #endif
  1220. #if !HAVE_THREADS
  1221. int ff_thread_init(AVCodecContext *s){
  1222. return -1;
  1223. }
  1224. #endif
  1225. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1226. {
  1227. unsigned int n = 0;
  1228. while(v >= 0xff) {
  1229. *s++ = 0xff;
  1230. v -= 0xff;
  1231. n++;
  1232. }
  1233. *s = v;
  1234. n++;
  1235. return n;
  1236. }
  1237. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1238. int i;
  1239. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1240. return i;
  1241. }
  1242. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1243. {
  1244. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
  1245. "version to the newest one from Git. If the problem still "
  1246. "occurs, it means that your file has a feature which has not "
  1247. "been implemented.\n", feature);
  1248. if(want_sample)
  1249. av_log_ask_for_sample(avc, NULL);
  1250. }
  1251. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1252. {
  1253. va_list argument_list;
  1254. va_start(argument_list, msg);
  1255. if (msg)
  1256. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1257. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1258. "of this file to ftp://upload.libav.org/incoming/ "
  1259. "and contact the libav-devel mailing list.\n");
  1260. va_end(argument_list);
  1261. }
  1262. static AVHWAccel *first_hwaccel = NULL;
  1263. void av_register_hwaccel(AVHWAccel *hwaccel)
  1264. {
  1265. AVHWAccel **p = &first_hwaccel;
  1266. while (*p)
  1267. p = &(*p)->next;
  1268. *p = hwaccel;
  1269. hwaccel->next = NULL;
  1270. }
  1271. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1272. {
  1273. return hwaccel ? hwaccel->next : first_hwaccel;
  1274. }
  1275. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1276. {
  1277. AVHWAccel *hwaccel=NULL;
  1278. while((hwaccel= av_hwaccel_next(hwaccel))){
  1279. if ( hwaccel->id == codec_id
  1280. && hwaccel->pix_fmt == pix_fmt)
  1281. return hwaccel;
  1282. }
  1283. return NULL;
  1284. }
  1285. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1286. {
  1287. if (ff_lockmgr_cb) {
  1288. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1289. return -1;
  1290. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_DESTROY))
  1291. return -1;
  1292. }
  1293. ff_lockmgr_cb = cb;
  1294. if (ff_lockmgr_cb) {
  1295. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1296. return -1;
  1297. if (ff_lockmgr_cb(&avformat_mutex, AV_LOCK_CREATE))
  1298. return -1;
  1299. }
  1300. return 0;
  1301. }
  1302. int avpriv_lock_avformat(void)
  1303. {
  1304. if (ff_lockmgr_cb) {
  1305. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_OBTAIN))
  1306. return -1;
  1307. }
  1308. return 0;
  1309. }
  1310. int avpriv_unlock_avformat(void)
  1311. {
  1312. if (ff_lockmgr_cb) {
  1313. if ((*ff_lockmgr_cb)(&avformat_mutex, AV_LOCK_RELEASE))
  1314. return -1;
  1315. }
  1316. return 0;
  1317. }
  1318. unsigned int avpriv_toupper4(unsigned int x)
  1319. {
  1320. return toupper( x &0xFF)
  1321. + (toupper((x>>8 )&0xFF)<<8 )
  1322. + (toupper((x>>16)&0xFF)<<16)
  1323. + (toupper((x>>24)&0xFF)<<24);
  1324. }
  1325. #if !HAVE_THREADS
  1326. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1327. {
  1328. f->owner = avctx;
  1329. return avctx->get_buffer(avctx, f);
  1330. }
  1331. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1332. {
  1333. f->owner->release_buffer(f->owner, f);
  1334. }
  1335. void ff_thread_finish_setup(AVCodecContext *avctx)
  1336. {
  1337. }
  1338. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1339. {
  1340. }
  1341. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1342. {
  1343. }
  1344. #endif
  1345. #if FF_API_THREAD_INIT
  1346. int avcodec_thread_init(AVCodecContext *s, int thread_count)
  1347. {
  1348. s->thread_count = thread_count;
  1349. return ff_thread_init(s);
  1350. }
  1351. #endif
  1352. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  1353. {
  1354. if (codec_id <= CODEC_ID_NONE)
  1355. return AVMEDIA_TYPE_UNKNOWN;
  1356. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  1357. return AVMEDIA_TYPE_VIDEO;
  1358. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  1359. return AVMEDIA_TYPE_AUDIO;
  1360. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  1361. return AVMEDIA_TYPE_SUBTITLE;
  1362. return AVMEDIA_TYPE_UNKNOWN;
  1363. }