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.

1432 lines
42KB

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