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.

502 lines
13KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. #include "dsputil.h"
  21. #include "mpegvideo.h"
  22. void *av_mallocz(int size)
  23. {
  24. void *ptr;
  25. ptr = av_malloc(size);
  26. if (!ptr)
  27. return NULL;
  28. memset(ptr, 0, size);
  29. return ptr;
  30. }
  31. /* cannot call it directly because of 'void **' casting is not automatic */
  32. void __av_freep(void **ptr)
  33. {
  34. av_free(*ptr);
  35. *ptr = NULL;
  36. }
  37. /* encoder management */
  38. AVCodec *first_avcodec;
  39. void register_avcodec(AVCodec *format)
  40. {
  41. AVCodec **p;
  42. p = &first_avcodec;
  43. while (*p != NULL) p = &(*p)->next;
  44. *p = format;
  45. format->next = NULL;
  46. }
  47. void avcodec_get_context_defaults(AVCodecContext *s){
  48. s->bit_rate= 800*1000;
  49. s->bit_rate_tolerance= s->bit_rate*10;
  50. s->qmin= 2;
  51. s->qmax= 31;
  52. s->rc_eq= "tex^qComp";
  53. s->qcompress= 0.5;
  54. s->max_qdiff= 3;
  55. s->b_quant_factor=1.25;
  56. s->b_quant_offset=1.25;
  57. s->i_quant_factor=-0.8;
  58. s->i_quant_offset=0.0;
  59. s->error_concealment= 3;
  60. s->workaround_bugs= FF_BUG_AUTODETECT;
  61. }
  62. /**
  63. * allocates a AVCodecContext and set it to defaults.
  64. * this can be deallocated by simply calling free()
  65. */
  66. AVCodecContext *avcodec_alloc_context(void){
  67. AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
  68. if(avctx==NULL) return NULL;
  69. avcodec_get_context_defaults(avctx);
  70. return avctx;
  71. }
  72. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  73. {
  74. int ret;
  75. avctx->codec = codec;
  76. avctx->frame_number = 0;
  77. if (codec->priv_data_size > 0) {
  78. avctx->priv_data = av_mallocz(codec->priv_data_size);
  79. if (!avctx->priv_data)
  80. return -ENOMEM;
  81. } else {
  82. avctx->priv_data = NULL;
  83. }
  84. ret = avctx->codec->init(avctx);
  85. if (ret < 0) {
  86. av_freep(&avctx->priv_data);
  87. return ret;
  88. }
  89. return 0;
  90. }
  91. int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  92. const short *samples)
  93. {
  94. int ret;
  95. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  96. avctx->frame_number++;
  97. return ret;
  98. }
  99. int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  100. const AVPicture *pict)
  101. {
  102. int ret;
  103. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  104. avctx->frame_number++;
  105. return ret;
  106. }
  107. /* decode a frame. return -1 if error, otherwise return the number of
  108. bytes used. If no frame could be decompressed, *got_picture_ptr is
  109. zero. Otherwise, it is non zero */
  110. int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture,
  111. int *got_picture_ptr,
  112. UINT8 *buf, int buf_size)
  113. {
  114. int ret;
  115. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  116. buf, buf_size);
  117. if (*got_picture_ptr)
  118. avctx->frame_number++;
  119. return ret;
  120. }
  121. /* decode an audio frame. return -1 if error, otherwise return the
  122. *number of bytes used. If no frame could be decompressed,
  123. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  124. *size in BYTES. */
  125. int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
  126. int *frame_size_ptr,
  127. UINT8 *buf, int buf_size)
  128. {
  129. int ret;
  130. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  131. buf, buf_size);
  132. avctx->frame_number++;
  133. return ret;
  134. }
  135. int avcodec_close(AVCodecContext *avctx)
  136. {
  137. if (avctx->codec->close)
  138. avctx->codec->close(avctx);
  139. av_freep(&avctx->priv_data);
  140. avctx->codec = NULL;
  141. return 0;
  142. }
  143. AVCodec *avcodec_find_encoder(enum CodecID id)
  144. {
  145. AVCodec *p;
  146. p = first_avcodec;
  147. while (p) {
  148. if (p->encode != NULL && p->id == id)
  149. return p;
  150. p = p->next;
  151. }
  152. return NULL;
  153. }
  154. AVCodec *avcodec_find_encoder_by_name(const char *name)
  155. {
  156. AVCodec *p;
  157. p = first_avcodec;
  158. while (p) {
  159. if (p->encode != NULL && strcmp(name,p->name) == 0)
  160. return p;
  161. p = p->next;
  162. }
  163. return NULL;
  164. }
  165. AVCodec *avcodec_find_decoder(enum CodecID id)
  166. {
  167. AVCodec *p;
  168. p = first_avcodec;
  169. while (p) {
  170. if (p->decode != NULL && p->id == id)
  171. return p;
  172. p = p->next;
  173. }
  174. return NULL;
  175. }
  176. AVCodec *avcodec_find_decoder_by_name(const char *name)
  177. {
  178. AVCodec *p;
  179. p = first_avcodec;
  180. while (p) {
  181. if (p->decode != NULL && strcmp(name,p->name) == 0)
  182. return p;
  183. p = p->next;
  184. }
  185. return NULL;
  186. }
  187. AVCodec *avcodec_find(enum CodecID id)
  188. {
  189. AVCodec *p;
  190. p = first_avcodec;
  191. while (p) {
  192. if (p->id == id)
  193. return p;
  194. p = p->next;
  195. }
  196. return NULL;
  197. }
  198. const char *pix_fmt_str[] = {
  199. "yuv420p",
  200. "yuv422",
  201. "rgb24",
  202. "bgr24",
  203. "yuv422p",
  204. "yuv444p",
  205. "rgba32",
  206. "bgra32",
  207. "yuv410p",
  208. "yuv411p",
  209. };
  210. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  211. {
  212. const char *codec_name;
  213. AVCodec *p;
  214. char buf1[32];
  215. char channels_str[100];
  216. int bitrate;
  217. if (encode)
  218. p = avcodec_find_encoder(enc->codec_id);
  219. else
  220. p = avcodec_find_decoder(enc->codec_id);
  221. if (p) {
  222. codec_name = p->name;
  223. } else if (enc->codec_name[0] != '\0') {
  224. codec_name = enc->codec_name;
  225. } else {
  226. /* output avi tags */
  227. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  228. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  229. enc->codec_tag & 0xff,
  230. (enc->codec_tag >> 8) & 0xff,
  231. (enc->codec_tag >> 16) & 0xff,
  232. (enc->codec_tag >> 24) & 0xff);
  233. } else {
  234. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  235. }
  236. codec_name = buf1;
  237. }
  238. switch(enc->codec_type) {
  239. case CODEC_TYPE_VIDEO:
  240. snprintf(buf, buf_size,
  241. "Video: %s%s",
  242. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  243. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  244. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  245. ", %s",
  246. pix_fmt_str[enc->pix_fmt]);
  247. }
  248. if (enc->width) {
  249. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  250. ", %dx%d, %0.2f fps",
  251. enc->width, enc->height,
  252. (float)enc->frame_rate / FRAME_RATE_BASE);
  253. }
  254. if (encode) {
  255. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  256. ", q=%d-%d", enc->qmin, enc->qmax);
  257. }
  258. bitrate = enc->bit_rate;
  259. break;
  260. case CODEC_TYPE_AUDIO:
  261. snprintf(buf, buf_size,
  262. "Audio: %s",
  263. codec_name);
  264. switch (enc->channels) {
  265. case 1:
  266. strcpy(channels_str, "mono");
  267. break;
  268. case 2:
  269. strcpy(channels_str, "stereo");
  270. break;
  271. case 6:
  272. strcpy(channels_str, "5:1");
  273. break;
  274. default:
  275. sprintf(channels_str, "%d channels", enc->channels);
  276. break;
  277. }
  278. if (enc->sample_rate) {
  279. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  280. ", %d Hz, %s",
  281. enc->sample_rate,
  282. channels_str);
  283. }
  284. /* for PCM codecs, compute bitrate directly */
  285. switch(enc->codec_id) {
  286. case CODEC_ID_PCM_S16LE:
  287. case CODEC_ID_PCM_S16BE:
  288. case CODEC_ID_PCM_U16LE:
  289. case CODEC_ID_PCM_U16BE:
  290. bitrate = enc->sample_rate * enc->channels * 16;
  291. break;
  292. case CODEC_ID_PCM_S8:
  293. case CODEC_ID_PCM_U8:
  294. case CODEC_ID_PCM_ALAW:
  295. case CODEC_ID_PCM_MULAW:
  296. bitrate = enc->sample_rate * enc->channels * 8;
  297. break;
  298. default:
  299. bitrate = enc->bit_rate;
  300. break;
  301. }
  302. break;
  303. default:
  304. av_abort();
  305. }
  306. if (encode) {
  307. if (enc->flags & CODEC_FLAG_PASS1)
  308. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  309. ", pass 1");
  310. if (enc->flags & CODEC_FLAG_PASS2)
  311. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  312. ", pass 2");
  313. }
  314. if (bitrate != 0) {
  315. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  316. ", %d kb/s", bitrate / 1000);
  317. }
  318. }
  319. /* Picture field are filled with 'ptr' addresses */
  320. void avpicture_fill(AVPicture *picture, UINT8 *ptr,
  321. int pix_fmt, int width, int height)
  322. {
  323. int size;
  324. size = width * height;
  325. switch(pix_fmt) {
  326. case PIX_FMT_YUV420P:
  327. picture->data[0] = ptr;
  328. picture->data[1] = picture->data[0] + size;
  329. picture->data[2] = picture->data[1] + size / 4;
  330. picture->linesize[0] = width;
  331. picture->linesize[1] = width / 2;
  332. picture->linesize[2] = width / 2;
  333. break;
  334. case PIX_FMT_YUV422P:
  335. picture->data[0] = ptr;
  336. picture->data[1] = picture->data[0] + size;
  337. picture->data[2] = picture->data[1] + size / 2;
  338. picture->linesize[0] = width;
  339. picture->linesize[1] = width / 2;
  340. picture->linesize[2] = width / 2;
  341. break;
  342. case PIX_FMT_YUV444P:
  343. picture->data[0] = ptr;
  344. picture->data[1] = picture->data[0] + size;
  345. picture->data[2] = picture->data[1] + size;
  346. picture->linesize[0] = width;
  347. picture->linesize[1] = width;
  348. picture->linesize[2] = width;
  349. break;
  350. case PIX_FMT_RGB24:
  351. case PIX_FMT_BGR24:
  352. picture->data[0] = ptr;
  353. picture->data[1] = NULL;
  354. picture->data[2] = NULL;
  355. picture->linesize[0] = width * 3;
  356. break;
  357. case PIX_FMT_RGBA32:
  358. case PIX_FMT_BGRA32:
  359. picture->data[0] = ptr;
  360. picture->data[1] = NULL;
  361. picture->data[2] = NULL;
  362. picture->linesize[0] = width * 4;
  363. break;
  364. case PIX_FMT_YUV422:
  365. picture->data[0] = ptr;
  366. picture->data[1] = NULL;
  367. picture->data[2] = NULL;
  368. picture->linesize[0] = width * 2;
  369. break;
  370. default:
  371. picture->data[0] = NULL;
  372. picture->data[1] = NULL;
  373. picture->data[2] = NULL;
  374. break;
  375. }
  376. }
  377. int avpicture_get_size(int pix_fmt, int width, int height)
  378. {
  379. int size;
  380. size = width * height;
  381. switch(pix_fmt) {
  382. case PIX_FMT_YUV420P:
  383. size = (size * 3) / 2;
  384. break;
  385. case PIX_FMT_YUV422P:
  386. size = (size * 2);
  387. break;
  388. case PIX_FMT_YUV444P:
  389. size = (size * 3);
  390. break;
  391. case PIX_FMT_RGB24:
  392. case PIX_FMT_BGR24:
  393. size = (size * 3);
  394. break;
  395. case PIX_FMT_RGBA32:
  396. case PIX_FMT_BGRA32:
  397. size = (size * 4);
  398. break;
  399. case PIX_FMT_YUV422:
  400. size = (size * 2);
  401. break;
  402. default:
  403. size = -1;
  404. break;
  405. }
  406. return size;
  407. }
  408. unsigned avcodec_version( void )
  409. {
  410. return LIBAVCODEC_VERSION_INT;
  411. }
  412. unsigned avcodec_build( void )
  413. {
  414. return LIBAVCODEC_BUILD;
  415. }
  416. /* must be called before any other functions */
  417. void avcodec_init(void)
  418. {
  419. static int inited = 0;
  420. if (inited != 0)
  421. return;
  422. inited = 1;
  423. dsputil_init();
  424. }
  425. /* this should be called after seeking and before trying to decode the next frame */
  426. void avcodec_flush_buffers(AVCodecContext *avctx)
  427. {
  428. MpegEncContext *s = avctx->priv_data;
  429. s->num_available_buffers=0;
  430. }
  431. static int raw_encode_init(AVCodecContext *s)
  432. {
  433. return 0;
  434. }
  435. static int raw_decode_frame(AVCodecContext *avctx,
  436. void *data, int *data_size,
  437. UINT8 *buf, int buf_size)
  438. {
  439. return -1;
  440. }
  441. static int raw_encode_frame(AVCodecContext *avctx,
  442. unsigned char *frame, int buf_size, void *data)
  443. {
  444. return -1;
  445. }
  446. AVCodec rawvideo_codec = {
  447. "rawvideo",
  448. CODEC_TYPE_VIDEO,
  449. CODEC_ID_RAWVIDEO,
  450. 0,
  451. raw_encode_init,
  452. raw_encode_frame,
  453. NULL,
  454. raw_decode_frame,
  455. };