/***************************************************************************** * CloseDecoder: daala decoder destruction *****************************************************************************/ static void CloseDecoder( vlc_object_t *p_this ) { decoder_t *p_dec = (decoder_t *)p_this; decoder_sys_t *p_sys = p_dec->p_sys; daala_info_clear(&p_sys->di); daala_comment_clear(&p_sys->dc); daala_decode_free(p_sys->dcx); free( p_sys ); }
static void CloseEncoder( vlc_object_t *p_this ) { encoder_t *p_enc = (encoder_t *)p_this; encoder_sys_t *p_sys = p_enc->p_sys; daala_info_clear(&p_sys->di); daala_comment_clear(&p_sys->dc); daala_encode_free(p_sys->dcx); free( p_sys ); }
int player_example_daala_stream_clear(player_example *player) { if (player == NULL) return -1; daala_info_clear(&player->di); daala_comment_clear(&player->dc); if (player->dctx != NULL) { daala_decode_free(player->dctx); player->dctx = NULL; } ogg_stream_clear(&player->os); player->od_state = ODS_NONE; return 0; }
static gboolean daala_dec_start (GstVideoDecoder * decoder) { GstDaalaDec *dec = GST_DAALA_DEC (decoder); GST_DEBUG_OBJECT (dec, "start"); daala_info_clear (&dec->info); daala_comment_clear (&dec->comment); GST_DEBUG_OBJECT (dec, "Setting have_header to FALSE"); dec->have_header = FALSE; daala_dec_reset (dec); return TRUE; }
static gboolean daala_dec_stop (GstVideoDecoder * decoder) { GstDaalaDec *dec = GST_DAALA_DEC (decoder); GST_DEBUG_OBJECT (dec, "stop"); daala_info_clear (&dec->info); daala_comment_clear (&dec->comment); daala_setup_free (dec->setup); dec->setup = NULL; daala_decode_free (dec->decoder); dec->decoder = NULL; daala_dec_reset (dec); if (dec->input_state) { gst_video_codec_state_unref (dec->input_state); dec->input_state = NULL; } if (dec->output_state) { gst_video_codec_state_unref (dec->output_state); dec->output_state = NULL; } return TRUE; }
int main(int argc, char *argv[]) { daala_packet dp; ogg_packet op; int long_option_index; int c; ogg_sync_state oy; ogg_page og; ogg_stream_state to; daala_info di; daala_comment dc; daala_setup_info *ds; daala_dec_ctx *dd; daala_image img; const char *optstring = "o:r"; struct option options [] = { { "output", required_argument, NULL, 'o' }, { "raw", no_argument, NULL, 'r' }, /*Disable YUV4MPEG2 headers:*/ { "version", no_argument, NULL, 0}, { NULL, 0, NULL, 0 } }; int frames = 0; int pix_fmt = 1; int daala_p = 0; int daala_processing_headers = 0; int stateflag = 0; /* single frame video buffering */ int videobuf_ready = 0; int raw = 0; FILE *outfile = NULL; ogg_int32_t pic_width = 0; ogg_int32_t pic_height = 0; ogg_int32_t fps_num = 0; ogg_int32_t fps_denom = 0; FILE *infile = stdin; outfile = stdout; dd = NULL; ds = NULL; daala_log_init(); #ifdef _WIN32 /* We need to set stdin/stdout to binary mode on windows. */ /* Beware the evil ifdef. We avoid these where we can, but this one we cannot. Don't add any more, you'll probably go to hell if you do. */ _setmode(_fileno(stdin), _O_BINARY); _setmode(_fileno(stdout), _O_BINARY); #endif /* Process option arguments. */ while ((c = getopt_long(argc, argv, optstring, options, &long_option_index)) != EOF) { switch (c) { case 'o': { if (strcmp(optarg, "-") != 0) { outfile = fopen(optarg, "wb"); if (outfile == NULL) { fprintf(stderr, "Unable to open output file '%s'\n", optarg); exit(1); } } else { outfile = stdout; } break; } case 'r': { raw = 1; break; } case 0: { if (strcmp(options[long_option_index].name, "version") == 0) { version(); } break; } default: usage(); break; } } if (optind < argc) { infile = fopen(argv[optind], "rb"); if (infile == NULL) { fprintf(stderr, "Unable to open '%s' for extraction.\n", argv[optind]); exit(1); } if (++optind < argc) { usage(); exit(1); } } /*Ok, Ogg parsing. The idea here is we have a bitstream that is made up of Ogg pages. The libogg sync layer will find them for us. There may be pages from several logical streams interleaved; we find the first daala stream and ignore any others. Then we pass the pages for our stream to the libogg stream layer which assembles our original set of packets out of them. start up Ogg stream synchronization layer */ ogg_sync_init(&oy); /* init supporting Theora structures needed in header parsing */ daala_comment_init(&dc); daala_info_init(&di); /*Ogg file open; parse the headers. Theora (like Vorbis) depends on some initial header packets for decoder setup and initialization. We retrieve these first before entering the main decode loop.*/ /* Only interested in Daala streams */ while (!stateflag) { int ret = buffer_data(infile, &oy); if (ret == 0) break; while (ogg_sync_pageout(&oy, &og) > 0) { int got_packet; ogg_stream_state test; /* is this a mandated initial header? If not, stop parsing */ if (!ogg_page_bos(&og)) { /* don't leak the page; get it into the appropriate stream */ queue_page(&og, &to, daala_p); stateflag = 1; break; } ogg_stream_init(&test, ogg_page_serialno(&og)); ogg_stream_pagein(&test, &og); got_packet = ogg_stream_packetpeek(&test, &op); ogg_to_daala_packet(&dp, &op); /* identify the codec: try daala */ if ((got_packet == 1) && !daala_p && (daala_processing_headers = daala_decode_header_in(&di, &dc, &ds, &dp)) >= 0) { /* it is daala -- save this stream state */ memcpy(&to, &test, sizeof(test)); daala_p = 1; /*Advance past the successfully processed header.*/ if (daala_processing_headers) ogg_stream_packetout(&to, NULL); } else { /* whatever it is, we don't care about it */ ogg_stream_clear(&test); } } /* fall through to non-bos page parsing */ } /* we're expecting more header packets. */ while (daala_p && daala_processing_headers) { int ret; /* look for further daala headers */ while (daala_processing_headers && (ret = ogg_stream_packetpeek(&to, &op))) { if (ret < 0) continue; ogg_to_daala_packet(&dp, &op); daala_processing_headers = daala_decode_header_in(&di, &dc, &ds, &dp); if (daala_processing_headers < 0) { fprintf(stderr, "Error parsing Daala stream headers; " "corrupt stream?\n"); exit(1); } else if (daala_processing_headers >= 0) { /*Advance past the successfully processed header.*/ ogg_stream_packetout(&to, NULL); } daala_p++; } /*Stop now so we don't fail if there aren't enough pages in a short stream.*/ if (!(daala_p && daala_processing_headers)) break; /* The header pages/packets will arrive before anything else we care about, or the stream is not obeying spec */ if (ogg_sync_pageout(&oy, &og) > 0) { queue_page(&og, &to, daala_p); /* demux into the appropriate stream */ } else { if (buffer_data(infile, &oy) == 0) { /* someone needs more data */ fprintf(stderr, "End of file while searching for codec headers.\n"); exit(1); } } } /* and now we have it all. initialize decoders */ if (daala_p) { dump_comments(&dc); dd = daala_decode_create(&di, ds); fprintf(stderr, "Ogg logical stream %lx is Daala %dx%d %.02f fps video\n", to.serialno, di.pic_width, di.pic_height, di.timebase_numerator/(double)di.timebase_denominator*di.frame_duration); } else { /* tear down the partial daala setup */ daala_info_clear(&di); daala_comment_clear(&dc); } /*Either way, we're done with the codec setup data.*/ daala_setup_free(ds); if (!raw && outfile) { static const char *CHROMA_TYPES[5] = { "420jpeg", NULL, "422jpeg", "444", "mono" }; pic_width = di.pic_width; pic_height = di.pic_height; fps_num = di.timebase_numerator; fps_denom = di.timebase_denominator*di.frame_duration; if (di.nplanes > 1) { /*calculate pixel_fmt based on the xdec & ydec values from one of the chroma planes.*/ if (di.plane_info[1].xdec == 1 && di.plane_info[1].ydec == 1) { pix_fmt = 0; } else if (di.plane_info[1].xdec == 1 && di.plane_info[1].ydec == 0) { pix_fmt = 2; } else if (di.plane_info[1].xdec == 0 && di.plane_info[1].ydec == 0) { pix_fmt = 3; } } else { pix_fmt = 4; } if (pix_fmt >= 5 || pix_fmt == 1) { fprintf(stderr, "Unknown pixel format: %i\n", pix_fmt); exit(1); } /*Store header information*/ fprintf(outfile, "YUV4MPEG2 W%d H%d F%d:%d Ip A%d:%d C%s\n", pic_width, pic_height, fps_num, fps_denom, di.pixel_aspect_numerator, di.pixel_aspect_denominator, CHROMA_TYPES[pix_fmt]); } /* install signal handler */ signal(SIGINT, sigint_handler); /*Finally the main decode loop. It's one Daala packet per frame, so this is pretty straightforward if we're not trying to maintain sync with other multiplexed streams. The videobuf_ready flag is used to maintain the input buffer in the libogg stream state. If there's no output frame available at the end of the decode step, we must need more input data. We could simplify this by just using the return code on ogg_page_packetout(), but the flag system extends easily to the case where you care about more than one multiplexed stream (like with audio playback). In that case, just maintain a flag for each decoder you care about, and pull data when any one of them stalls.*/ stateflag = 0; /* playback has not begun */ /* queue any remaining pages from data we buffered but that did not contain headers */ while (ogg_sync_pageout(&oy, &og) > 0) { queue_page(&og, &to, daala_p); } while (!got_sigint) { while (daala_p && !videobuf_ready) { if (ogg_stream_packetout(&to, &op) > 0) { ogg_to_daala_packet(&dp, &op); if (daala_decode_packet_in(dd, &dp) >= 0) { videobuf_ready = 1; frames++; } } else break; } if (!videobuf_ready && feof(infile)) break; if (!videobuf_ready) { /* no data yet for somebody. Grab another page */ buffer_data(infile, &oy); while (ogg_sync_pageout(&oy, &og) > 0) { queue_page(&og, &to, daala_p); } } /* dumpvideo frame, and get new one */ else if (outfile && daala_decode_img_out(dd, &img)) { video_write(outfile, &img, raw); } videobuf_ready = 0; } /*Flush the output frame buffer of decoder.*/ while (!got_sigint && daala_decode_img_out(dd, &img)) { video_write(outfile, &img, raw); } /* end of decoder loop -- close everything */ if (daala_p) { ogg_stream_clear(&to); daala_decode_free(dd); daala_comment_clear(&dc); daala_info_clear(&di); } ogg_sync_clear(&oy); if (infile && infile != stdin) fclose(infile); if (outfile && outfile != stdout) fclose(outfile); fprintf(stderr, "\n\n%d frames\n", frames); fprintf(stderr, "\nDone.\n"); return 0; }
static int open_encoder(struct videnc_state *ves, const struct vidsz *size) { daala_info di; daala_comment dc; daala_packet dp; int err = 0; int complexity = 7; int video_q = 30; int bitrate = ves->bitrate; info("daala: open encoder (%d x %d, %d bps)\n", size->w, size->h, bitrate); if (ves->enc) { debug("daala: re-opening encoder\n"); daala_encode_free(ves->enc); } daala_info_init(&di); daala_comment_init(&dc); di.pic_width = size->w; di.pic_height = size->h; di.timebase_numerator = 1; di.timebase_denominator = ves->fps; di.frame_duration = 1; di.pixel_aspect_numerator = -1; di.pixel_aspect_denominator = -1; di.nplanes = 3; di.plane_info[0].xdec = 0; /* YUV420P */ di.plane_info[0].ydec = 0; di.plane_info[1].xdec = 1; di.plane_info[1].ydec = 1; di.plane_info[2].xdec = 1; di.plane_info[2].ydec = 1; di.keyframe_rate = 100; info("daala: open encoder with bitstream version %u.%u.%u\n", di.version_major, di.version_minor, di.version_sub); ves->enc = daala_encode_create(&di); if (!ves->enc) { warning("daala: failed to open DAALA encoder\n"); return ENOMEM; } daala_encode_ctl(ves->enc, OD_SET_QUANT, &video_q, sizeof(video_q)); daala_encode_ctl(ves->enc, OD_SET_COMPLEXITY, &complexity, sizeof(complexity)); daala_encode_ctl(ves->enc, OD_SET_BITRATE, &bitrate, sizeof(bitrate)); for (;;) { int r; r = daala_encode_flush_header(ves->enc, &dc, &dp); if (r < 0) { warning("daala: flush_header returned %d\n", r); break; } else if (r == 0) break; debug("daala: header: %lld bytes header=%d key=%d\n", dp.bytes, daala_packet_isheader(&dp), daala_packet_iskeyframe(&dp)); #if 0 re_printf("bos=%lld, eos=%lld, granule=%lld, packetno=%lld\n", dp.b_o_s, dp.e_o_s, dp.granulepos, dp.packetno); #endif err = send_packet(ves, dp.b_o_s, dp.packet, dp.bytes); if (err) break; } daala_info_clear(&di); daala_comment_clear(&dc); return err; }