Skip to content

Commit

Permalink
音声入力がパイプとかの場合でも動くようにさせる
Browse files Browse the repository at this point in the history
flacのput-flac()を自前で実装

入力がパイプだった時の対策の一環

音声入力がパイプとかの場合でも動くようにさせる

……やつの残り

無駄な条件判別を修正

修正中に不要になったカタログメッセージを削除
  • Loading branch information
hcmiya committed Jun 4, 2019
1 parent 21eabe4 commit dbd734b
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 75 deletions.
1 change: 1 addition & 0 deletions src/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ GLOBAL bool leave_header_packets;

GLOBAL FILE *stream_input, *built_stream, *tag_output;
GLOBAL bool tag_output_to_file;
GLOBAL bool input_is_regular_file;

GLOBAL pthread_t parser_thread;

Expand Down
17 changes: 14 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#include <signal.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>


#define GLOBAL_MAIN
#include "opuscomment.h"
Expand Down Expand Up @@ -390,9 +393,17 @@ int main(int argc, char **argv) {
}

O.in = argv[optind];
stream_input = fopen(O.in, "r");
if (!stream_input) {
fileerror(O.in);
if (strcmp(O.in, "-")) {
stream_input = fopen(O.in, "r");
if (!stream_input) {
fileerror(O.in);
}
struct stat sb;
fstat(fileno(stream_input), &sb);
input_is_regular_file = S_ISREG(sb.st_mode);
}
else {
stream_input = stdin;
}

open_output_file();
Expand Down
3 changes: 1 addition & 2 deletions src/opuscomment.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ struct edit_st {
void parse_opt_tag(int, char const*);
void pticonv_close(void);
void read_page(ogg_sync_state*);
noreturn void read_flac(void);
noreturn void put_left(long rew);
void read_flac(void);
void move_file(void);
iconv_t iconv_new(char const *to, char const *from);
void open_output_file(void);
Expand Down
13 changes: 8 additions & 5 deletions src/parse-tags.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "opuscomment.h"

static bool from_file;
static FILE *edit_input;
static void readerror(void) {
if (from_file) {
fileerror(O.tag_filename);
Expand Down Expand Up @@ -66,7 +67,7 @@ static void toutf8(int fdu8) {
iconv_t cd = iconv_new("UTF-8", O.tag_raw ? "UTF-8" : nl_langinfo(CODESET));
size_t readlen, remain, total;
remain = 0; total = 0;
while ((readlen = fread(&lbuf[remain], 1, buflen - remain, stdin)) != 0) {
while ((readlen = fread(&lbuf[remain], 1, buflen - remain, edit_input)) != 0) {
total += readlen;
if (total > TAG_LENGTH_LIMIT__INPUT) {
mainerror(err_main_long_input);
Expand Down Expand Up @@ -96,7 +97,7 @@ static void toutf8(int fdu8) {
if (iconvret != (size_t)-1 || ie == EINVAL) break;
}
}
if (fclose(stdin) == EOF) {
if (fclose(edit_input) == EOF) {
readerror();
}
if (remain) {
Expand Down Expand Up @@ -539,11 +540,14 @@ void *parse_tags(void* nouse_) {
}
if (O.tag_filename && strcmp(O.tag_filename, "-") != 0) {
from_file = true;
FILE *tmp = freopen(O.tag_filename, "r", stdin);
if (!tmp) {
edit_input = fopen(O.tag_filename, "r");
if (!edit_input) {
fileerror(O.tag_filename);
}
}
else {
edit_input = stdin;
}

if (do_read) {
// UTF-8化された文字列をチャンク化する処理をスレッド化(化が多い)
Expand All @@ -558,7 +562,6 @@ void *parse_tags(void* nouse_) {
toutf8(pfd[1]);
pthread_join(split_thread, NULL);
}
fclose(stdin);

struct edit_st *rtn = calloc(1, sizeof(*rtn));
rtn->str = strstore;
Expand Down
8 changes: 7 additions & 1 deletion src/read-flac.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,11 @@ void read_flac(void) {
store_tags_flac(rst, est);
}
write_buffer("\x81\0\0", 4, built_stream); // 「最後のヘッダ」標識を立てたパディングで〆
put_left(-ftell(stream_input)); // "seeked_len(0) - rew" でfseek()するので

opst = PAGE_SOUND;
while (readlen = fread(gbuf, 1, gbuflen, stream_input)) {
size_t writelen = fwrite(gbuf, 1, readlen, built_stream);
if (writelen != readlen) oserror();
}
if (ferror(stream_input)) oserror();
}
147 changes: 85 additions & 62 deletions src/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

#include "opuscomment.h"

bool parse_comment(ogg_page *og);
static bool parse_comment_term(ogg_page *og, bool packet_break_in_page);
bool parse_page_sound(ogg_page *og);

static long seeked_len;
static char *outtmp;
static bool remove_tmp;
Expand All @@ -22,7 +26,7 @@ void move_file(void) {
if (fclose(built_stream) == EOF) {
fileerror(O.out ? O.out : outtmp);
}
if (O.out) {
if (O.out || !input_is_regular_file) {
}
else {
int fd = fileno(stream_input);
Expand All @@ -39,7 +43,7 @@ void move_file(void) {
}
}

noreturn void put_left(long rew) {
static noreturn void put_left(long rew) {
clearerr(stream_input);
if (fseek(stream_input, seeked_len - rew, SEEK_SET)) {
oserror();
Expand All @@ -60,6 +64,19 @@ noreturn void put_left(long rew) {
exit(0);
}

static void put_non_opus_stream() {
rewind(non_opus_stream);
uint8_t buf[STACK_BUF_LEN];
size_t readlen;
while (readlen = fread(buf, 1, STACK_BUF_LEN, non_opus_stream)) {
if (fwrite(buf, 1, readlen, built_stream) != readlen) {
oserror();
}
}
fclose(non_opus_stream);
non_opus_stream = NULL;
}

void store_tags(ogg_page *np, struct rettag_st *rst, struct edit_st *est, bool packet_break_in_page) {
FILE *fptag = rst->tag;
size_t const pagebuflen = 65536; // oggページの最大長 = 65307
Expand Down Expand Up @@ -136,17 +153,19 @@ void store_tags(ogg_page *np, struct rettag_st *rst, struct edit_st *est, bool p
ogg_page_checksum_set(&og);
write_page(&og, built_stream);

// 出力するタグ部分のページ番号が入力の音声開始部分のページ番号に満たない場合、
// 空のページを生成して開始ページ番号を合わせる
og.header_len = 27;
og.header[5] = 0;
set_granulepos(&og, -1);
og.header[26] = 0;
og.body_len = 0;
while (idx < opus_idx - packet_break_in_page) {
set_pageno(&og, idx++);
ogg_page_checksum_set(&og);
write_page(&og, built_stream);
if (input_is_regular_file) {
// 出力するタグ部分のページ番号が入力の音声開始部分のページ番号に満たない場合、
// 空のページを生成して開始ページ番号を合わせる
og.header_len = 27;
og.header[5] = 0;
set_granulepos(&og, -1);
og.header[26] = 0;
og.body_len = 0;
while (idx < opus_idx - packet_break_in_page) {
set_pageno(&og, idx++);
ogg_page_checksum_set(&og);
write_page(&og, built_stream);
}
}

if (packet_break_in_page) {
Expand All @@ -168,18 +187,9 @@ void store_tags(ogg_page *np, struct rettag_st *rst, struct edit_st *est, bool p
write_page(np, built_stream);
}

{
rewind(non_opus_stream);
size_t readlen;
while (readlen = fread(pagebuf, 1, pagebuflen, non_opus_stream)) {
if (fwrite(pagebuf, 1, readlen, built_stream) != readlen) {
oserror();
}
}
fclose(non_opus_stream);
non_opus_stream = NULL;
}
if (idx == opus_idx) {
put_non_opus_stream();

if (input_is_regular_file && idx == opus_idx) {
put_left(0);
/* NOTREACHED */
}
Expand Down Expand Up @@ -228,24 +238,29 @@ void open_output_file(void) {
remove_tmp = false;
}
else {
char const *tmpl = "opuscomment.XXXXXX";
outtmp = calloc(strlen(O.in) + strlen(tmpl) + 1, 1);
char *p = strrchr(O.in, '/');
if (p) {
p++;
strncpy(outtmp, O.in, p - O.in);
strcpy(outtmp + (p - O.in), tmpl);
if (input_is_regular_file) {
char const *tmpl = "opuscomment.XXXXXX";
outtmp = calloc(strlen(O.in) + strlen(tmpl) + 1, 1);
char *p = strrchr(O.in, '/');
if (p) {
p++;
strncpy(outtmp, O.in, p - O.in);
strcpy(outtmp + (p - O.in), tmpl);
}
else {
strcpy(outtmp, tmpl);
}
int fd = mkstemp(outtmp);
if (fd == -1) {
oserror();
}
remove_tmp = true;
atexit(cleanup);
built_stream = fdopen(fd, "w+");
}
else {
strcpy(outtmp, tmpl);
}
int fd = mkstemp(outtmp);
if (fd == -1) {
oserror();
built_stream = stdout;
}
remove_tmp = true;
atexit(cleanup);
built_stream = fdopen(fd, "w+");
}
non_opus_stream = tmpfile();

Expand Down Expand Up @@ -346,8 +361,6 @@ static bool copy_tag_packet(ogg_page *og, bool *packet_break_in_page) {
return packet_term;
}

bool parse_comment(ogg_page *og);

static pthread_t retriever_thread;
bool parse_info_border(ogg_page *og) {
// ここにはページ番号1で来ているはず
Expand All @@ -358,23 +371,31 @@ bool parse_info_border(ogg_page *og) {

if (/*O.gain_fix && */O.edit == EDIT_NONE) {
// 出力ゲイン編集のみの場合
if (O.out) {
// 出力指定が別にある場合残りをコピー
put_left(og->header_len + og->body_len);
if (input_is_regular_file) {
if (O.out) {
// 出力指定が別にある場合残りをコピー
put_left(og->header_len + og->body_len);
}
else {
// 上書きするなら最初のページのみを直接書き換えようとする
uint8_t b[header_packet_len];
FILE *stream_overwrite = freopen(NULL, "r+", stream_input);
if (!stream_overwrite
|| fseek(built_stream, built_header_pos, SEEK_SET)
|| fseek(stream_overwrite, header_packet_pos, SEEK_SET)
|| header_packet_len != fread(b, 1, header_packet_len, built_stream)
|| header_packet_len != fwrite(b, 1, header_packet_len, stream_overwrite)
) {
oserror();
}
exit(0);
}
}
else {
// 上書きするなら最初のページのみを直接書き換えようとする
uint8_t b[header_packet_len];
FILE *stream_overwrite = freopen(NULL, "r+", stream_input);
if (!stream_overwrite
|| fseek(built_stream, built_header_pos, SEEK_SET)
|| fseek(stream_overwrite, header_packet_pos, SEEK_SET)
|| header_packet_len != fread(b, 1, header_packet_len, built_stream)
|| header_packet_len != fwrite(b, 1, header_packet_len, stream_overwrite)
) {
oserror();
}
exit(0);
// 入力がパイプの時
put_non_opus_stream();
opst = PAGE_SOUND;
return parse_page_sound(og);
}
/* NOTREACHED */
}
Expand All @@ -391,9 +412,6 @@ bool parse_info_border(ogg_page *og) {
return parse_comment(og);
}

static bool parse_comment_term(ogg_page *og, bool packet_break_in_page);
bool parse_page_sound(ogg_page *og);

bool parse_comment(ogg_page *og) {
if (ogg_page_eos(og) || opus_idx > 1 && !ogg_page_continued(og)) {
opuserror(err_opus_bad_stream);
Expand Down Expand Up @@ -439,8 +457,13 @@ bool parse_page_sound(ogg_page *og) {
ogg_page_checksum_set(og);
write_page(og, built_stream);
if (ogg_page_eos(og)) {
put_left(0);
/* NOTREACHED */
if (input_is_regular_file) {
put_left(0);
/* NOTREACHED */
}
else {
opus_idx_diff = 0;
}
}
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/version.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#define OPUSCOMMENT_VERSION "1.5.8"
#define OPUSCOMMENT_VERSION "1.5.9"

#define OPUSCOMMENT_REVISION_YEAR (2019 - 1900)
#define OPUSCOMMENT_REVISION_MONTH (6 - 1)
#define OPUSCOMMENT_REVISION_DAY 2
#define OPUSCOMMENT_REVISION_DAY 4

0 comments on commit dbd734b

Please sign in to comment.