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.

1394 lines
41KB

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