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.

512 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(unsigned 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->error_resilience= 1;
  61. s->workaround_bugs= FF_BUG_AUTODETECT;
  62. s->frame_rate = 25 * FRAME_RATE_BASE;
  63. s->gop_size= 50;
  64. s->me_method= ME_EPZS;
  65. }
  66. /**
  67. * allocates a AVCodecContext and set it to defaults.
  68. * this can be deallocated by simply calling free()
  69. */
  70. AVCodecContext *avcodec_alloc_context(void){
  71. AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
  72. if(avctx==NULL) return NULL;
  73. avcodec_get_context_defaults(avctx);
  74. return avctx;
  75. }
  76. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  77. {
  78. int ret;
  79. avctx->codec = codec;
  80. avctx->frame_number = 0;
  81. if (codec->priv_data_size > 0) {
  82. avctx->priv_data = av_mallocz(codec->priv_data_size);
  83. if (!avctx->priv_data)
  84. return -ENOMEM;
  85. } else {
  86. avctx->priv_data = NULL;
  87. }
  88. ret = avctx->codec->init(avctx);
  89. if (ret < 0) {
  90. av_freep(&avctx->priv_data);
  91. return ret;
  92. }
  93. return 0;
  94. }
  95. int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  96. const short *samples)
  97. {
  98. int ret;
  99. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  100. avctx->frame_number++;
  101. return ret;
  102. }
  103. int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  104. const AVPicture *pict)
  105. {
  106. int ret;
  107. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  108. emms_c(); //needed to avoid a emms_c() call before every return;
  109. avctx->frame_number++;
  110. return ret;
  111. }
  112. /* decode a frame. return -1 if error, otherwise return the number of
  113. bytes used. If no frame could be decompressed, *got_picture_ptr is
  114. zero. Otherwise, it is non zero */
  115. int avcodec_decode_video(AVCodecContext *avctx, AVPicture *picture,
  116. int *got_picture_ptr,
  117. UINT8 *buf, int buf_size)
  118. {
  119. int ret;
  120. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  121. buf, buf_size);
  122. emms_c(); //needed to avoid a emms_c() call before every return;
  123. if (*got_picture_ptr)
  124. avctx->frame_number++;
  125. return ret;
  126. }
  127. /* decode an audio frame. return -1 if error, otherwise return the
  128. *number of bytes used. If no frame could be decompressed,
  129. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  130. *size in BYTES. */
  131. int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
  132. int *frame_size_ptr,
  133. UINT8 *buf, int buf_size)
  134. {
  135. int ret;
  136. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  137. buf, buf_size);
  138. avctx->frame_number++;
  139. return ret;
  140. }
  141. int avcodec_close(AVCodecContext *avctx)
  142. {
  143. if (avctx->codec->close)
  144. avctx->codec->close(avctx);
  145. av_freep(&avctx->priv_data);
  146. avctx->codec = NULL;
  147. return 0;
  148. }
  149. AVCodec *avcodec_find_encoder(enum CodecID id)
  150. {
  151. AVCodec *p;
  152. p = first_avcodec;
  153. while (p) {
  154. if (p->encode != NULL && p->id == id)
  155. return p;
  156. p = p->next;
  157. }
  158. return NULL;
  159. }
  160. AVCodec *avcodec_find_encoder_by_name(const char *name)
  161. {
  162. AVCodec *p;
  163. p = first_avcodec;
  164. while (p) {
  165. if (p->encode != NULL && strcmp(name,p->name) == 0)
  166. return p;
  167. p = p->next;
  168. }
  169. return NULL;
  170. }
  171. AVCodec *avcodec_find_decoder(enum CodecID id)
  172. {
  173. AVCodec *p;
  174. p = first_avcodec;
  175. while (p) {
  176. if (p->decode != NULL && p->id == id)
  177. return p;
  178. p = p->next;
  179. }
  180. return NULL;
  181. }
  182. AVCodec *avcodec_find_decoder_by_name(const char *name)
  183. {
  184. AVCodec *p;
  185. p = first_avcodec;
  186. while (p) {
  187. if (p->decode != NULL && strcmp(name,p->name) == 0)
  188. return p;
  189. p = p->next;
  190. }
  191. return NULL;
  192. }
  193. AVCodec *avcodec_find(enum CodecID id)
  194. {
  195. AVCodec *p;
  196. p = first_avcodec;
  197. while (p) {
  198. if (p->id == id)
  199. return p;
  200. p = p->next;
  201. }
  202. return NULL;
  203. }
  204. const char *pix_fmt_str[] = {
  205. "yuv420p",
  206. "yuv422",
  207. "rgb24",
  208. "bgr24",
  209. "yuv422p",
  210. "yuv444p",
  211. "rgba32",
  212. "bgra32",
  213. "yuv410p",
  214. "yuv411p",
  215. };
  216. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  217. {
  218. const char *codec_name;
  219. AVCodec *p;
  220. char buf1[32];
  221. char channels_str[100];
  222. int bitrate;
  223. if (encode)
  224. p = avcodec_find_encoder(enc->codec_id);
  225. else
  226. p = avcodec_find_decoder(enc->codec_id);
  227. if (p) {
  228. codec_name = p->name;
  229. } else if (enc->codec_name[0] != '\0') {
  230. codec_name = enc->codec_name;
  231. } else {
  232. /* output avi tags */
  233. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  234. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  235. enc->codec_tag & 0xff,
  236. (enc->codec_tag >> 8) & 0xff,
  237. (enc->codec_tag >> 16) & 0xff,
  238. (enc->codec_tag >> 24) & 0xff);
  239. } else {
  240. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  241. }
  242. codec_name = buf1;
  243. }
  244. switch(enc->codec_type) {
  245. case CODEC_TYPE_VIDEO:
  246. snprintf(buf, buf_size,
  247. "Video: %s%s",
  248. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  249. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  250. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  251. ", %s",
  252. pix_fmt_str[enc->pix_fmt]);
  253. }
  254. if (enc->width) {
  255. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  256. ", %dx%d, %0.2f fps",
  257. enc->width, enc->height,
  258. (float)enc->frame_rate / FRAME_RATE_BASE);
  259. }
  260. if (encode) {
  261. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  262. ", q=%d-%d", enc->qmin, enc->qmax);
  263. }
  264. bitrate = enc->bit_rate;
  265. break;
  266. case CODEC_TYPE_AUDIO:
  267. snprintf(buf, buf_size,
  268. "Audio: %s",
  269. codec_name);
  270. switch (enc->channels) {
  271. case 1:
  272. strcpy(channels_str, "mono");
  273. break;
  274. case 2:
  275. strcpy(channels_str, "stereo");
  276. break;
  277. case 6:
  278. strcpy(channels_str, "5:1");
  279. break;
  280. default:
  281. sprintf(channels_str, "%d channels", enc->channels);
  282. break;
  283. }
  284. if (enc->sample_rate) {
  285. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  286. ", %d Hz, %s",
  287. enc->sample_rate,
  288. channels_str);
  289. }
  290. /* for PCM codecs, compute bitrate directly */
  291. switch(enc->codec_id) {
  292. case CODEC_ID_PCM_S16LE:
  293. case CODEC_ID_PCM_S16BE:
  294. case CODEC_ID_PCM_U16LE:
  295. case CODEC_ID_PCM_U16BE:
  296. bitrate = enc->sample_rate * enc->channels * 16;
  297. break;
  298. case CODEC_ID_PCM_S8:
  299. case CODEC_ID_PCM_U8:
  300. case CODEC_ID_PCM_ALAW:
  301. case CODEC_ID_PCM_MULAW:
  302. bitrate = enc->sample_rate * enc->channels * 8;
  303. break;
  304. default:
  305. bitrate = enc->bit_rate;
  306. break;
  307. }
  308. break;
  309. default:
  310. av_abort();
  311. }
  312. if (encode) {
  313. if (enc->flags & CODEC_FLAG_PASS1)
  314. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  315. ", pass 1");
  316. if (enc->flags & CODEC_FLAG_PASS2)
  317. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  318. ", pass 2");
  319. }
  320. if (bitrate != 0) {
  321. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  322. ", %d kb/s", bitrate / 1000);
  323. }
  324. }
  325. /* Picture field are filled with 'ptr' addresses */
  326. void avpicture_fill(AVPicture *picture, UINT8 *ptr,
  327. int pix_fmt, int width, int height)
  328. {
  329. int size;
  330. size = width * height;
  331. switch(pix_fmt) {
  332. case PIX_FMT_YUV420P:
  333. picture->data[0] = ptr;
  334. picture->data[1] = picture->data[0] + size;
  335. picture->data[2] = picture->data[1] + size / 4;
  336. picture->linesize[0] = width;
  337. picture->linesize[1] = width / 2;
  338. picture->linesize[2] = width / 2;
  339. break;
  340. case PIX_FMT_YUV422P:
  341. picture->data[0] = ptr;
  342. picture->data[1] = picture->data[0] + size;
  343. picture->data[2] = picture->data[1] + size / 2;
  344. picture->linesize[0] = width;
  345. picture->linesize[1] = width / 2;
  346. picture->linesize[2] = width / 2;
  347. break;
  348. case PIX_FMT_YUV444P:
  349. picture->data[0] = ptr;
  350. picture->data[1] = picture->data[0] + size;
  351. picture->data[2] = picture->data[1] + size;
  352. picture->linesize[0] = width;
  353. picture->linesize[1] = width;
  354. picture->linesize[2] = width;
  355. break;
  356. case PIX_FMT_RGB24:
  357. case PIX_FMT_BGR24:
  358. picture->data[0] = ptr;
  359. picture->data[1] = NULL;
  360. picture->data[2] = NULL;
  361. picture->linesize[0] = width * 3;
  362. break;
  363. case PIX_FMT_RGBA32:
  364. case PIX_FMT_BGRA32:
  365. picture->data[0] = ptr;
  366. picture->data[1] = NULL;
  367. picture->data[2] = NULL;
  368. picture->linesize[0] = width * 4;
  369. break;
  370. case PIX_FMT_YUV422:
  371. picture->data[0] = ptr;
  372. picture->data[1] = NULL;
  373. picture->data[2] = NULL;
  374. picture->linesize[0] = width * 2;
  375. break;
  376. default:
  377. picture->data[0] = NULL;
  378. picture->data[1] = NULL;
  379. picture->data[2] = NULL;
  380. break;
  381. }
  382. }
  383. int avpicture_get_size(int pix_fmt, int width, int height)
  384. {
  385. int size;
  386. size = width * height;
  387. switch(pix_fmt) {
  388. case PIX_FMT_YUV420P:
  389. size = (size * 3) / 2;
  390. break;
  391. case PIX_FMT_YUV422P:
  392. size = (size * 2);
  393. break;
  394. case PIX_FMT_YUV444P:
  395. size = (size * 3);
  396. break;
  397. case PIX_FMT_RGB24:
  398. case PIX_FMT_BGR24:
  399. size = (size * 3);
  400. break;
  401. case PIX_FMT_RGBA32:
  402. case PIX_FMT_BGRA32:
  403. size = (size * 4);
  404. break;
  405. case PIX_FMT_YUV422:
  406. size = (size * 2);
  407. break;
  408. default:
  409. size = -1;
  410. break;
  411. }
  412. return size;
  413. }
  414. unsigned avcodec_version( void )
  415. {
  416. return LIBAVCODEC_VERSION_INT;
  417. }
  418. unsigned avcodec_build( void )
  419. {
  420. return LIBAVCODEC_BUILD;
  421. }
  422. /* must be called before any other functions */
  423. void avcodec_init(void)
  424. {
  425. static int inited = 0;
  426. if (inited != 0)
  427. return;
  428. inited = 1;
  429. //dsputil_init();
  430. }
  431. /* this should be called after seeking and before trying to decode the next frame */
  432. void avcodec_flush_buffers(AVCodecContext *avctx)
  433. {
  434. MpegEncContext *s = avctx->priv_data;
  435. s->num_available_buffers=0;
  436. }
  437. static int raw_encode_init(AVCodecContext *s)
  438. {
  439. return 0;
  440. }
  441. static int raw_decode_frame(AVCodecContext *avctx,
  442. void *data, int *data_size,
  443. UINT8 *buf, int buf_size)
  444. {
  445. return -1;
  446. }
  447. static int raw_encode_frame(AVCodecContext *avctx,
  448. unsigned char *frame, int buf_size, void *data)
  449. {
  450. return -1;
  451. }
  452. AVCodec rawvideo_codec = {
  453. "rawvideo",
  454. CODEC_TYPE_VIDEO,
  455. CODEC_ID_RAWVIDEO,
  456. 0,
  457. raw_encode_init,
  458. raw_encode_frame,
  459. NULL,
  460. raw_decode_frame,
  461. };